You are here

public function FieldWrapperBase::settingsForm in (Entity Reference) Field Formatters 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/FieldWrapperBase.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldWrapperBase::settingsForm()
  2. 8 src/Plugin/Field/FieldFormatter/FieldWrapperBase.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldWrapperBase::settingsForm()

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. 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 elements for the formatter settings.

Overrides FormatterBase::settingsForm

File

src/Plugin/Field/FieldFormatter/FieldWrapperBase.php, line 181

Class

FieldWrapperBase
Wraps an existing field.

Namespace

Drupal\field_formatter\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $field_storage = $this->fieldDefinition
    ->getFieldStorageDefinition();
  $formatter_options = $this
    ->getAvailableFormatterOptions($field_storage);
  if (!empty($formatter_options)) {
    $formatter_type = $this
      ->getSettingFromFormState($form_state, 'type');
    $settings = $this
      ->getSettingFromFormState($form_state, 'settings');
    if (!isset($formatter_options[$formatter_type])) {
      $formatter_type = key($formatter_options);
      $settings = [];
    }
    $form['type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Formatter'),
      '#options' => $formatter_options,
      '#default_value' => $formatter_type,
      // Note: We cannot use ::foo syntax, because the form is the entity form
      // display.
      '#ajax' => [
        'callback' => [
          get_class(),
          'onFormatterTypeChange',
        ],
        'wrapper' => 'field-formatter-settings-ajax',
        'method' => 'replace',
      ],
    ];
    $options = [
      'field_definition' => $this
        ->getFieldDefinition($field_storage),
      'configuration' => [
        'type' => $formatter_type,
        'settings' => $settings,
        'label' => '',
        'weight' => 0,
      ],
      'view_mode' => '_custom',
    ];

    // Get the formatter settings form.
    $settings_form = [
      '#value' => [],
    ];
    if ($formatter = $this->formatterPluginManager
      ->getInstance($options)) {
      $settings_form = $formatter
        ->settingsForm([], $form_state);
    }
    $form['settings'] = $settings_form;
    $form['settings']['#prefix'] = '<div id="field-formatter-settings-ajax">';
    $form['settings']['#suffix'] = '</div>';
  }
  return $form;
}