Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional custom fields with showon #39051

Merged
merged 5 commits into from Jan 12, 2023
Merged

Conversation

obuisard
Copy link
Contributor

This is a follow up on discussions with users who needed the functionality and with Guido @degobbis who provided a widely used plugin which adds the functionality to the content and user contexts.

Summary of Changes

The showon attribute is used in forms to allow 'conditional fields' functionality.
A field can be shown or hidden depending on another field's values.
Although available, this functionality does not exist out of the box for custom fields.
This PR adds a new parameter to the form section of the options tab in custom field forms.
It works for any context, subforms and third-party custom fields.

Testing Instructions

Create a custom field of type 'list'. Call it 'List of items'. The field name should read 'list-of-items' once saved.
Add list values (text/value): None/none, Value 1/value1, Value 2/value2, Value 3/value3.

Create a second custom field of type 'Text'. Call it 'Conditional text'.
In the 'options' tab, in the 'form options' section, go to the 'Showon Attribute' parameter.
Enter: list-of-items:value1[OR]list-of-items:value2

Now go edit an article and look for the 'List of items' field in the 'Fields' tab.
The text field 'Conditional text' should only show when the list values of 'List of items' are Value 1 or Value 2.

This also works in subforms. However, fields found in subforms are 'renamed'.
When adding the previously created field 'List of items' to a subform, it shows as field[ID] (where ID is the id of the field), not as 'list-of-items'.
Therefore, when using 'List of items' and 'Conditional text' in a subform, the showon attribute for 'Conditional text' will be:
field36:value1[OR]field36:value2 where 36 is whatever ID the field 'List of items' has in the administrator console.

Actual result BEFORE applying this Pull Request

All fields show on a form (by default, unless some other parameter have been set).

Expected result AFTER applying this Pull Request

Fields are shown conditionally, depending on the values set in other fields.

Unexpected results:
This solution works well for custom fields in forms, when conditional fields are needed.
However, in this solution, nothing prevents field values to show when displayed, even though they could have been hidden in the forms. In some cases, hidden fields should also be hidden in outputs. If anyone has any idea how this could be dealt with, please discuss your solution here. Thank you!

Link to documentations

The syntax is already fully documented at https://docs.joomla.org/Form_field#Common_attributes.
It could also be appropriate to have a specific section where custom field creation is documented.
It needs some additions for the subform cases.

Please select:

  • Documentation link for docs.joomla.org: [<link>]

  • No documentation changes for docs.joomla.org needed

  • Pull Request link for manual.joomla.org:

  • No documentation changes for manual.joomla.org needed

Added the showon attribute entry to the form options
Added label and description keys for the showon attribute parameter
Added the showon attribute to the node creation in onCustomFieldsPrepareDom
@laoneo
Copy link
Member

laoneo commented Oct 23, 2022

A developer would understand that option, but a user not. I'm not sure if we should expose such a technical attribute to the UI.

@obuisard
Copy link
Contributor Author

A developer would understand that option, but a user not. I'm not sure if we should expose such a technical attribute to the UI.

I do agree, I thought about it as well. How can we offer such functionality then? @degobbis plugin works but is limited to a few contexts. What if offered as a functionality that can be turned on/off?

@degobbis
Copy link
Contributor

@obuisard For the "Unexpected results" I think about the implementation in the component the problem in the frontend can not be solved.

As you must have noticed in my plugin, I also edit the content before output to remove the hidden fields by using the events.

For the user fields, even a different workaround is needed.

In my opinion, the best way to solve them is to use a plugin as I do.

@obuisard
Copy link
Contributor Author

@obuisard For the "Unexpected results" I think about the implementation in the component the problem in the frontend can not be solved.

As you must have noticed in my plugin, I also edit the content before output to remove the hidden fields by using the events.

For the user fields, even a different workaround is needed.

In my opinion, the best way to solve them is to use a plugin as I do.

Thank you, Guido.
It may be the way to go indeed. However, I am going to look further to see if the existing plugins we already have could be enough to avoid adding another one.

@HLeithner
Copy link
Member

Not everything have to be a beginner feature, we have so many settings a user can't understand but a integrator can use. This field would be really help full and basically the functionality is in joomla already only the field is missing.

Better wording for key

Co-authored-by: Brian Teeman <brian@teeman.net>
Co-authored-by: Quy <quy@nomonkeybiz.com>
@brianteeman
Copy link
Contributor

Thjis works great and is a much needed feature. It is a bit technical for most users but as long as its documented its fine. Agree 100% with @HLeithner

@brianteeman
Copy link
Contributor

I have tested this item ✅ successfully on c154e73


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39051.

1 similar comment
@alikon
Copy link
Contributor

alikon commented Dec 23, 2022

I have tested this item ✅ successfully on c154e73


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39051.

@crystalenka
Copy link
Member

Is there a way to get the showon to work in subforms without altering the field name?

Use case: fields can be both standalone and in subforms.

