You are here

public function ResponsiveImageFormatter::settingsForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php \Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter::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

core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php, line 128

Class

ResponsiveImageFormatter
Plugin for responsive image formatter.

Namespace

Drupal\responsive_image\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $responsive_image_options = [];
  $responsive_image_styles = $this->responsiveImageStyleStorage
    ->loadMultiple();
  if ($responsive_image_styles && !empty($responsive_image_styles)) {
    foreach ($responsive_image_styles as $machine_name => $responsive_image_style) {
      if ($responsive_image_style
        ->hasImageStyleMappings()) {
        $responsive_image_options[$machine_name] = $responsive_image_style
          ->label();
      }
    }
  }
  $elements['responsive_image_style'] = [
    '#title' => t('Responsive image style'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('responsive_image_style') ?: NULL,
    '#required' => TRUE,
    '#options' => $responsive_image_options,
    '#description' => [
      '#markup' => $this->linkGenerator
        ->generate($this
        ->t('Configure Responsive Image Styles'), new Url('entity.responsive_image_style.collection')),
      '#access' => $this->currentUser
        ->hasPermission('administer responsive image styles'),
    ],
  ];
  $link_types = [
    'content' => t('Content'),
    'file' => t('File'),
  ];
  $elements['image_link'] = [
    '#title' => t('Link image to'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('image_link'),
    '#empty_option' => t('Nothing'),
    '#options' => $link_types,
  ];
  return $elements;
}