You are here

protected function FlexsliderImageFormatterTrait::captionSettings in Flex Slider 8.2

Returns the form element for caption settings.

Parameters

\Drupal\Core\Field\FormatterBase $formatter: The formatter having this trait.

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The image field definition.

Return value

array The caption settings render array.

2 calls to FlexsliderImageFormatterTrait::captionSettings()
FlexsliderFormatter::settingsForm in flexslider_fields/src/Plugin/Field/FieldFormatter/FlexsliderFormatter.php
Returns a form to configure settings for the formatter.
FlexsliderResponsiveFormatter::settingsForm in flexslider_fields/src/Plugin/Field/FieldFormatter/FlexsliderResponsiveFormatter.php
Returns a form to configure settings for the formatter.

File

flexslider_fields/src/Plugin/Field/FieldFormatter/FlexsliderImageFormatterTrait.php, line 102

Class

FlexsliderImageFormatterTrait
A trait for all image-related FlexSlider formatters.

Namespace

Drupal\flexslider_fields\Plugin\Field\FieldFormatter

Code

protected function captionSettings(FormatterBase $formatter, FieldDefinitionInterface $field_definition) {
  $field_settings = $field_definition
    ->getSettings();

  // Set the caption options.
  $caption_options = [
    0 => $formatter
      ->t('None'),
    1 => $formatter
      ->t('Image title'),
    'alt' => $formatter
      ->t('Image ALT attribute'),
  ];
  $default_value = $formatter
    ->getSetting('caption');

  // Remove the options that are not available.
  $action_fields = [];
  if ($field_settings['title_field'] === FALSE) {
    unset($caption_options[1]);

    // User action required on the image title.
    $action_fields[] = 'title';
    if ($default_value == 1) {
      $default_value = '';
    }
  }
  if ($field_settings['alt_field'] === FALSE) {
    unset($caption_options['alt']);

    // User action required on the image alt.
    $action_fields[] = 'alt';
    if ($default_value == 'alt') {
      $default_value = '';
    }
  }

  // Create the caption element.
  $element['caption'] = [
    '#title' => $formatter
      ->t('Choose a caption source'),
    '#type' => 'select',
    '#options' => $caption_options,
    '#default_value' => $default_value,
  ];

  // If the image field doesn't have all of the suitable caption sources,
  // tell the user.
  if ($action_fields) {
    $action_text = $formatter
      ->t('enable the @action_field field', [
      '@action_field' => implode(' and/or ', $action_fields),
    ]);

    /* This may be a base field definition (e.g. in Views UI) which means it
     * is not associated with a bundle and will not have the toUrl() method.
     * So we need to check for the existence of the method before we can
     * build a link to the image field edit form.
     */
    if (method_exists($field_definition, 'toUrl')) {

      // Build the link to the image field edit form for this bundle.
      $rel = "{$field_definition->getTargetEntityTypeId()}-field-edit-form";
      $action = $field_definition
        ->toLink($action_text, $rel, [
        'fragment' => 'edit-settings-alt-field',
        'query' => \Drupal::destination()
          ->getAsArray(),
      ])
        ->toRenderable();
    }
    else {

      // Just use plain text if we can't build the field edit link.
      $action = [
        '#markup' => $action_text,
      ];
    }
    $element['caption']['#description'] = $formatter
      ->t('You need to @action for this image field to be able to use it as a caption.', [
      '@action' => render($action),
    ]);

    // If there are no suitable caption sources, disable the caption element.
    if (count($action_fields) >= 2) {
      $element['caption']['#disabled'] = TRUE;
    }
  }
  return $element;
}