You are here

public function FileImageFormatter::settingsForm in File Entity (fieldable files) 8.2

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 ImageFormatter::settingsForm

File

src/Plugin/Field/FieldFormatter/FileImageFormatter.php, line 164

Class

FileImageFormatter
Implementation of the 'image' formatter for the file_entity files.

Namespace

Drupal\file_entity\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $element = parent::settingsForm($form, $form_state);
  unset($element['image_link']);
  $available_fields = $this->entityFieldManager
    ->getFieldDefinitions($form['#entity_type'], $form['#bundle']);
  $options = [];
  foreach ($available_fields as $field_name => $field_definition) {
    if ($field_definition instanceof FieldConfigInterface && $field_definition
      ->getType() == 'string') {
      $options[$field_name] = $field_definition
        ->label();
    }
  }
  $element['title'] = [
    '#title' => $this
      ->t('Image title field'),
    '#description' => $this
      ->t('The field that is used as source for the image title attribute.'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $this
      ->getSetting('title'),
    '#empty_option' => $this
      ->t('No title attribute'),
    '#empty_value' => '_none',
  ];
  $element['alt'] = [
    '#title' => $this
      ->t('Image alt field'),
    '#description' => $this
      ->t('The field that is used as source for the image alt attribute.'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $this
      ->getSetting('alt'),
    '#empty_option' => $this
      ->t('No alt attribute'),
    '#empty_value' => '_none',
  ];
  return $element;
}