No Code Attached Yet
avatar crystalenka
crystalenka
13 Sep 2021

Steps to reproduce the issue

  1. Create a category custom field (text, editor, or media) marked as required and as subform only.
  2. If text or editor is your field type of choice, set the field to filter Safe HTML, alphanumeric, or telephone.
  3. Go to any content category.
  4. The field does not render (as expected).
  5. Try to save the category.

Expected result

The category saves as normal, since the field is subform only and not included in a subform assigned to this category.

Actual result

The category errors on save, asking for the required field.
Screen Shot 2021-09-13 at 11 13 47

System information (as much as possible)

Info Detail
Joomla! Version Joomla! 4.0.2 Stable [ Furaha ] 24-August-2021 19:54 GMT
PHP Version 7.4.12

Additional comments

Additionally, jcfields loads the field in the front end even if there is no content and it is not assigned to a subform.
Screen Shot 2021-09-13 at 11 12 04

avatar crystalenka crystalenka - open - 13 Sep 2021
avatar joomla-cms-bot joomla-cms-bot - change - 13 Sep 2021
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 13 Sep 2021
avatar crystalenka
crystalenka - comment - 13 Sep 2021

Sorry, one more detail: (I will update above also)

Set the field to filter Safe HTML, alphanumeric, or telephone. (Every other filter works fine)

avatar crystalenka crystalenka - change - 13 Sep 2021
The description was changed
avatar crystalenka crystalenka - edited - 13 Sep 2021
avatar nikosdion
nikosdion - comment - 13 Sep 2021

I can confirm the issue and I have figure out why it happens. This is a two part issue. I will make a PR to solve both parts.

Part I. “Only Use In Subform” doesn't do anything

The new “Only Use In Subform” option in a Custom Field's definition not doing what it's supposed to do.

Supposedly, custom fields with this option enabled would be excluded from everything except defining or rendering a subform custom field.

In reality, this option does nothing useful. It displays a badge in the fields list. Oh, yeah, it also makes the Custom Fields where this option is enabled have a category list of [-1]. That's the beginning, middle and end of it. Totally useless.

Part II. PlgSystemFields::onContentPrepareForm doesn't actually work form com_categories

You will see that the system plugin ALLEGEDLY has special handling for editing categories in com_categories. Allegedly, because it works on the edit page but NOT when saving! When saving all fields from all categories are loaded since the plugin runs before the form data is loaded from the request, meaning there is no category filter present.

You see, $data is an empty array when you the onContentPrepareForm event is fired when saving the form. I see that the choice to not populate the form data when loading the form in \Joomla\CMS\MVC\Controller\FormController::save dates back to 2010 when Andrew made a commit about fixing some unspecified issue saving plugins.

The problem is that for com_categories and only this component we actually need to load the form data from the request when saving data. Otherwise PlgSystemFields:: onContentPrepareForm sees that $data is empty and doesn't set the category ID. As a result FieldsHelper::prepareForm populates $assignedCatids to null, therefore it loads all fields from all categories. If any of these fields is required AND has a validator which doesn't accept NULL values — like the ones @crystalenka mentioned — you get a save error.

There are a few options to fix it. One is to override CategoryController::save. However, this results in severe code duplication which will be a pain in the posterior to manage whenever the base FormController::save method is updated.

The other option is to force CategoryModel::getForm to always load the data. However, this may break b/c; I'm especially concerned about third party plugins which may use the empty $data as a signal when figuring out which fields to add to the category edit view.

The final option is to change PlgSystemFields::onContentPrepareForm itself. Since we're doing special handling for com_categories we might just as well populate data from the request if we are saving the form. This is what I plan on doing. To keep things b/c this nasty workaround will ONLY apply to com_content categories, not third party extensions' categories. In fact, custom fields SHOULD only apply to com_content categories — which is not the case now — but that's a different bug for a different day, n'est ce pas?

avatar crystalenka
crystalenka - comment - 13 Sep 2021

I always find the weird ones...

avatar nikosdion
nikosdion - comment - 13 Sep 2021

Pull request in #35547 Please test.

avatar brianteeman
brianteeman - comment - 13 Sep 2021

I always find the weird ones...

/me struggling to resist the temptation to make a comment that its not just in code

avatar brianteeman
brianteeman - comment - 13 Sep 2021

In fact, custom fields SHOULD only apply to com_content categories — which is not the case now —

Why? We have custom fields available in other components that use categories.

avatar alikon alikon - change - 13 Sep 2021
Status New Closed
Closed_Date 0000-00-00 00:00:00 2021-09-13 12:10:35
Closed_By alikon
avatar alikon alikon - close - 13 Sep 2021
avatar alikon
alikon - comment - 13 Sep 2021

closing as we have pr #35547

avatar nikosdion
nikosdion - comment - 13 Sep 2021

@brianteeman Which ones and how?!

The only two options I get in the Joomla 4 fields component is Categories and Articles, the former only listing com_content categories when I define a field. It does not list Banner categories, nor does it list Akeeba Ticket System categories. I checked, since I'd really love to use custom fields everywhere (they are far more useful than what most people think).

Seriously, I want to know! How can you use custom fields in 3PD extensions? Where is the documentation? I have a very obvious use case with my own software (Akeeba Ticket System) and I could use them for some site building use cases I have in mind. But... how?

avatar brianteeman
brianteeman - comment - 13 Sep 2021

ok - talking in non code language so dont bite me for using the wrong terminology.

The only two options I get in the Joomla 4 fields component is Categories and Articles, the former only listing com_content categories when I define a field

Joomla fields uses "context" and you can see in core that it is used in content, users, contacts and categories.

Seriously, I want to know! How can you use custom fields in 3PD extensions? Where is the documentation? I

I assume that you missed this https://docs.joomla.org/J3.x:Adding_custom_fields/Implement_into_your_component - yeah I know its crazy but there is actually documentation for it.

avatar nikosdion
nikosdion - comment - 13 Sep 2021

@brianteeman Thanks! I couldn't find this documentation page as I would never think to look into a tutorial on using custom fields to find the developer's documentation. It's like putting tomatoes in the fruit isle. Technically correct but people are not going to find them there unless someone tells them or they bump into it by accident. No sane person would be looking for tomatoes anywhere outside the vegetables isle.

In this case I am going to remove the conditional from the PR.

avatar brianteeman
brianteeman - comment - 13 Sep 2021

I found it by searching adding custom fields and it was the first page in my results ;) dont get me started on how truly awful the search experience is on that site but at least it finds things which duck duck go never finds on your site

I'm sure that there have been several talks at JAB about this topic as well - might be worth checking there.

avatar nikosdion
nikosdion - comment - 13 Sep 2021

My site is plagued by VERY bad keywords in ticket titles, courtesy of clients whop misidentify the actual issue 95% of the time. No search engine seems to truly figure out the weight of created date in returning search results from a support section either. Last week I had someone ask me about the MINISUPPORT subscription which was available for all of 7 months back in 2011. Search is a HARD problem.

Anyway, I removed the conditional from the PR so it now applies to all content categories. Hopefully it doesn't break anything for anyone ??

Add a Comment

Login with GitHub to post a comment