Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi
I'm trying to add States in some paragraph fields, for being able to make visible some fields depending on the value of others.
Which is the best way to achive that? i've being trying with hook_entity_form_display_alter but with no luck.
Thanks in advance.
Regards.
Comments
Comment #2
dajjenI'm also interested in this, have tried several hooks and also tried to override paragraphwidget but no luck.
Comment #3
jaimecalvo CreditAttribution: jaimecalvo commentedI found this one:
https://www.drupal.org/node/2868155
then you can use hook_form_paragraphs_subform_alter().
Comment #4
slydevil CreditAttribution: slydevil commentedThis module will allow you to do that: https://www.drupal.org/project/field_states_ui
You'll need the patch I just created as well: https://www.drupal.org/project/field_states_ui/issues/2857300
Comment #5
srijeeta CreditAttribution: srijeeta commentedField States UI worked perfectly with patch.
But for this modules returns a lot of warnings and notices.
Example:
Comment #6
afeijoHere is a simple working example:
Have fun ;)
Comment #7
GAMe CreditAttribution: GAMe commentedHi afeijo,
Thanks for the code example. Im struggling with the same and trying to decipher what I need from this. I currently have a paragraph type with 2 fields in which I need one field to be visible if a value from the select field is set. Fields are:
Paragraph type: pg_our_food_section
Select field with values: field_food_taste_type (value = 3)
Field to show if above value is set: field_food_flavour_type
I should also add im adding this code in the .theme file rather than a custom module.
Any help or direction on getting this working would be amazing.
Thanks
:-)
Comment #8
afeijohi GAMe,
Did you follow my code? Just copy it, and change the $field_name line to your field that will be controlled
and for the field with the options, change this line to your proper field name:
$variation_field = $element['subform']['field_options'];
in my example, it is named field_options
Comment #9
dbgilbert CreditAttribution: dbgilbert at SEI Global Services Inc. commentedHi afeijo,
Your example looks like exactly what I'm looking for, but inside the 'subform' object I do not see any fields for the paragraph's I'm trying to apply states to. I see it has '#attributes' and '#access' but nothing else. This is for a paragraph field that can have multiple paragraphs of different types. Each paragraph in the field triggers the widget form alter function and I can differentiate them by the '#paragraph_type' but 'subform' lists no fields in any of the many widgets.
Comment #10
interactivestrategies.com CreditAttribution: interactivestrategies.com commentedRunning in to the exact same issue as @dbgilbert in #9 -- "subform" has no fields in it, only #attributes and #access. I was pointed to
hook_field_widget(_multivalue)_WIDGET_TYPE_form_alter()
after reviewing #2868155, but something else seems to be resetting $element somehow. FWIW I'm on core 8.7.5 and Paragraphs 1.8.Comment #11
natemow CreditAttribution: natemow as a volunteer commentedFigured out the empty "subform" issue when using these hooks -- the paragraph widget settings need to be set with Edit Mode "Open"; "Closed" and "Preview" will return empty subform arrays, and just not fire at all from the AJAX edit callback. Seems to behave the same using both the Classic and Experimental widgets. Not sure I love the UX of having to leave stuff Open by default, but can work around with a bit of JS.
Comment #12
huberbuber CreditAttribution: huberbuber commentedI am also having issues with this, I am trying to change the state of Field A depending in Field B inside the same paragraph. I have used the bellow code, but do not get any change.
Comment #13
Poindexterous CreditAttribution: Poindexterous commentedI'm also running into some trouble with this and I've been grappling with it for a few days.
I have a paragraph with a checkbox called "field_automatic" and a select list for taxonomy called "field_blog_topic". I'd like to set this up so when "field_automatic" is checked, the "field_blog_topic" is invisible. I've modified afeijo's code accordingly but it doesn't seem to be working, though I'm not sure what I'm going wrong here:
Looking at the object in Kint, I am seeing the #states array added to my blog_topic field, but the visibility doesn't toggle on or off when I check the "field_automatic" field. Am I overlooking something? My paragraph is using classic mode and is set to "open"
Note: I'm trying to alter the paragraph forms inside a block form and also the layout builder.
Thanks!
Comment #14
BerdirThe key is an arbitrary jquery selector. If you struggle with the parents, which is indeed tricky, you could try to just add a custom class to the form element and use that to select it.
Comment #15
diegoluisr CreditAttribution: diegoluisr commentedThe problem is related to paragraph rendering, I have the same problem, in addition, I have modified the title and description but those element doesn't see applied with the custom information.
Above example must work, but it hasn't
Comment #16
BerdirThe name is definitely not field_conditional on a paragraphs field, those field names are deeply nested.
Comment #17
diegoluisr CreditAttribution: diegoluisr commentedCould explain yourself?
Find below the keys when I call the array_keys function over $element['subform'], You could see that both fields (field_conditioned, field_conditional) are in the proper position.
And the next code shows the array position for "#states", but this is never rendered, This is the same situation if I want to modify the title or the description. Paragraphs seem as is not getting those changes.
Comment #18
BerdirI'm not sure what you mean with the correct position, but again, I'm pretty certain that this is not the complete name of the paragraph field. #states is about client-side javascript, forget the render array for a moment, and just look at the HTML and the actual name attribute of the input element you want to work with.
Comment #19
diegoluisr CreditAttribution: diegoluisr commented@Berdir, Thanks a lot, your comments gave me the clues to find the issue (my mistake), In my environment `$variation_field['widget']['#parents']` doesn't exist, I don't know why, but, I solved if modifying the function that @afeijo suggested.