@richard67
Copy link
Member

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39051.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Dec 23, 2022
@obuisard
Copy link
Contributor Author

Is there a way to get the showon to work in subforms without altering the field name?

Use case: fields can be both standalone and in subforms.

Unfortunately, that is the way subforms are saved: using field ids and not field names. A change would be B/C for all subforms created to date.

@obuisard
Copy link
Contributor Author

obuisard commented Dec 23, 2022

Thank you Brian @brianteeman and Nicola @alikon for testing this PR!

@degobbis
Copy link
Contributor

Is there now a solution for the output in the frontend?

@carlitorweb
Copy link
Contributor

carlitorweb commented Jan 10, 2023

Documentation created at https://docs.joomla.org/Help4.x:Fields:_Edit

@obuisard
Copy link
Contributor Author

This also works in subforms. However, fields found in subforms are 'renamed'.
When adding the previously created field 'List of items' to a subform, it shows as field[ID] (where ID is the id of the field), not as 'list-of-items'.
Therefore, when using 'List of items' and 'Conditional text' in a subform, the showon attribute for 'Conditional text' will be:
field36:value1[OR]field36:value2 where 36 is whatever ID the field 'List of items' has in the administrator console.

Super Carlos @carlitorweb ! However, we probably should mention the specific case when dealing with showon in subforms, where the field id is used, not the field name.

@carlitorweb
Copy link
Contributor

@obuisard thank you for the correction, I added it.

@obuisard
Copy link
Contributor Author

@obuisard thank you for the correction, I added it.

Thank you so much!

@obuisard
Copy link
Contributor Author

Is there now a solution for the output in the frontend?

Nobody came forward with a solution at this time.

@HLeithner HLeithner merged commit 6554f62 into joomla:4.3-dev Jan 12, 2023
@olivdee
Copy link

olivdee commented Apr 25, 2023

My suspicion is getting stronger and stronger. It must be a Javascript issue.
Since the dom is already loaded when you click on the plus button and the new line is not considered by the observer, the showon function is not implemented.

How to check? I gladly do that if guided

if this is relevant or helpful:

  • in devtools network tab there is no blocked request or 404 status, but there is also no showon.js or showon.min.js loaded (if those are the related/needed files in order for this to work), nor is there a console error of any kind
  • also if I remove the min="1" showon attribute, then there is no more error in console

@olivdee
Copy link

olivdee commented Apr 25, 2023

Just for the record: I just installed from scratch another clean J!4.3, this time on a different server, used the exact above mentioned steps and it is still not working. Afterwards I installed @nikosdion plg_content_fieldsshowon-main.zip and enabled it, still no luck...

@olivdee
Copy link

olivdee commented Apr 26, 2023

FYI: showon.min.js gets loaded when editing the fields, but not when editing an article with those custom fields assigned to it

one question:
Where is the difference between the "Show On" field inside the fields "General" tab vs. the "Showon Attribute" field inside the fields "Options" tab??? BTW, I tried adding "field1:value1" inside both, none worked...

@carlitorweb
Copy link
Contributor

"Show On" field inside the fields "General"

There is not a Show on field under General tab for the Subform custom field. Only the one under Options tab exist

I tried adding "field1:value1" inside both, none worked

field[ID] is only needed when you have the field inside an subform field. Check here

@olivdee
Copy link

olivdee commented Apr 26, 2023

I see, thank you, then "Show On" inside "General" is part of @nikosdion plugin plg_content_fieldsshowon-main.zip - in fact I just disabled the plugin and that field is gone.

Yes, I have that field inside a subform field as explained, that is why I am using it.

