0

I am looking for a solution for the following requirements:

  1. Display only the category of first level when creating new category.
  2. Display only the category of second level when creating article in Joomla.

enter image description here

1
  • Welcome to JSE. Please take our tour while you wait for support. Please edit your question to include any progress that you have made. Show us some proof of researcher (which documentation pages you've already read that weren't helpful). Right now, your question is a "requirements dump", but we would appreciate if you would show us your efforts prior to asking. Please continue to researcher and attempt to self-solve. If you find a solution, you are welcome to answer your own question in a way which will benefit future researchers. Commented Apr 7, 2020 at 21:16

1 Answer 1

0

What you could do is create a couple of custom form fields which restrict the categories in the way you want. Then write a plugin which will trigger on onContentPrepareForm and change the relevant form fields to the custom ones.

Start with the existing category form field code in administrator/components/com_categories/models/fields/categoryedit.php.

You need to change 3 things -

  1. Change the class name - eg use JFormFieldCategoryEdit1
  2. Change the field type - eg public $type = 'CategoryEdit1';
  3. Add an extra where clause to the query in getOptions()

$query->where('a.level = 1'); and level = 2 for the other custom field

Then save the file somewhere - eg in a customisations directory.

Then create a content plugin which will change the type of the category field to the one you want, eg:

 public function onContentPrepareForm(JForm $form, $data)
 {
    if ($form->getName() == "com_content.article" && isset($data->id)) 
    {
        $form->setFieldAttribute("catid", "type", "categoryedit2");
        JForm::addFieldPath(JPATH_ROOT . '/customisations');
    }
    if ($form->getName() == "com_categories.categorycom_content" && !isset($data->id)) 
    {
        $form->setFieldAttribute("parent_id", "type", "categoryedit1");
        JForm::addFieldPath(JPATH_ROOT . '/customisations');
    }
    return true;
}

I tested the code a bit, but not thoroughly, so you may need to change some things. But the general idea works ok.

2
  • 1
    Your snippet should be using elseif instead of two separate if blocks, right? I would probably declare $form->getName() as a variable, so as not call the same method twice. Commented Apr 8, 2020 at 0:19
  • Yes, if you're using this code you'll probably want to tidy it up as suggested, including also putting db->quote() around 'level' Commented Apr 8, 2020 at 17:57

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.