User tests: Successful: Unsuccessful:
Pull Request resolves #36544
Custom fields assigned to a specific category were incorrectly appearing on the new category form even when they had no relevance to the category being created.
Why this happened:
In FieldsHelper::prepareForm(), the variable $assignedCatids is resolved from $data->catid, $data->fieldscatid, or the form's catid value. For a new category form, none of these are available — a category does not belong to another category via a catid field the way an article does. So $assignedCatids resolves to 0.
When getFields() is then called with $data, the condition that populates filter.assigned_cat_ids:
if ($item && (isset($item->catid) || isset($item->fieldscatid))) {...is never entered, because neither catid nor fieldscatid is set on $data. This means filter.assigned_cat_ids stays as [] (empty), and the model query returns all fields for the context with no category filtering — including fields that are assigned to specific categories.
There is a guard in the code intended to handle this case:
if (!$assignedCatids && !empty($field->assigned_cat_ids) && $form->getField($field->name)) {
$form->setFieldAttribute($field->name, 'required', 'false');
}However, this guard never fires because $field->assigned_cat_ids is not populated on the field object returned by the model — it is a filter parameter, not a field property. So category-specific fields render unconditionally on the new category form, and if any of them are marked as required, the user is blocked from saving a new category unless they fill in a field that is irrelevant to the category being created.
The fix:
In prepareForm(), before the second call to getFields(), explicitly set $data->fieldscatid = 0 when no category context can be determined and neither catid nor fieldscatid is already set on $data:
if (!$assignedCatids && !isset($data->catid) && !isset($data->fieldscatid)) {
$data->fieldscatid = 0;
}This causes getFields() to enter the filtering condition and set filter.assigned_cat_ids to [0], which restricts the model query to return only fields with no category assignment — the correct behavior when no category context is known.
The custom Fields tab loads on the new category form and displays "Test category field" even though it is assigned specifically to "Test category with field". Because the field is marked as required, the new category cannot be saved unless the custom field is filled in, even though it has no relevance to the category being created. Switching the parent category has no effect on the displayed fields until after saving.
The new category form loads without the custom field that is assigned to "Test category with field". The Fields tab either does not appear or shows only fields that have no specific category assignment. The new category can be saved without being blocked by irrelevant required fields.
Please select:
<link><link>| Status | New | ⇒ | Pending |
| Category | ⇒ | Administration com_fields |
| Labels |
Added:
PR-5.4-dev
|
||