You are here

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

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

Class

FieldFormatterFromViewDisplay
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);
  $options = [];
  $er_target_entity_type = $this->fieldDefinition
    ->getSetting('target_type');
  $er_handler_settings = $this->fieldDefinition
    ->getSetting('handler_settings');
  if (!empty($er_handler_settings['target_bundles'])) {
    $er_target_bundles = $er_handler_settings['target_bundles'];
    foreach ($er_target_bundles as $er_target_bundle => $er_target_bundle_val) {
      $optionsByBundle = \Drupal::service('entity_display.repository')
        ->getViewModeOptionsByBundle($er_target_entity_type, $er_target_bundle);

      // Add option by key to prevent duplicates:
      foreach ($optionsByBundle as $key => $option) {
        $options[$key] = $option;
      }
    }
  }
  else {
    $options = \Drupal::service('entity_display.repository')
      ->getViewModeOptions($er_target_entity_type);
  }

  // Sort options in alphabetcial order:
  asort($options);
  $form['view_mode'] = [
    '#title' => $this
      ->t('View mode'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $this
      ->getSetting('view_mode'),
    '#empty_option' => 'Default',
    '#empty_value' => 'default',
  ];
  $form['field_name'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field name'),
    '#default_value' => $this
      ->getSetting('field_name'),
    '#options' => $this
      ->getAvailableFieldNames(),
  ];
  return $form;
}