You are here

function template_preprocess_bxslider in BxSlider 8

Preprocess function.

1 call to template_preprocess_bxslider()
template_preprocess_bxslider_ths in ./bxslider.module
Preprocess function.

File

./bxslider.module, line 52
BxSlider integration.

Code

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';
    }
  }
}