You are here

public function BlazyAdminBase::mediaSwitchForm in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/Form/BlazyAdminBase.php \Drupal\blazy\Form\BlazyAdminBase::mediaSwitchForm()
  2. 8 src/Form/BlazyAdminBase.php \Drupal\blazy\Form\BlazyAdminBase::mediaSwitchForm()

Returns re-usable media switch form elements.

1 call to BlazyAdminBase::mediaSwitchForm()
BlazyAdminFormatter::buildSettingsForm in src/Form/BlazyAdminFormatter.php
Defines re-usable form elements.

File

src/Form/BlazyAdminBase.php, line 462

Class

BlazyAdminBase
A base for blazy admin integration to have re-usable methods in one place.

Namespace

Drupal\blazy\Form

Code

public function mediaSwitchForm(array &$form, $definition = []) {
  $settings = isset($definition['settings']) ? $definition['settings'] : [];
  $lightboxes = $this
    ->manager()
    ->getLightboxes();
  $is_token = function_exists('token_theme');
  if (!isset($settings['media_switch'])) {
    return;
  }
  $form['media_switch'] = $this
    ->baseForm($definition)['media_switch'];
  $form['media_switch']['#prefix'] = '<h3 class="form__title form__title--media-switch">' . t('Media switcher') . '</h3>';
  if (empty($definition['no_ratio'])) {
    $form['ratio'] = $this
      ->baseForm($definition)['ratio'];
  }

  // Optional lightbox integration.
  if (!empty($lightboxes)) {
    $form['box_style'] = $this
      ->baseForm($definition)['box_style'];
    if (!empty($definition['multimedia'])) {
      $form['box_media_style'] = $this
        ->baseForm($definition)['box_media_style'];
    }
    $box_captions = [
      'auto' => t('Automatic'),
      'alt' => t('Alt text'),
      'title' => t('Title text'),
      'alt_title' => t('Alt and Title'),
      'title_alt' => t('Title and Alt'),
      'entity_title' => t('Content title'),
      'custom' => t('Custom'),
    ];
    if (!empty($definition['box_captions'])) {
      $form['box_caption'] = [
        '#type' => 'select',
        '#title' => t('Lightbox caption'),
        '#options' => $box_captions,
        '#weight' => -98,
        '#states' => $this
          ->getState(static::STATE_LIGHTBOX_ENABLED, $definition),
        '#description' => t('Automatic will search for Alt text first, then Title text. Try selecting <strong>- None -</strong> first when changing if trouble with form states.'),
      ];
      if ($this
        ->showAltTitleFieldHint($definition)) {
        $form['box_caption']['#description'] = $this
          ->showAltTitleFieldHint($definition);
      }
      $form['box_caption_custom'] = [
        '#title' => t('Lightbox custom caption'),
        '#type' => 'textfield',
        '#weight' => -97,
        '#states' => $this
          ->getState(static::STATE_LIGHTBOX_CUSTOM, $definition),
        '#description' => t('Multi-value rich text field will be mapped to each image by its delta.'),
      ];
      if ($is_token) {
        $types = isset($definition['entity_type_id']) ? [
          $definition['entity_type_id'],
        ] : [];
        $types = isset($definition['target_type']) ? array_merge($types, [
          $definition['target_type'],
        ]) : $types;
        $token_tree = [
          '#theme' => 'token_tree_link',
          '#text' => t('Tokens'),
          '#token_types' => $types,
        ];
        $form['box_caption_custom']['#field_suffix'] = drupal_render($token_tree);
      }
      else {
        $form['box_caption_custom']['#description'] .= ' ' . t('Install Token module to browse available tokens.');
      }
    }
  }
  drupal_alter('blazy_media_switch_form_element', $form, $definition);
}