You are here

public function BlazyAdminBase::baseForm in Blazy 8

Same name and namespace in other branches
  1. 8.2 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 383

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();
  $image_styles = function_exists('image_style_options') ? image_style_options(FALSE) : [];
  $is_responsive = function_exists('responsive_image_get_image_dimensions') && !empty($definition['responsive_image']);
  $form = [];
  if (empty($definition['no_image_style'])) {
    $form['image_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Image style'),
      '#options' => $image_styles,
      '#description' => $this
        ->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.'),
      '#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. Be sure to add Thumbnail style if using Photobox. 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) {
        $form['media_switch']['#options'][$lightbox] = $this
          ->t('Image to @lightbox', [
          '@lightbox' => $lightbox,
        ]);
      }

      // Re-use the same image style for both lightboxes.
      $form['box_style'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Lightbox image style'),
        '#options' => $image_styles,
        '#states' => $this
          ->getState(static::STATE_LIGHTBOX_ENABLED, $definition),
        '#weight' => -99,
      ];
      if (!empty($definition['multimedia'])) {
        $form['box_media_style'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Lightbox video style'),
          '#options' => $image_styles,
          '#description' => $this
            ->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' => -99,
        ];
      }
    }

    // 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',
      'enforced',
    ];
    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. 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>fluid</strong> if unsure. Choose <strong>enforced</strong> if you can stick to one aspect ratio and want multi-serving, or Responsive images. <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 Responsive image.', [
          '@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' => -96,
      ];
      if ($is_responsive) {
        $form['ratio']['#states'] = $this
          ->getState(static::STATE_RESPONSIVE_IMAGE_STYLE_DISABLED, $definition);
      }
    }
  }
  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' => -96,
      '#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' => function_exists('image_style_options') ? image_style_options(TRUE) : [],
      '#description' => $this
        ->t('Usages: Photobox/PhotoSwipe thumbnail, or custom work with thumbnails. Leave empty to not use thumbnails.'),
      '#weight' => -100,
    ];
  }
  $this->blazyManager
    ->getModuleHandler()
    ->alter('blazy_base_form_element', $form, $definition);
  return $form;
}