0

Despite training, too many users write their content (in the frontend) and save it without ever selecting a category. The content category dropdown is 'out of sight and out of mind' hidden behind the Publishing tab which many users never click on.

Currently, if the user makes no category selection, his article will end up in the first category by default. Can this be changed so that an article cannot be saved without a category having been actively chosen?

Many thanks.

1 Answer 1

0

An option which might help would be to move the category field from the publishing tab to the main tab. You can do that by creating a template override for the tmpl file which is components/com_content/tmpl/form/edit.php. Just move the line:

<?php echo $this->form->renderField('catid'); ?>

up so that it's near the line:

<?php echo $this->form->renderField('articletext'); ?>

To force the user to actively make a selection you need to change the catid field. The form is in components/com_content/forms/article.php.

You need to change the field

    <field
        name="catid"
        type="categoryedit"
        label="JCATEGORY"
        extension="com_content"
        required="true"
    />

to be something like:

    <field
        name="catid"
        type="categoryedit"
        label="JCATEGORY"
        extension="com_content"
        required="true"
    >
        <option>Please select one</option>
    </field>

However, you shouldn't just edit this form XML file as your edits will be overwritten when the next Joomla upgrade is applied.

Instead you should write a content plugin which listens for onContentPrepareForm and then change the form programmatically. How you do this is described in Dynamically Changing Forms.

You could try defining a variable $xml to be a string with the above field definition, and then using load:

$form->load($xml, true);

to replace the definition of the 'catid' field with your own.

I'm not 100% sure if this will work or not. It might need you to do more complicated manipulations of the form.

UPDATE

I wrote up the plugin to check it and the above approach works. So if you decide to try the plugin approach then here's the key section of code:

public function onContentPrepareForm(Event $event)
{
    if (!$this->getApplication()->isClient('site')) {
        return;
    }
    [$form, $data] = array_values($event->getArguments());
    if ($form->getName() !== 'com_content.article') {
        return;
    }
    $xml = <<<EOD
    <field
        name="catid"
        type="categoryedit"
        label="JCATEGORY"
        extension="com_content"
        required="true"
    >
        <option>Please select one</option>
    </field>
    EOD;
    $form->load($xml, true);
}

It may be helpful to look at this Plugin Tutorial, and you shouldn't find it too difficult to modify that for this case.

It's also worth moving the Category to the first tab as well, because if a user doesn't enter a category then the error message doesn't direct them to that particular category field, and could be confusing for them.

1
  • Thank you Robbie. Overriding the edit.php file to make the dropdown more prominent was something I'd considered too. That, along with a prominent notice to select a category might be my best option as creating a plugin may be above my skillset. I'll certainly give it a go on my dev site though. Thanks for pointing me in the right direction (and for your work on the Joomla documentation).
    – Michael
    Commented Sep 6 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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