0

I have a boolean check box that I want to control the visibility of an entity-reference field below it. By default, this check box is unchecked, which means the entity-reference field should not be shown.

I've tried a handful of things in my .theme file, but I can't seem to solve the problem.

use Drupal\Core\Form\FormStateInterface;

function mytheme_form_node_news_form_alter(&$form, FormStateInterface 
  &$form_state, $form_id) {
    $form['field_news_top_story_image']['#states'] = [ 
    'visible' => [ 
        ':input[name="field_news_topstory"]' => ['checked' => TRUE] 
    ]
];

}

node_news_form (and eventually node_news_edit_form once I figure this out) appears to be the form I want to affect. field_news_topstory is my checkbox and field_news_top_story_image is the entity-reference field I want to hide* until field_news_topstory is checked. *I've set this field to disabled in Manage Form Display.

Am I close? :)

EDIT: Had my code backwards, but still not working.

5
  • try with $form['field_news_top_story_image']['#states'] = [ 'visible' => [ ':input[name="field_news_topstory"]' => ['checked' => TRUE] ] ];
    – berramou
    Feb 5, 2020 at 20:59
  • @berramou - How does the initial state of field_news_top_story_image get set to hidden (or disabled)? I was thinking I'd set it to disabled in Manage Form Display, but i'm not sure that's the right move. Thanks!
    – pburgh
    Feb 5, 2020 at 21:10
  • field_news_top_story_image will be initially hidden because it has state and the rule in the state is to make it visible only if the field_news_topstory input checked, so it remain hidden until the checkbox input checked
    – berramou
    Feb 5, 2020 at 21:16
  • @berramou Thanks for clarifying. I'm still not seeing what I need. I've posted my updated code above. Perhaps I have the wrong form id? If I view the source of the two forms I'm trying to affect I see <input data-drupal-selector="edit-node-news-form" type="hidden" name="form_id" value="node_news_form" /> and <input data-drupal-selector="edit-node-news-edit-form" type="hidden" name="form_id" value="node_news_edit_form" />
    – pburgh
    Feb 5, 2020 at 21:22
  • I added a list field to my content type and changed ':input[name="field_news_topstory"]' => ['checked' => TRUE] to 'select[name="field_news_test_field"]' => ['value' => 'Yes'] and now my field_news_top_story_image visibility can be toggled. Can I not use a boolean as shown in my original post?
    – pburgh
    Feb 6, 2020 at 15:35

1 Answer 1

0

After a lot of banging on this, I found the right combo. We don't have a custom admin theme, so I ended up moving this code in to a custom module.

The important part I was missing was [value] in ':input[name="field_news_topstory[value]"]' => ['checked' => TRUE]

use Drupal\Core\Form\FormStateInterface;

function mymodule_form_node_news_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    $form['field_news_top_story_image']['#states'] = [
       'visible' => [
         ':input[name="field_news_topstory[value]"]' => ['checked' => TRUE]
       ]
   ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.