If we add a “taxonomy” reference field, we have a field in the form that lists the terms. If I dump $form
, I get for this field:
field_zone" => array:7 [▼ "#type" => "container" "#parents" => array:1 [▶] "#attributes" => array:1 [▶] "widget" => array:15 [▼ "#title" => "Zone" "#description" => "" "#field_parents" => [] "#required" => false "#delta" => 0 "#weight" => 0 "#element_validate" => array:1 [▶] "#key_column" => "target_id" "#type" => "checkboxes" "#default_value" => [] "#options" => array:12 [▼ 433 => DrupalCoreFieldFieldFilteredMarkup {#6483 ▼ #string: "Asie-Pacifique" } 434 => DrupalCoreFieldFieldFilteredMarkup {#6580 ▼ #string: "-Asie de l’Est" } 10 => DrupalCoreFieldFieldFilteredMarkup {#6703 ▼ #string: "Afrique" } 11 => DrupalCoreFieldFieldFilteredMarkup {#6704 ▼ #string: "-Afrique de l’Ouest" } 12 => DrupalCoreFieldFieldFilteredMarkup {#6705 ▼ #string: "-Afrique Centrale" } 13 => DrupalCoreFieldFieldFilteredMarkup {#6706 ▶} 14 => DrupalCoreFieldFieldFilteredMarkup {#6707 ▶} 15 => DrupalCoreFieldFieldFilteredMarkup {#6708 ▶} 16 => DrupalCoreFieldFieldFilteredMarkup {#6709 ▶} 17 => DrupalCoreFieldFieldFilteredMarkup {#6710 ▶} 18 => DrupalCoreFieldFieldFilteredMarkup {#6711 ▶} 19 => DrupalCoreFieldFieldFilteredMarkup {#6712 ▶} ] "#after_build" => array:1 [▶] "#field_name" => "field_zone_expertise" "#parents" => array:1 [▶] "#tree" => true ]
We can therefore see that the field is of the container type and that each taxonomy term is an option. In front, it appears as checkboxes.
Is it possible to use #states
with these options? I have tried many things without success.
I know how to do it with a single field but not with this multiple field. I would like when we check on the parent term that the child terms are checked.
I tried to do it with custom JS but I encounter problems when I add the “checked” attribute :
$('#edit-field-zone-11').attr('checked', true);
It works but not always well because Drupal doesn’t use this attribute.