Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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">
    <description>This is the description of the plugin</description>
        <filename plugin="acf">acf.php</filename>
        <language tag="en-GB">en-GB.plg_content_acf.ini</language>
        <language tag="en-GB">en-GB.plg_content_acf.sys.ini</language>


<?xml version="1.0" encoding="UTF-8"?>
  <fields name="attribs" >
    <fieldset name="acf" >
        description="Description here"
share|improve this question
What about adding a category selector as a plugin parameter in your XML file? – Lodder Mar 21 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 at 9:30
I'm just mocking up and code and testing. One question 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 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 at 9:52
up vote 5 down vote accepted

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:

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

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

share|improve this answer
This works like a charm and will get me going. Thx a lot – web-tiki Mar 21 at 10:43
Nice job guys... Can you find a way to share installable package with the rest of us, by any chance? ;-) – Mikan Mar 21 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 at 10:56
@Lodder - Agreed, on both counts... :-) – Mikan Mar 21 at 11:00
@web-tiki - Great and thank you... – Mikan Mar 21 at 11:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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