You are here

public function BlazyAdminBase::baseForm in Blazy 8.2

Same name and namespace in other branches
  1. 8 src/Form/BlazyAdminBase.php \Drupal\blazy\Form\BlazyAdminBase::baseForm()
  2. 7 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 312

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->blazyManager
    ->getLightboxes();
  $form = [];
  $ui_url = '/admin/config/media/blazy';
  if ($this->blazyManager
    ->getModuleHandler()
    ->moduleExists('blazy_ui')) {
    $ui_url = Url::fromRoute('blazy.settings')
      ->toString();
  }
  if (empty($definition['no_image_style'])) {
    $form['image_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Image style'),
      '#options' => $this
        ->getEntityAsOptions('image_style'),
      '#description' => $this
        ->t('The content image style. This will be treated as the fallback image to override the global option <a href=":url">Responsive image 1px placeholder</a>, which is normally smaller, if Responsive image are provided. Shortly, leave it empty to make Responsive image fallback respected. 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.', [
        ':url' => $ui_url,
      ]),
      '#weight' => -100,
    ];
  }
  if (isset($settings['media_switch'])) {
    $form['media_switch'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Media switcher'),
      '#options' => [
        'content' => $this
          ->t('Image linked to content'),
      ],
      '#empty_option' => $this
        ->t('- None -'),
      '#description' => $this
        ->t('May depend on the enabled supported or supportive modules: colorbox, photobox etc. Add Thumbnail style if using Photobox, Slick, or others which may need it. 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 = Unicode::ucwords(str_replace('_', ' ', $lightbox));
        $form['media_switch']['#options'][$lightbox] = $this
          ->t('Image to @lightbox', [
          '@lightbox' => $name,
        ]);
      }

      // Re-use the same image style for both lightboxes.
      $form['box_style'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Lightbox image style'),
        '#options' => $this
          ->getResponsiveImageOptions() + $this
          ->getEntityAsOptions('image_style'),
        '#states' => $this
          ->getState(static::STATE_LIGHTBOX_ENABLED, $definition),
        '#weight' => -97,
        '#description' => $this
          ->t('Supports both Responsive and regular images.'),
      ];
      if (!empty($definition['multimedia'])) {
        $form['box_media_style'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Lightbox video style'),
          '#options' => $this
            ->getEntityAsOptions('image_style'),
          '#description' => $this
            ->t('Allows different lightbox video dimensions. Or can be used to have a swipable video if <a href=":url1">Blazy PhotoSwipe</a> or <a href=":url2">Slick Lightbox</a> installed.', [
            ':url1' => 'https:drupal.org/project/blazy_photoswipe',
            ':url2' => 'https:drupal.org/project/slick_lightbox',
          ]),
          '#states' => $this
            ->getState(static::STATE_LIGHTBOX_ENABLED, $definition),
          '#weight' => -96,
        ];
      }
    }

    // Adds common supported entities for media integration.
    if (!empty($definition['multimedia'])) {
      $form['media_switch']['#options']['media'] = $this
        ->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',
    ];
    if (empty($definition['no_ratio'])) {
      $form['ratio'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Aspect ratio'),
        '#options' => array_combine($ratio, $ratio),
        '#empty_option' => $this
          ->t('- None -'),
        '#description' => $this
          ->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. Use fixed ratio (non-fluid) to avoid JS works, or if it fails Responsive image. Fixed ratio means, all images from mobile to desktop use the same aspect ratio. Fluid means dimensions are calculated and JS works are attempted to fix aspect ratio. <a href="@link" target="_blank">Learn more</a>, or leave empty to DIY (such as using CSS mediaquery), or when working with multi-image-style plugin like GridStack.', [
          '@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' => -95,
      ];
    }
  }
  if (!empty($definition['target_type']) && !empty($definition['view_mode'])) {
    $form['view_mode'] = [
      '#type' => 'select',
      '#options' => $this
        ->getViewModeOptions($definition['target_type']),
      '#title' => $this
        ->t('View mode'),
      '#description' => $this
        ->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.'),
      '#weight' => -94,
      '#enforced' => TRUE,
    ];
    if ($this->blazyManager
      ->getModuleHandler()
      ->moduleExists('field_ui')) {
      $form['view_mode']['#description'] .= ' ' . $this
        ->t('Manage view modes on the <a href=":view_modes">View modes page</a>.', [
        ':view_modes' => Url::fromRoute('entity.entity_view_mode.collection')
          ->toString(),
      ]);
    }
  }
  if (!empty($definition['thumbnail_style'])) {
    $form['thumbnail_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Thumbnail style'),
      '#options' => $this
        ->getEntityAsOptions('image_style'),
      '#description' => $this
        ->t('Usages: Placeholder replacement for image effects (blur, etc.), Photobox/PhotoSwipe thumbnail, or custom work with thumbnails. Be sure to have similar aspect ratio for the best blur effect. Leave empty to not use thumbnails.'),
      '#weight' => -96,
    ];
  }

  // @todo this can also be used for local video poster image option.
  if (isset($definition['images'])) {
    $form['image'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Main stage'),
      '#options' => is_array($definition['images']) ? $definition['images'] : [],
      '#description' => $this
        ->t('Main background/stage/poster image field with the only supported field types: <b>Image</b> or <b>Media</b> containing Image field. You may want to add a new Image field to this entity.'),
      '#prefix' => '<h3 class="form__title form__title--fields">' . $this
        ->t('Fields') . '</h3>',
    ];
  }
  $this->blazyManager
    ->getModuleHandler()
    ->alter('blazy_base_form_element', $form, $definition);
  return $form;
}