You are here

blazy_test.module in Blazy 8

Same filename and directory in other branches
  1. 8.2 tests/modules/blazy_test/blazy_test.module

Testing various Blazy hooks, etc.

File

tests/modules/blazy_test/blazy_test.module
View source
<?php

/**
 * @file
 * Testing various Blazy hooks, etc.
 */

/**
 * Implements hook_theme().
 */
function blazy_test_theme() {
  return [
    'blazy_test' => [
      'render element' => 'element',
    ],
  ];
}

/**
 * Prepares variables for blazy-test.html.twig templates.
 */
function template_preprocess_blazy_test(&$variables) {
  $element = $variables['element'];
  $variables['content'] = $element['#children'];
  $variables['settings'] = isset($element['#settings']) ? $element['#settings'] : [];
}

/**
 * Implements hook_blazy_attach_alter().
 */
function blazy_test_blazy_attach_alter(array &$load, array $settings = []) {
  if (!empty($settings['blazy_test'])) {
    $load['library'][] = 'foo/fake_library';
    $manager = \Drupal::service('blazy.manager');
    $template = [
      '#theme' => 'blazy_test',
    ];
    $load['drupalSettings']['blazy_test'] = [
      'options' => [
        'slideshow' => TRUE,
      ],
      'container' => $manager
        ->getRenderer()
        ->renderPlain($template),
    ];
  }
}

/**
 * Implements hook_blazy_attach_alter().
 */
function blazy_test_blazy_lightboxes_alter(array &$lightboxes) {
  $lightboxes[] = 'blazy_test';
}

/**
 * Implements hook_blazy_attach_alter().
 */
function blazy_test_blazy_alter(array &$image, array $settings = []) {
  if (!empty($settings['media_switch']) && $settings['media_switch'] == 'blazy_test') {
    $image['#pre_render'][] = 'blazy_test_pre_render';
  }
}

/**
 * The #pre_render callback: Sets blazy_test related URL attributes.
 */
function blazy_test_pre_render($image) {
  $settings = isset($image['#settings']) ? $image['#settings'] : [];

  // Video's HREF points to external site, adds URL to local image.
  if (!empty($settings['box_url']) && !empty($settings['embed_url'])) {
    $image['#url_attributes']['data-box-url'] = $settings['box_url'];
  }
  return $image;
}

/**
 * Implements hook_hook_info().
 */
function blazy_test_hook_info() {
  $hooks['blazy_test_skins_info'] = [
    'group' => 'blazy_test',
  ];
  return $hooks;
}

/**
 * Implements hook_library_info_alter().
 */
function blazy_test_library_info_alter(array &$libraries, $extension) {
  if ($extension !== 'blazy') {
    return;
  }
  $path = \Drupal::root() . '/libraries/blazy/blazy.min.js';
  if (!is_file($path)) {
    $libraries['blazy']['js'] = [
      'https://cdn.jsdelivr.net/blazy/latest/blazy.min.js' => [
        'type' => 'external',
        'weight' => -4,
      ],
    ];
    unset($libraries['blazy']['js']['/libraries/blazy/blazy.min.js']);
  }
}
if (!function_exists('token_theme')) {

  /**
   * Dummy function.
   */
  function token_theme() {

    // Empty block to satisfy coder.
  }
}