Steps to reproduce:

  1. Add a field of type "Boolean" to any entity type and bundle.
  2. Edit the display settings of that field.
  3. The two text fields for custom labels stay visible no matter what you select for "Output format".

This is caused by the following in \Drupal\Core\Field\Plugin\Field\FieldFormatter\BooleanFormatter::settingsForm(), which (I'm pretty sure) incorrectly hard-codes field_boolean as the field name:

    $form['format'] = [
      '#type' => 'select',
      '#title' => $this->t('Output format'),
      '#default_value' => $this->getSetting('format'),
      '#options' => $formats,
    ];
    $form['format_custom_true'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Custom output for TRUE'),
      '#default_value' => $this->getSetting('format_custom_true'),
      '#states' => [
        'visible' => [
          'select[name="fields[field_boolean][settings_edit_form][settings][format]"]' => ['value' => 'custom'],
        ],
      ],
    ];
    $form['format_custom_false'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Custom output for FALSE'),
      '#default_value' => $this->getSetting('format_custom_false'),
      '#states' => [
        'visible' => [
          'select[name="fields[field_boolean][settings_edit_form][settings][format]"]' => ['value' => 'custom'],
        ],
      ],
    ];

Comments

drunken monkey created an issue. See original summary.

drunken monkey’s picture

Title: Javascript states not working for boolean entity fields » Javascript states not working for boolean fields
Component: views.module » field system
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new2.35 KB

After a bit of research, it seems the bug is definitely in the boolean formatter. Updating the IS.

The fix is pretty simple, once you've found out how to get the field name at that point in the code. Please see the attached patch.

Also, the only other formatter (at least in that folder) with #states seems to also have them wrong. In \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::settingsForm():

    $elements['custom_date_format']['#states']['visible'][] = array(
      ':input[name="options[settings][date_format]"]' => array('value' => 'custom'),
    );

However, I couldn't get this formatter to appear in the Fields UI, and it seems to be written to work with Views (the #states is correct for that), so I'm not 100% positive we should fix it there, too. However, I think so, since Views already has a work-around in place to correct the assumed #states selectors, and because it's at least pretty confusing for others who want to write field formatters and look for examples in Core.

swentel’s picture

Status: Needs review » Reviewed & tested by the community

Ha, funny hardcoded bug :)

Re: timestamp item, it's not exposed as a field type for field ui (although we could do that one day, but not in this issue)

Note for committers: views overrides the states in \Drupal\views\Plugin\views\field\Date.

Test it manually, works great.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Nice! I wish we had front end testing... Committed 7c90373 and pushed to 8.0.x and 8.1.x. Thanks!

  • alexpott committed 96dc192 on 8.1.x
    Issue #2650994 by drunken monkey: Javascript states not working for...

  • alexpott committed 7c90373 on
    Issue #2650994 by drunken monkey: Javascript states not working for...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.