You are here

public function PhotoswipeFieldFormatter::settingsForm in PhotoSwipe 8

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

src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php, line 39

Class

PhotoswipeFieldFormatter
Plugin implementation of the 'photoswipe_field_formatter' formatter.

Namespace

Drupal\photoswipe\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $image_styles = image_style_options(FALSE);
  $image_styles_hide = $image_styles;
  $image_styles_hide['hide'] = $this
    ->t('Hide (do not display image)');
  $element['photoswipe_node_style_first'] = [
    '#title' => $this
      ->t('Node image style for first image'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('photoswipe_node_style_first'),
    '#empty_option' => $this
      ->t('No special style.'),
    '#options' => $image_styles_hide,
    '#description' => $this
      ->t('Image style to use in the content for the first image.'),
  ];
  $element['photoswipe_node_style'] = [
    '#title' => $this
      ->t('Node image style'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('photoswipe_node_style'),
    '#empty_option' => $this
      ->t('None (original image)'),
    '#options' => $image_styles_hide,
    '#description' => $this
      ->t('Image style to use in the node.'),
  ];
  $element['photoswipe_image_style'] = [
    '#title' => $this
      ->t('Photoswipe image style'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('photoswipe_image_style'),
    '#empty_option' => $this
      ->t('None (original image)'),
    '#options' => $image_styles,
    '#description' => $this
      ->t('Image style to use in the Photoswipe.'),
  ];

  // Set our caption options.
  $caption_options = [
    'title' => $this
      ->t('Image Title Tag'),
    'alt' => $this
      ->t('Image Alt Tag'),
    'node_title' => $this
      ->t('Node Title'),
  ];

  // Add the other node fields as options.
  if (!empty($form['#fields'])) {
    foreach ($form['#fields'] as $node_field) {
      if ($node_field != $this->fieldDefinition
        ->getName()) {
        $caption_options[$node_field] = $node_field;
      }
    }
  }
  $element['photoswipe_caption'] = [
    '#title' => $this
      ->t('Photoswipe image caption'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('photoswipe_caption'),
    '#options' => $caption_options,
    '#description' => $this
      ->t('Field that should be used for the caption.'),
  ];

  // Add the current view mode so we can control view mode for node fields.
  $element['photoswipe_view_mode'] = [
    '#type' => 'hidden',
    '#value' => $this->viewMode,
  ];
  return $element + parent::settingsForm($form, $form_state);
}