You are here

public function ColorboxFormatter::settingsForm in Colorbox 8

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/ColorboxFormatter.php, line 126

Class

ColorboxFormatter
Plugin implementation of the 'colorbox' formatter.

Namespace

Drupal\colorbox\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)');
  $description_link = Link::fromTextAndUrl($this
    ->t('Configure Image Styles'), Url::fromRoute('entity.image_style.collection'));
  $element['colorbox_node_style'] = [
    '#title' => $this
      ->t('Image style for content'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('colorbox_node_style'),
    '#empty_option' => $this
      ->t('None (original image)'),
    '#options' => $image_styles_hide,
    '#description' => $description_link
      ->toRenderable() + [
      '#access' => $this->currentUser
        ->hasPermission('administer image styles'),
    ],
  ];
  $element['colorbox_node_style_first'] = [
    '#title' => $this
      ->t('Image style for first image in content'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('colorbox_node_style_first'),
    '#empty_option' => $this
      ->t('No special style.'),
    '#options' => $image_styles,
    '#description' => $description_link
      ->toRenderable() + [
      '#access' => $this->currentUser
        ->hasPermission('administer image styles'),
    ],
  ];
  $element['colorbox_image_style'] = [
    '#title' => $this
      ->t('Image style for Colorbox'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('colorbox_image_style'),
    '#empty_option' => $this
      ->t('None (original image)'),
    '#options' => $image_styles,
    '#description' => $description_link
      ->toRenderable() + [
      '#access' => $this->currentUser
        ->hasPermission('administer image styles'),
    ],
  ];
  $gallery = [
    'post' => $this
      ->t('Per post gallery'),
    'page' => $this
      ->t('Per page gallery'),
    'field_post' => $this
      ->t('Per field in post gallery'),
    'field_page' => $this
      ->t('Per field in page gallery'),
    'custom' => $this
      ->t('Custom (with tokens)'),
    'none' => $this
      ->t('No gallery'),
  ];
  $element['colorbox_gallery'] = [
    '#title' => $this
      ->t('Gallery (image grouping)'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('colorbox_gallery'),
    '#options' => $gallery,
    '#description' => $this
      ->t('How Colorbox should group the image galleries.'),
  ];
  $element['colorbox_gallery_custom'] = [
    '#title' => $this
      ->t('Custom gallery'),
    '#type' => 'textfield',
    '#default_value' => $this
      ->getSetting('colorbox_gallery_custom'),
    '#description' => $this
      ->t('All images on a page with the same gallery value (rel attribute) will be grouped together. It must only contain lowercase letters, numbers, and underscores.'),
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => [
          'value' => 'custom',
        ],
      ],
    ],
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $entity_type = '';
    if (isset($form['#entity_type']) && !empty($form['#entity_type'])) {
      $entity_type = $form['#entity_type'];
    }
    $element['colorbox_token_gallery'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Replacement patterns'),
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $entity_type,
        'file',
      ],
      '#states' => [
        'visible' => [
          ':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => [
            'value' => 'custom',
          ],
        ],
      ],
    ];
  }
  else {
    $element['colorbox_token_gallery'] = [
      '#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_edit_form][settings][colorbox_gallery]"]' => [
            'value' => 'custom',
          ],
        ],
      ],
    ];
  }
  $caption = [
    'auto' => $this
      ->t('Automatic'),
    'title' => $this
      ->t('Title text'),
    'alt' => $this
      ->t('Alt text'),
    'entity_title' => $this
      ->t('Content title'),
    'custom' => $this
      ->t('Custom (with tokens)'),
    'none' => $this
      ->t('None'),
  ];
  $element['colorbox_caption'] = [
    '#title' => $this
      ->t('Caption'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('colorbox_caption'),
    '#options' => $caption,
    '#description' => $this
      ->t('Automatic will use the first non-empty value out of the title, the alt text and the content title.'),
  ];
  $element['colorbox_caption_custom'] = [
    '#title' => $this
      ->t('Custom caption'),
    '#type' => 'textfield',
    '#default_value' => $this
      ->getSetting('colorbox_caption_custom'),
    '#states' => [
      'visible' => [
        ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => [
          'value' => 'custom',
        ],
      ],
    ],
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $entity_type = '';
    if (isset($form['#entity_type']) && !empty($form['#entity_type'])) {
      $entity_type = $form['#entity_type'];
    }
    $element['colorbox_token_caption'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Replacement patterns'),
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $entity_type,
        'file',
      ],
      '#states' => [
        'visible' => [
          ':input[name$="[settings_edit_form][settings][colorbox_caption]"]' => [
            'value' => 'custom',
          ],
        ],
      ],
    ];
  }
  else {
    $element['colorbox_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_edit_form][settings][colorbox_caption]"]' => [
            'value' => 'custom',
          ],
        ],
      ],
    ];
  }
  return $element;
}