5

With the help of this and this I made a plugin to add a custom fields to the article component. It works fine but I would like to make these custom fields mandatory and only for a specific article category.

The plugin is called "acf"

acf.php :

 defined ( '_JEXEC' ) or die ( 'Restricted access' );
 class plgContentAcf extends JPlugin {
    protected $autoloadLanguage = true;
    function onContentPrepareForm($form, $data) {
        $app = JFactory::getApplication();
        $option = $app->input->get('option');
        switch($option) {
            case 'com_content':
                if ($app->isAdmin()) {
                    JForm::addFormPath(__DIR__ . '/forms');
                    $form->loadFile('content', false);
                }
                return true;
        }
        return true;
    }
 }
 ?>

acf.xml :

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.4" type="plugin" group="content" method="upgrade">
    <name>plg_content_acf</name>
    <author></author>
    <creationDate></creationDate>
    <copyright></copyright>
    <license></license>
    <authorEmail></authorEmail>
    <authorUrl></authorUrl>
    <version>1.0.0</version>
    <description>This is the description of the plugin</description>
    <files>
        <filename plugin="acf">acf.php</filename>
        <filename>index.html</filename>
        <folder>forms</folder>
    </files>
    <languages>
        <language tag="en-GB">en-GB.plg_content_acf.ini</language>
        <language tag="en-GB">en-GB.plg_content_acf.sys.ini</language>
    </languages>
</extension>

forms/content.php:

<?xml version="1.0" encoding="UTF-8"?>
<form>
  <fields name="attribs" >
    <fieldset name="acf" >
      <field
        type="filelist"
        name="path"
        label="Document"
        directory="images/documents"
        filter="\.pdf$"
        hide_default="true"
        validate="options"
      />
      <field
        name="anchor"
        type="text"
        label="Anchor"
        description="Description here"
        class="required"
      />
    </fieldset>
  </fields>
</form>
  • What about adding a category selector as a plugin parameter in your XML file? – Lodder Mar 21 '16 at 9:26
  • @Lodder that would be great, how should I do that (I edited the acf.xml file in the question)? – web-tiki Mar 21 '16 at 9:30
  • 2
    I'm just mocking up and code and testing. One question though....you want this to only appear on articles that are assigned to a specific category, but what if you're creating a new article. You first have to assign the category, save it, then the custom form fields will appear, then you need to save again. – Lodder Mar 21 '16 at 9:45
  • thx @Lodder good question. What if the custom field would alway render, and be hidden/shown as the category is selected with JS. Then the only issue would be to allow other categories to be saved without the mandatory custom field. Is that possible? – web-tiki Mar 21 '16 at 9:52
5

Right, been playing around and got it working.

You're firstly going to need to add the parameter to select a category to your acf.xml file, like so:

<config>
    <fields name="params">
        <fieldset name="basic">
            <field name="category" type="category" extension="com_content" label="Select a category" />
        </fieldset>
    </fields>
</config>

Then in your acf.php:

class plgContentAcf extends JPlugin 
{
    protected $autoloadLanguage = true;

    function onContentPrepareForm($form, $data)
    {
        $app = JFactory::getApplication();

        if ($app->isAdmin())
        {
            $option = $app->input->get('option');
            $view   = $app->input->get('view');

            if ($option === 'com_content' && $view === 'article')
            {
                $model = JModelLegacy::getInstance('Article', 'ContentModel');
                $catid = $model->getItem($app->input->get('id'))->catid;

                if ($this->params->get('category') === $catid)
                {
                    JForm::addFormPath(__DIR__ . '/forms');
                    $form->loadFile('content', false);
                }
            }

            return true;
        }
    }
}

As you can see, I've made a few changes to the code, such as:

  • Remove the switch/case.
  • Ensure all code is only executed when isAdmin is true
  • Ensure the form field and params are retrieved only on the article view in the com_content component

The form is then only loaded when the ID of the category selected in the plugin options, match the category ID of the current article.

Hope this helps

  • This works like a charm and will get me going. Thx a lot – web-tiki Mar 21 '16 at 10:43
  • 1
    Nice job guys... Can you find a way to share installable package with the rest of us, by any chance? ;-) – Mikan Mar 21 '16 at 10:49
  • @Mikan, I wont do that cause it's not my extension to make available. If the OP wants to, I think Github would be a good place to put it ;) – Lodder Mar 21 '16 at 10:56
  • @Lodder - Agreed, on both counts... :-) – Mikan Mar 21 '16 at 11:00
  • @web-tiki - You can do if you like :) – Lodder Mar 21 '16 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.