I am looking for a solution for the following requirements:
- Display only the category of first level when creating new category.
- Display only the category of second level when creating article in Joomla.
I am looking for a solution for the following requirements:
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 -
JFormFieldCategoryEdit1
public $type = 'CategoryEdit1';
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.
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