You are here

public function FieldFormatterWithInlineSettings::settingsForm in (Entity Reference) Field Formatters 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/FieldFormatterWithInlineSettings.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldFormatterWithInlineSettings::settingsForm()
  2. 3.x src/Plugin/Field/FieldFormatter/FieldFormatterWithInlineSettings.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldFormatterWithInlineSettings::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 FieldFormatterBase::settingsForm

File

src/Plugin/Field/FieldFormatter/FieldFormatterWithInlineSettings.php, line 218

Class

FieldFormatterWithInlineSettings
Plugin implementation of the 'link' formatter.

Namespace

Drupal\field_formatter\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);

  // Name of the field this formatter is currently displaying.
  $target_entity_type_id = $this->fieldDefinition
    ->getSetting('target_type');
  $field_storage_definitions = $this->entityFieldManager
    ->getFieldStorageDefinitions($target_entity_type_id);
  $formatted_field_name = $this
    ->getSettingFromFormState($form_state, 'field_name');

  // In case there is no definition for specified field.
  if (isset($field_storage_definitions[$formatted_field_name])) {
    $field_storage = $field_storage_definitions[$formatted_field_name];
    $formatter_options = $this
      ->getAvailableFormatterOptions($field_storage);
  }
  $form['#prefix'] = '<div id="field-formatter-ajax">';
  $form['#suffix'] = '</div>';
  $form['field_name'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field name'),
    '#default_value' => $formatted_field_name,
    '#options' => $this
      ->getAvailableFieldNames(),
    // Note: We cannot use ::foo syntax, because the form is the entity form
    // display.
    '#ajax' => [
      'callback' => [
        get_class(),
        'onFieldNameChange',
      ],
      'wrapper' => 'field-formatter-ajax',
      'method' => 'replace',
    ],
    '#submit' => [
      [
        get_class(),
        'rebuildSubmit',
      ],
    ],
    '#executes_submit_callback' => TRUE,
  ];
  $form['label'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Label'),
    '#options' => [
      'above' => $this
        ->t('Above'),
      'inline' => $this
        ->t('Inline'),
      'hidden' => '- ' . $this
        ->t('Hidden') . ' -',
      'visually_hidden' => '- ' . $this
        ->t('Visually Hidden') . ' -',
    ],
    '#default_value' => $this
      ->getSettingFromFormState($form_state, 'label'),
  ];
  if ($formatted_field_name && !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',
      ],
      '#submit' => [
        [
          get_class(),
          'rebuildSubmit',
        ],
      ],
      '#executes_submit_callback' => TRUE,
    ];
    $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;
}