You are here

bxslider.module in BxSlider 8

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

BxSlider integration.

File

bxslider.module
View source
<?php

/**
 * @file
 * BxSlider integration.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function bxslider_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the bxslider module.
    case 'help.page.bxslider':
      $output = '';
      $output .= '<h3>' . t('About BxSlider') . '</h3>';
      $output .= '<p>' . t('Integration BxSlider with Field Formatter.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_theme().
 */
function bxslider_theme($existing, $type, $theme, $path) {
  return [
    'bxslider' => [
      'variables' => [
        'items' => [],
        'settings' => [],
      ],
      'template' => 'bxslider',
    ],
    'bxslider_ths' => [
      'variables' => [
        'items' => [],
        'thumbnail_items' => [],
        'settings' => [],
      ],
      'template' => 'bxslider-thumbnail',
    ],
  ];
}

/**
 * Preprocess function.
 */
function template_preprocess_bxslider(&$variables) {
  foreach ($variables['items'] as $key => $item) {
    if ($variables['settings']['colorbox']['enable']) {

      // Configure colorbox formatter according to current settings.
      $colorbox_formatter_settings = [
        'style_name' => $variables['settings']['image_style'],
        'colorbox_node_style' => $variables['settings']['image_style'],
        'colorbox_node_style_first' => $variables['settings']['image_style'],
        'colorbox_image_style' => $variables['settings']['colorbox']['image_style'],
        'colorbox_gallery' => $variables['settings']['colorbox']['colorbox_gallery'],
        'colorbox_gallery_custom' => $variables['settings']['colorbox']['colorbox_gallery_custom'],
        'colorbox_caption' => $variables['settings']['colorbox']['colorbox_caption'],
        'colorbox_caption_custom' => $variables['settings']['colorbox']['colorbox_caption_custom'],
      ];

      // Use Colorbox Formatter theme.
      $variables['slides'][$key]['image'] = [
        '#theme' => 'colorbox_formatter',
        '#item' => $item['item'],
        '#item_attributes' => $item['item_attributes'],
        '#entity' => $item['entity'],
        '#settings' => $colorbox_formatter_settings,
      ];
    }
    elseif (!empty($variables['settings']['image_style'])) {
      $variables['slides'][$key]['image'] = [
        '#theme' => 'image_style',
        '#style_name' => $variables['settings']['image_style'],
      ];
    }
    else {
      $variables['slides'][$key]['image'] = [
        '#theme' => 'image',
      ];
    }
    if (($entity = $item['item']->entity) && empty($item['item']->uri)) {
      $variables['slides'][$key]['image']['#uri'] = $entity
        ->getFileUri();
    }
    else {
      $variables['slides'][$key]['image']['#uri'] = $item['item']->uri;
    }
  }
  if ($variables['settings']['colorbox']['enable']) {

    // Attach Colorbox libraries.
    \Drupal::service('colorbox.attachment')
      ->attach($variables);
  }

  // If is selected custom pager. It is never used for variant with
  // thumbnails slider.
  if (isset($variables['settings']['bxslider']['pagerCustom_type']) && $variables['settings']['bxslider']['pagerCustom_type'] != 'none') {
    $method2_items = '';
    foreach ($variables['items'] as $key => $item) {
      if (isset($variables['settings']['bxslider']['pagerCustom_type']) && ($variables['settings']['bxslider']['pagerCustom_type'] == 'thumbnail_pager_method1' || $variables['settings']['bxslider']['pagerCustom_type'] == 'thumbnail_pager_method2')) {

        // Prepare variables for theme_image_style.
        $pager_custom_image_style = empty($variables['settings']['bxslider']['pagerCustom_image_style']) ? 'thumbnail' : $variables['settings']['bxslider']['pagerCustom_image_style'];
        if (($entity = $item['item']->entity) && empty($item['item']->uri)) {
          $image_uri = $entity
            ->getFileUri();
        }
        else {
          $image_uri = $item['item']->uri;
        }
        $pager_custom_image_render = [
          '#theme' => 'image_style',
          '#style_name' => $pager_custom_image_style,
          '#uri' => $image_uri,
        ];
        $variables['items'][$key]['slide_pagerCustom'] = $pager_custom_image_render;
        $pager_custom_image = (string) \Drupal::service('renderer')
          ->render($pager_custom_image_render);
        if ($variables['settings']['bxslider']['pagerCustom_type'] == 'thumbnail_pager_method2') {
          $method2_items .= 'case ' . $key . ': return "' . $pager_custom_image . '";';
        }
      }
    }
    if (isset($variables['settings']['bxslider']['pagerCustom_type']) && $variables['settings']['bxslider']['pagerCustom_type'] == 'thumbnail_pager_method1') {

      // Use first value in the template and other set in bxslider
      // configuration.
      $variables['settings']['bxslider']['pagerCustom'] = 'bxslider-pager-th-' . $variables['settings']['slider_id'];
      $variables['#attached']['drupalSettings']['bxslider'][$variables['settings']['slider_id']]['bxslider']['pagerCustom'] = '#bxslider-pager-th-' . $variables['settings']['slider_id'];
    }
    if (isset($variables['settings']['bxslider']['pagerCustom_type']) && $variables['settings']['bxslider']['pagerCustom_type'] == 'thumbnail_pager_method2') {

      // Remove comments generated in debug mode, and new lines.
      $method2_items = trim(preg_replace('/\\s+/', ' ', $method2_items));
      $method2_items = preg_replace('/<!--(.*)-->/Uis', '', $method2_items);
      $variables['#attached']['drupalSettings']['bxslider'][$variables['settings']['slider_id']]['bxslider']['buildPager'] = 'switch(slideIndex){' . $method2_items . '}';
      $variables['#attached']['library'][] = 'bxslider/jquery.bxslider_custom_pager2';
    }
  }
}

/**
 * Preprocess function.
 */
function template_preprocess_bxslider_ths(&$variables) {
  template_preprocess_bxslider($variables);
  foreach ($variables['thumbnail_items'] as $key => $item) {
    if (!empty($variables['settings']['thumbnail_slider']['thumbnail_style'])) {
      $variables['thumbnails'][$key]['image'] = [
        '#theme' => 'image_style',
        '#style_name' => $variables['settings']['thumbnail_slider']['thumbnail_style'],
      ];
    }
    else {
      $variables['thumbnails'][$key]['image'] = [
        '#theme' => 'image',
      ];
    }
    if (($entity = $item->entity) && empty($item->uri)) {
      $variables['thumbnails'][$key]['image']['#uri'] = $entity
        ->getFileUri();
    }
    else {
      $variables['thumbnails'][$key]['image']['#uri'] = $item->uri;
    }
  }

  // Add additional class to a thumbnail wrapper.
  $default_wrapper_class = !empty($variables['settings']['thumbnail_slider']['wrapperClass']) ? $variables['settings']['thumbnail_slider']['wrapperClass'] : 'bx-wrapper';
  $variables['#attached']['drupalSettings']['bxslider_ths'][$variables['settings']['slider_id']]['thumbnail_slider']['wrapperClass'] = $default_wrapper_class . ' bx-wrapper__ths';
}

Functions

Namesort descending Description
bxslider_help Implements hook_help().
bxslider_theme Implements hook_theme().
template_preprocess_bxslider Preprocess function.
template_preprocess_bxslider_ths Preprocess function.