function BooleanFormatter::settingsForm

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\BooleanFormatter::settingsForm()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\BooleanFormatter::settingsForm()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\BooleanFormatter::settingsForm()

Overrides FormatterBase::settingsForm

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php, line 63

Class

BooleanFormatter
Plugin implementation of the 'boolean' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::settingsForm($form, $form_state);
    $formats = [];
    foreach ($this->getOutputFormats() as $format_name => $format) {
        if (is_array($format)) {
            if ($format_name == 'default') {
                $formats[$format_name] = $this->t('Field settings (@on_label / @off_label)', [
                    '@on_label' => $format[0],
                    '@off_label' => $format[1],
                ]);
            }
            else {
                $formats[$format_name] = $this->t('@on_label / @off_label', [
                    '@on_label' => $format[0],
                    '@off_label' => $format[1],
                ]);
            }
        }
        else {
            $formats[$format_name] = $format;
        }
    }
    $field_name = $this->fieldDefinition
        ->getName();
    $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_name . '][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_name . '][settings_edit_form][settings][format]"]' => [
                    'value' => 'custom',
                ],
            ],
        ],
    ];
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.