You are here

public function ReadonlyFieldWidget::settingsForm in Read-only Field Widget 8

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form definition for the widget settings.

Overrides WidgetBase::settingsForm

File

src/Plugin/Field/FieldWidget/ReadonlyFieldWidget.php, line 129

Class

ReadonlyFieldWidget
Plugin implementation of the 'readonly_field_widget' widget.

Namespace

Drupal\readonly_field_widget\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $field_type_formatters = $this->fieldFormatterManager
    ->getOptions($this->fieldDefinition
    ->getType());
  $field_type_definitions = $this->fieldFormatterManager
    ->getDefinitions();
  $formatters = [];
  foreach ($field_type_formatters as $formatter_type => $formatter_label) {
    if (!empty($field_type_definitions[$formatter_type]) && $field_type_definitions[$formatter_type]['class']::isApplicable($this->fieldDefinition)) {
      $formatters[$formatter_type] = $formatter_label;
    }
  }
  $field_name = $this->fieldDefinition
    ->getName();
  $element = [
    'label' => [
      '#title' => $this
        ->t('Label'),
      '#type' => 'select',
      '#options' => $this
        ->labelOptions(),
      '#default_value' => $this
        ->getSetting('label'),
    ],
    'formatter_type' => [
      '#title' => $this
        ->t('Format'),
      '#type' => 'select',
      '#options' => $formatters,
      '#default_value' => $this
        ->getSetting('formatter_type'),
    ],
    'show_description' => [
      '#title' => $this
        ->t('Show Description'),
      '#description' => $this
        ->t('Show the configured description under widget.'),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('show_description'),
    ],
  ];
  foreach (array_keys($formatters) as $formatter_plugin_id) {
    $formatter_plugin = $this
      ->getFormatterInstance($formatter_plugin_id);
    $settings_form = $formatter_plugin
      ->settingsForm($form, $form_state);
    if (!empty($settings_form)) {
      $element['formatter_settings'][$formatter_plugin_id] = [
        '#type' => 'fieldset',
        '#title' => $formatters[$formatter_plugin_id] . ' ' . $this
          ->t('Settings'),
        '#states' => [
          'visible' => [
            ':input[name="fields[' . $field_name . '][settings_edit_form][settings][formatter_type]"]' => [
              'value' => $formatter_plugin_id,
            ],
          ],
        ],
      ] + $settings_form;
    }
  }
  return $element;
}