In fact I just build that exact thingy without a subform, changed "field1:value1" to "field-list:value1" etc. and it works as expected (without subform and without the aforementioned plugin. Rebuilding it with a subform, using "field1:value1" again, with or without the plugin it does not work... There must be an issue/bug or something very stupid I am missing out here.

@olivdee
Copy link

olivdee commented Apr 26, 2023

I am giving up on this... Tried every combination, with and without the plugin, with the plugins own "Show on" field under General tab and with the native one under Options tab, also with both filled out, made tests with either "field1" (ID) or "field-list" (name), created a fields group as well, added only the list-field to be shown in "Subform Only" and added the two conditional fields to be shown in "Category: All" (here funny thing is, when the plugin is enabled, they do not show up separately with the list-field, when the plugin is disabled they show up) - nothing works...

Maybe somebody can please either confirm that there is actually a bug/error or provide a step-by-step guide on how he did succeed in using this:

  1. Field List with 2 different list values
  2. Conditional Field 1 shown only on Field List with value1 and the respective "Showon Attribute" inside "Options" tab
  3. Conditional Field 2 shown only on Field List with value2 and the respective "Showon Attribute" inside "Options" tab
  4. Subform Field (Content Repeater) with the repeatable Field List inside
  5. Test Article with the Subform Field, when either of the 2 Field List values are selected, the respective Conditional Field should appear:
    image

I would be very very thankful for this!
Thanks in advance!

@degobbis
Copy link
Contributor

@olivdee just to make sure we are using this correctly:
the 1 in field1 is not the field number 1 in the sequence, but there should be the fieldID.

So the same number as if you were adding the field in the article to the content.

@olivdee
Copy link

olivdee commented Apr 26, 2023

yes:
image
image
image

this is what you mean and what I understood, is that correct?

@degobbis
Copy link
Contributor

yes, so we can give up now :-(

@olivdee
Copy link

olivdee commented Apr 26, 2023

does that mean:

  1. nobody fully tested this till the end?
  2. somebody did once upon a time, it was working, but it is broken now?
  3. it is not broken, it actually works, but there is some spectacular miss on my side, or I am too stupid to get it done?

@richard67
Copy link
Member

Shouldn't it be the name which is used in showon? I.e. "field-list:1"?

@olivdee
Copy link

olivdee commented Apr 26, 2023

as far as I understood, only if not used inside a subform, within a subform it is "field[ID]:1"

@richard67
Copy link
Member

as far as I understood, only if not used inside a subform, within a subform it is "field[ID]:1"

@olivdee Yes, sorry, now as having read more in detail I see. According to the docs you are right and it should be "field1:1" (if ID is 1 as shown in your last screenshot. In one of the previous screenshots it was 7).

@olivdee
Copy link

olivdee commented Apr 26, 2023 via email

@carlitorweb
Copy link
Contributor

@olivdee I in fact right now working in a site and using a conditional custom field inside a subform, and is workig as expected

Captura

@carlitorweb
Copy link
Contributor

carlitorweb commented Apr 26, 2023

@olivdee can you set the "Conditional Text" as Only Use In Subform and check again. You need have both fields with Only Use In Subform as Yes

@olivdee
Copy link

olivdee commented Apr 26, 2023

thank you for confirming that it is working! I was using both fields only use in subform, actually like this:
image

@olivdee
Copy link

olivdee commented Apr 26, 2023

and "Content Repeater" contains only "Field List"... so stupid... it HAS to contain all 3, isn't it?

@richard67
Copy link
Member

and "Content Repeater" contains only "Field List"... so stupid... it HAS to contain all 3, isn't it?

Yes.

@olivdee
Copy link

olivdee commented Apr 26, 2023

sorry, it was a long day...
I guess I need to look into overriding the admin template in order to hide the not shown/hidden fields, and make the rest full width or shown vertically instead of horizontally or something like that - any hints?

@FilMar61
Copy link

FilMar61 commented May 5, 2023

I don't know if I can ask it here but I have a problem with a required field.
When I set a field required but I use also Show on, I can't save the article when the required field is not visible.
It probably sounds logic as the field is required but I had hoped that if the 'show on' hides the field, it also 'hide' it for the validation.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39051.

@obuisard
Copy link
Contributor Author

obuisard commented May 5, 2023

@FilMar61 do you mind opening an issue in the issue tracker? That way we can have people look at it, confirm this is the case, and have it processed. Thank you!

@FilMar61
Copy link

FilMar61 commented May 5, 2023

Oeps, sorry that I posted this on the wrong place.
I opened a new issue (I hope the right way, this time ;-)

@obuisard
Copy link
Contributor Author

obuisard commented May 5, 2023

Oops, sorry that I posted this on the wrong place. I opened a new issue (I hope the right way, this time ;-)

No problem @FilMar61! It will have more visibility than the merged PR :-)

@HDInfautre
Copy link

Hello
@obuisard
Could this lead to the 4.3 series using Showon on the front-end :-) ?
I'm putting it here, as I haven't seen any specific discussion or request on this subject, but I'm not very familiar with github. What's for sure is that several of us have mentioned the importance of not restricting it to the backoffice.

@obuisard
Copy link
Contributor Author

Hello @obuisard Could this lead to the 4.3 series using Showon on the front-end :-) ? I'm putting it here, as I haven't seen any specific discussion or request on this subject, but I'm not very familiar with github. What's for sure is that several of us have mentioned the importance of not restricting it to the backoffice.

Hello @HDInfautre, I apologize for the late answer.
The best would be to open an issue in the issue tracker at https://issues.joomla.org (login with your GitHub account), like that people can comment on it and developers will be aware of the necessity to improve support for showon in the public side of a site.

@HDInfautre
Copy link

Hello

Hello @obuisard Could this lead to the 4.3 series using Showon on the front-end :-) ? I'm putting it here, as I haven't seen any specific discussion or request on this subject, but I'm not very familiar with github. What's for sure is that several of us have mentioned the importance of not restricting it to the backoffice.

Hello @HDInfautre, I apologize for the late answer. The best would be to open an issue in the issue tracker at https://issues.joomla.org (login with your GitHub account), like that people can comment on it and developers will be aware of the necessity to improve support for showon in the public side of a site.

Ok, for those interested on this thread, you can vote!? ;-)
https://issues.joomla.org/tracker/joomla-cms/40762

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet