You are here

public function DynamicEntityReferenceEntityFormatter::settingsForm in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/DynamicEntityReferenceEntityFormatter.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldFormatter\DynamicEntityReferenceEntityFormatter::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 EntityReferenceEntityFormatter::settingsForm

File

src/Plugin/Field/FieldFormatter/DynamicEntityReferenceEntityFormatter.php, line 76

Class

DynamicEntityReferenceEntityFormatter
Plugin implementation of the 'rendered entity' formatter.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $labels = \Drupal::service('entity_type.repository')
    ->getEntityTypeLabels(TRUE);
  $options = $labels[(string) t('Content', [], [
    'context' => 'Entity type group',
  ])];
  $entity_type_ids = DynamicEntityReferenceItem::getTargetTypes($this
    ->getFieldSettings());
  $elements['view_mode'] = [];
  foreach ($entity_type_ids as $entity_type_id) {
    $elements[$entity_type_id] = [
      '#type' => 'container',
    ];
    $elements[$entity_type_id]['view_mode'] = [
      '#type' => 'select',
      '#options' => $this->entityDisplayRepository
        ->getViewModeOptions($entity_type_id),
      '#title' => t('View mode for %entity', [
        '%entity' => $options[$entity_type_id],
      ]),
      '#default_value' => $this
        ->getSetting($entity_type_id)['view_mode'],
      '#required' => TRUE,
    ];
  }
  return $elements;
}