You are here

public function PhotoswipeFieldFormatter::settingsForm in PhotoSwipe 8.2

Same name and namespace in other branches
  1. 8 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 43

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('Entity title'),
    'custom' => $this
      ->t('Custom (with tokens)'),
  ];
  $element = $this
    ->addEntityReferenceSettings($element);

  // Add the other parent entity fields as options.
  if (isset($form['#fields'])) {
    foreach ($form['#fields'] as $parent_field) {
      if ($parent_field != $this->fieldDefinition
        ->getName()) {
        $caption_options[$parent_field] = $parent_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.'),
  ];
  $element['photoswipe_caption_custom'] = [
    '#title' => $this
      ->t('Custom caption'),
    '#type' => 'textarea',
    '#default_value' => $this
      ->getSetting('photoswipe_caption_custom'),
    '#states' => [
      'visible' => [
        ':input[name$="[settings][photoswipe_caption]"]' => [
          'value' => 'custom',
        ],
      ],
    ],
  ];
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {
    $element['photoswipe_token_caption'] = [
      '#type' => 'fieldset',
      '#title' => t('Replacement patterns'),
      '#theme' => 'token_tree_link',
      // A KLUDGE! Need to figure out current entity type.
      // in both entity display and views contexts.
      '#token_types' => [
        'file',
        'node',
      ],
      '#states' => [
        'visible' => [
          ':input[name$="[settings][photoswipe_caption]"]' => [
            'value' => 'custom',
          ],
        ],
      ],
    ];
  }
  else {
    $element['photoswipe_token_caption'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Replacement patterns'),
      '#description' => '<strong class="error">' . $this
        ->t('For token support the <a href="@token_url">token module</a> must be installed.', [
        '@token_url' => 'http://drupal.org/project/token',
      ]) . '</strong>',
      '#states' => [
        'visible' => [
          ':input[name$="[settings][photoswipe_caption]"]' => [
            'value' => 'custom',
          ],
        ],
      ],
    ];
  }

  // 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);
}