You are here

insert_colorbox.module in Insert 8.2

File

modules/insert_colorbox/insert_colorbox.module
View source
<?php

/**
 * @file
 */
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormStateInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\insert\Utility\InsertUtility;

/**
 * Implements hook_insert_config_form().
 */
function insert_colorbox_insert_config_form(array $form) {
  $config = \Drupal::config('insert_colorbox.config');
  $form['insert_colorbox'] = [
    '#type' => 'fieldset',
    '#title' => t('Insert Colorbox'),
  ];
  $stylesList = _insert_colorbox_styles();
  $stylesList = InsertUtility::stylesListToOptions($stylesList);
  $form['insert_colorbox']['insert_colorbox__style'] = [
    '#type' => 'select',
    '#title' => t('Image style'),
    '#options' => array_merge([
      '0' => t('use “Link image to” widget setting'),
    ], $stylesList),
    '#default_value' => $config
      ->get('style'),
    '#description' => t('Select which image style to use for viewing images in the colorbox.'),
  ];
  $form['insert_colorbox']['insert_colorbox__gallery'] = [
    '#type' => 'radios',
    '#title' => t('Gallery'),
    '#description' => t('Select whether images placed with Insert be grouped in one or more colorbox galleries or if images shall open individual colorboxes.'),
    '#options' => [
      'post' => t('Per post gallery'),
      'page' => t('Per page gallery'),
      'field_post' => t('Per field in post gallery'),
      'field_page' => t('Per field in page gallery'),
      '0' => t('No gallery'),
    ],
    '#default_value' => $config
      ->get('gallery'),
  ];
  return $form;
}

/**
 * Implements hook_insert_config_submit_form().
 */
function insert_colorbox_insert_config_submit_form(FormStateInterface $form_state) {

  /** @var \Drupal\Core\Config\Config $config */
  $config = \Drupal::getContainer()
    ->get('config.factory')
    ->getEditable('insert_colorbox.config');
  $config
    ->set('style', $form_state
    ->getValue('insert_colorbox__style'));
  $config
    ->set('gallery', $form_state
    ->getValue('insert_colorbox__gallery'));
  $config
    ->save();
}

/**
 * Implements hook_theme().
 */
function insert_colorbox_theme() {
  return [
    'insert_colorbox_image' => [
      'template' => 'insert-colorbox-image',
      'pattern' => 'insert_colorbox_image__[a-z0-9_]+',
    ],
  ];
}

/**
 * @return array
 */
function _insert_colorbox_styles() {
  $stylesList = [];
  foreach (ImageStyle::loadMultiple() as $style) {

    /* @var ImageStyle $style */
    $stylesList[$style
      ->getName()] = $style;
  }
  $stylesList['image'] = [
    'label' => t('Original image'),
    'weight' => -10,
  ];
  return $stylesList;
}

/**
 * Implements hook_insert_variables().
 */
function insert_colorbox_insert_variables($insertType, array &$element, $styleName, array &$vars) {
  if (strpos($styleName, 'colorbox__') !== 0) {
    return;
  }
  $insertSettings = $element['#insert']['settings'];

  // The following lines are adapted from colorbox module's
  // template_preprocess_colorbox_formatter():
  static $gallery_token = NULL;
  $config = \Drupal::config('insert_colorbox.config');
  switch ($config
    ->get('gallery')) {
    case 'post':
      $vars['gallery_id'] = 'gallery-' . $element['#entity_type'];
      break;
    case 'page':
      $vars['gallery_id'] = 'gallery-all';
      break;
    case 'field_post':
      $vars['gallery_id'] = 'gallery-' . $element['#entity_type'] . '-' . $element['#field_name'];
      break;
    case 'field_page':
      $vars['gallery_id'] = 'gallery-' . $element['#field_name'];
      break;
    default:
      $vars['gallery_id'] = '';
  }
  $colorboxConfig = \Drupal::config('colorbox.settings');
  if (!empty($vars['gallery_id']) && $colorboxConfig
    ->get('advanced.unique_token')) {
    if (is_null($gallery_token)) {
      $gallery_token = Crypt::randomBytesBase64(8);
    }
    $vars['gallery_id'] = $vars['gallery_id'] . '-' . $gallery_token;
  }
  $vars['url_link'] = $vars['url_original'];
  $styleName = NULL;
  if ($config
    ->get('style') === '0') {
    $styleName = $insertSettings['link_image'];
  }
  elseif ($config
    ->get('style') !== 'image') {
    $styleName = $config
      ->get('style');
  }
  if ($styleName !== NULL) {
    $url = InsertUtility::buildDerivativeUrl($vars['file'], $styleName, \Drupal::config('insert.config')
      ->get('absolute'));
    if ($url !== NULL) {
      $vars['url_link'] = $url;
    }
  }
}

/**
 * Implements hook_insert_styles().
 */
function insert_colorbox_insert_styles($insertType) {
  if ($insertType !== INSERT_TYPE_IMAGE) {
    return [];
  }
  $insertStyles = [];

  /* @var ImageStyle $style */
  foreach (ImageStyle::loadMultiple() as $style) {
    $insertStyles['colorbox__' . $style
      ->getName()] = [
      'label' => t('Colorbox @style', [
        '@style' => $style
          ->getName(),
      ]),
    ];
  }
  return $insertStyles;
}

/**
 * Implements hook_insert_render().
 */
function insert_colorbox_insert_render($styleName, array $vars) {
  $moduleName = explode('__', $styleName, 2)[0];
  if ($moduleName !== 'colorbox') {
    return [];
  }
  $templateStyleName = str_replace('-', '_', $styleName);
  $templateFieldName = str_replace('-', '_', $vars['field_name']);
  return \Drupal::theme()
    ->render([
    'insert_colorbox_image__' . $templateFieldName . '__' . $templateStyleName,
    'insert_colorbox_image__' . $templateFieldName,
    'insert_colorbox_image__' . $templateStyleName,
    'insert_colorbox_image',
  ], $vars);
}

/**
 * Implements hook_editor_js_settings_alter().
 */
function insert_colorbox_editor_js_settings_alter(array &$settings) {
  InsertUtility::addEditorExtraAllowedContent($settings, [
    'a[data-colorbox-gallery]',
  ]);
}

/**
 * Implements hook_insert_allowed_html().
 */
function insert_colorbox_insert_allowed_html() {
  return [
    'tags' => [
      'a' => NULL,
    ],
    'attributes' => [
      'a' => [
        'data-colorbox-gallery' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_module_implements_alter().
 */
function insert_colorbox_module_implements_alter(array &$implementations, $hook) {
  if ($hook === 'insert_variables') {

    // Move hook_insert_variables() implementation to the end of the execution
    // list to have it processed after insert_insert_variables().
    $group = $implementations['insert_colorbox'];
    unset($implementations['insert_colorbox']);
    $implementations['insert_colorbox'] = $group;
  }
}