I have a Paragraph with two List (text) fields. One is using the Select widget and the other using Check boxes. There are 14 checkbox options—I'm trying to show/hide certain checkboxes based on the value of the select.

In my hook_field_widget_WIDGET_TYPE_form_alter() function, I can access the field containing the checkboxes but I can't figure out how to access the checkbox elements themselves in such a way so that I can alter their ['#states'].

function test_field_widget_paragraphs_form_alter(&$element, &$form_state, $context) {
  if ($element['#paragraph_type'] == 'my_test') {
    $select = $element['subform']['field_select']['widget'];
    $checks = $element['subform']['field_checkboxes']['widget'];
    $element['subform']['field_checkboxes']['widget']['#title'] = "This works."

Inspecting the $checks variable, I can see the individual ['#options'] as well as default_values but I can't sort out the syntax to use so that I can alter their states.

// Null and null:

// Returns a `Drupal\Core\Field\FieldFilteredMarkup` object, not the form element.

Am I barking up the wrong tree?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.