You are here

public function BlazyAdminBase::baseForm in Blazy 7

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

Returns simple form elements common for Views field, EB widget, formatters.

File

src/Form/BlazyAdminBase.php, line 347

Class

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

Namespace

Drupal\blazy\Form

Code

public function baseForm($definition = []) {
  $settings = isset($definition['settings']) ? $definition['settings'] : [];
  $lightboxes = $this
    ->manager()
    ->getLightboxes();
  $image_styles = image_style_options(FALSE);
  $is_responsive = function_exists('picture_get_mapping_options') && !empty($definition['responsive_image']);
  $form = [];
  if (empty($definition['no_image_style'])) {
    $form['image_style'] = [
      '#type' => 'select',
      '#title' => t('Image style'),
      '#options' => $image_styles,
      '#description' => t('The content image style. This will be treated as the fallback image, which is normally smaller, if Breakpoints are provided. Otherwise this is the only image displayed. This image style is also used to provide dimensions not only for image/iframe but also any media entity like local video, where no images are even associated with, to have the designated dimensions in tandem with aspect ratio as otherwise no UI to customize for.'),
      '#weight' => -101,
    ];
  }
  if (isset($settings['media_switch'])) {
    $form['media_switch'] = [
      '#type' => 'select',
      '#title' => t('Media switcher'),
      '#options' => [
        'content' => t('Image linked to content'),
      ],
      '#empty_option' => t('- None -'),
      '#description' => t('May depend on the enabled supported or supportive modules: media, colorbox, photobox, photoswipe, intense, or any of blazy-supported lightboxes. Try selecting "<strong>- None -</strong>" first before changing if trouble with this complex form states.'),
      '#weight' => -99,
    ];

    // Optional lightbox integration.
    if (!empty($lightboxes)) {
      foreach ($lightboxes as $lightbox) {
        $name = ucwords(str_replace('_', ' ', $lightbox));
        $form['media_switch']['#options'][$lightbox] = t('Image to @lightbox', [
          '@lightbox' => $name,
        ]);
      }

      // Re-use the same image style for both lightboxes.
      $form['box_style'] = [
        '#type' => 'select',
        '#title' => t('Lightbox image style'),
        '#options' => $image_styles,
        '#states' => $this
          ->getState(static::STATE_LIGHTBOX_ENABLED, $definition),
        '#weight' => -98,
      ];
      if (!empty($definition['multimedia'])) {
        $form['box_media_style'] = [
          '#type' => 'select',
          '#title' => t('Lightbox video style'),
          '#options' => $image_styles,
          '#description' => t('Allows different lightbox video dimensions. Or can be used to have a swipable video if Blazy PhotoSwipe installed.'),
          '#states' => $this
            ->getState(static::STATE_LIGHTBOX_ENABLED, $definition),
          '#weight' => -98,
        ];
      }
    }

    // Adds common supported entities for media integration.
    if (!empty($definition['multimedia'])) {
      $form['media_switch']['#options']['media'] = t('Image to iFrame');
    }

    // http://en.wikipedia.org/wiki/List_of_common_resolutions
    $ratio = [
      '1:1',
      '3:2',
      '4:3',
      '8:5',
      '16:9',
      'fluid',
      'enforced',
    ];
    if (empty($definition['no_ratio'])) {
      $form['ratio'] = [
        '#type' => 'select',
        '#title' => t('Aspect ratio'),
        '#options' => array_combine($ratio, $ratio),
        '#empty_option' => t('- None -'),
        '#description' => t('Aspect ratio to get consistently responsive images and iframes. Coupled with Image style. And to fix layout reflow and excessive height issues. <a href="@dimensions" target="_blank">Image styles and video dimensions</a> must <a href="@follow" target="_blank">follow the aspect ratio</a>. If not, images will be distorted. Choose <strong>enforced</strong> if you can stick to one aspect ratio and want multi-serving, or Picture images. Try <strong>fluid</strong> if unsure. <a href="@link" target="_blank">Learn more</a>, or leave empty to DIY, or when working with multi-image-style plugin like GridStack. <br /><strong>Note!</strong> Only compatible with Blazy multi-serving images, but not Picture image, except for <b>enforced</b>.', [
          '@dimensions' => '//size43.com/jqueryVideoTool.html',
          '@follow' => '//en.wikipedia.org/wiki/Aspect_ratio_%28image%29',
          '@link' => '//www.smashingmagazine.com/2014/02/27/making-embedded-content-work-in-responsive-design/',
        ]),
        '#weight' => -93,
      ];
      if ($is_responsive) {
        $form['ratio']['#states'] = $this
          ->getState(static::STATE_RESPONSIVE_IMAGE_STYLE_DISABLED, $definition);
      }
    }
  }
  if (!empty($definition['thumbnail_style'])) {
    $form['thumbnail_style'] = [
      '#type' => 'select',
      '#title' => t('Thumbnail style'),
      '#options' => $image_styles,
      '#description' => t('Usages: Photobox/ PhotoSwipe thumbnail, or custom work with thumbnails. Leave empty to not use thumbnails.'),
      '#weight' => -94,
    ];
  }
  if (!empty($definition['target_type']) && !empty($definition['use_view_mode'])) {
    $options = $this
      ->getViewModeOptions($definition['target_type']);
    $form['view_mode'] = [
      '#type' => 'select',
      '#options' => $options,
      '#title' => t('View mode'),
      '#description' => t('Required to grab the fields, or to have custom entity display as fallback display. If it has fields, be sure the selected "View mode" is enabled, and the enabled fields here are not hidden there. Create view modes using hook_entity_info_alter, or <a href="@url" target="_blank">entity_view_mode</a>.', [
        '@url' => '//drupal.org/project/entity_view_mode',
      ]),
      '#weight' => -92,
      '#access' => count($options) > 1,
      '#enforced' => TRUE,
    ];
  }
  drupal_alter('blazy_base_form_element', $form, $definition);
  return $form;
}