You are here

blazy_photoswipe.module in Blazy PhotoSwipe 8

Same filename and directory in other branches
  1. 7 blazy_photoswipe.module

Provides a simple Blazy PhotoSwipe integration.

File

blazy_photoswipe.module
View source
<?php

/**
 * @file
 * Provides a simple Blazy PhotoSwipe integration.
 */

/**
 * Overrides variables for blazy.html.twig.
 */
function blazy_photoswipe_preprocess_blazy(&$variables) {
  $settings = $variables['settings'];
  if (!empty($settings['media_switch']) && $settings['media_switch'] == 'photoswipe') {

    // Video's HREF points to external site, adds URL to local image.
    // Do not rely on IMG as IMG is not there when using CSS background.
    if (!empty($settings['box_url'])) {
      $variables['url_attributes']['data-box-url'] = $settings['box_url'];
    }
  }
}

/**
 * Implements hook_blazy_attach_alter().
 */
function blazy_photoswipe_blazy_attach_alter(array &$load, $attach = []) {
  if (!empty($attach['photoswipe'])) {
    $config = blazy()
      ->configLoad();
    $five = !empty($config['extras']['photoswipe']) && $config['extras']['photoswipe'] == 5;
    $load['library'][] = $five ? 'blazy_photoswipe/load5' : 'blazy_photoswipe/load';

    // Allow other modules to alter / extend the options to pass to photoswipe.
    $options = blazy()
      ->configLoad('options', 'photoswipe.settings');
    blazy()
      ->getModuleHandler()
      ->alter('blazy_photoswipe_js_options', $options);
    $load['drupalSettings']['photoswipe']['options'] = $options;
    if (!$five) {
      $template = [
        '#theme' => 'photoswipe_container',
      ];
      $load['drupalSettings']['photoswipe']['container'] = blazy()
        ->getRenderer()
        ->renderPlain($template);
    }
  }
}

/**
 * Implements hook_blazy_lightboxes_alter().
 */
function blazy_photoswipe_blazy_lightboxes_alter(array &$lightboxes) {
  $lightboxes[] = 'photoswipe';
}

/**
 * Implements hook_blazy_settings_alter().
 */
function blazy_photoswipe_blazy_settings_alter(array &$build, $items) {
  $settings =& $build['settings'];

  // Tell Blazy we are supporting rich box: local video within lightbox.
  if (!empty($settings['media_switch']) && $settings['media_switch'] == 'photoswipe') {
    $config = blazy()
      ->configLoad();
    $settings['_photoswipe5'] = !empty($config['extras']['photoswipe']) && $config['extras']['photoswipe'] == 5;
    $settings['_richbox'] = TRUE;
  }
}

/**
 * Implements hook_config_schema_info_alter().
 */
function blazy_photoswipe_config_schema_info_alter(array &$definitions) {
  if (isset($definitions['blazy.settings'])) {
    $definitions['blazy.settings']['mapping']['extras']['mapping']['photoswipe']['type'] = 'integer';
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function blazy_photoswipe_form_blazy_settings_form_alter(array &$form, $form_state) {
  $settings = blazy()
    ->configLoad();
  $form['extras']['#access'] = TRUE;
  $form['extras']['photoswipe'] = [
    '#type' => 'select',
    '#title' => t('PhotoSwipe'),
    '#options' => [
      5 => t('PhotoSwipe 5'),
    ],
    '#empty_option' => t('- None -'),
    '#default_value' => isset($settings['extras']['photoswipe']) ? $settings['extras']['photoswipe'] : '',
    '#description' => t('Warning! WIP, might break. Switch to PhotoSwipe 5. Default is PhotoSwipe 4. Check out /admin/help/blazy_photoswipe for howtos. Clear cache. See #3214672'),
  ];
}

/**
 * Implements hook_help().
 */
function blazy_photoswipe_help($route_name) {
  if ($route_name == 'help.page.blazy_photoswipe') {
    $output = file_get_contents(dirname(__FILE__) . '/README.md');

    // @todo remove check once Blazy has stable release.
    return function_exists('blazy_parse_markdown') ? blazy_parse_markdown($output) : '<pre>' . $output . '</pre>';
  }
  return '';
}