You are here

function _views_slideshow_preprocess_views_view_slideshow in Views Slideshow 8.4

Same name and namespace in other branches
  1. 8.3 views_slideshow.theme.inc \_views_slideshow_preprocess_views_view_slideshow()

Views Slideshow: slideshow.

Related topics

1 call to _views_slideshow_preprocess_views_view_slideshow()
template_preprocess_views_view_slideshow in ./views_slideshow.module
Views Slideshow: Slideshow.

File

./views_slideshow.theme.inc, line 32
The theme system, which controls the output of views slideshow.

Code

function _views_slideshow_preprocess_views_view_slideshow(&$vars) {
  $options = $vars['view']->style_plugin->options;
  $vars['skin'] = 'default';
  $vars['slideshow'] = '';
  $main_frame_module = $options['slideshow_type'];
  if (empty($main_frame_module)) {

    // Get all slideshow types.
    $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
    $types = $typeManager
      ->getDefinitions();
    if ($types) {
      foreach ($types as $id => $definition) {
        $main_frame_module = $id;
        break;
      }
    }
  }

  // Make sure the main slideshow settings are defined before building the
  // slideshow.
  if (empty($main_frame_module)) {
    \Drupal::messenger()
      ->addMessage(t('No main frame module is enabled for views slideshow. This is often because another module which Views Slideshow needs is not enabled. For example, 4.x needs a module like "Views Slideshow: Cycle" enabled.'), 'error');
  }
  elseif (empty($options[$main_frame_module])) {
    \Drupal::messenger()
      ->addMessage(t('The options for @module does not exists.', [
      '@module' => $main_frame_module,
    ]), 'error');
  }
  elseif (!empty($vars['rows'])) {
    $settings = $options[$main_frame_module];
    $view = $vars['view'];
    $rows = $vars['rows'];

    // The #name element is not available on Views edit pages.
    $view_element_name = isset($view->element['#name']) ? $view->element['#name'] : '';
    $vss_id = $view_element_name . '-' . $view->current_display;

    // Give each slideshow a unique id if there are more than one on the page.
    static $instances = [];
    if (isset($instances[$vss_id])) {
      $instances[$vss_id]++;
      $vss_id .= "_" . $instances[$vss_id];
    }
    else {
      $instances[$vss_id] = 1;
    }

    // Building our default methods.
    $methods = [
      'goToSlide' => [],
      'nextSlide' => [],
      'pause' => [],
      'play' => [],
      'previousSlide' => [],
      'transitionBegin' => [],
      'transitionEnd' => [],
    ];

    // Pull all widget info and slideshow info and merge them together.
    $widgetTypeManager = \Drupal::service('plugin.manager.views_slideshow.widget_type');
    $widgetTypes = $widgetTypeManager
      ->getDefinitions();
    $slideshowTypeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
    $slideshowTypes = $slideshowTypeManager
      ->getDefinitions();
    $addons = array_merge($widgetTypes, $slideshowTypes);

    // Loop through all the addons and call their methods if needed.
    foreach ($addons as $addon_id => $addon_info) {
      foreach ($addon_info['accepts'] as $imp_key => $imp_value) {
        if (is_array($imp_value)) {
          $methods[$imp_key][] = \Drupal::service('views_slideshow.format_addons_name')
            ->format($addon_id);
        }
        else {
          $methods[$imp_value][] = \Drupal::service('views_slideshow.format_addons_name')
            ->format($addon_id);
        }
      }
    }
    $vars['#attached']['library'][] = 'views_slideshow/widget_info';
    $vars['#attached']['drupalSettings']['viewsSlideshow'][$vss_id] = [
      'methods' => $methods,
      'paused' => 0,
    ];

    // Process Skins.
    $skinManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_skin');
    $skin = $skinManager
      ->createInstance($options['slideshow_skin']);
    $vars['skin'] = $skin
      ->getClass();
    foreach ($skin
      ->getLibraries() as $library) {
      $vars['#attached']['library'][] = $library;
    }

    // Process Widgets.
    // Build weights.
    $weight = [];
    for ($i = 1; $i <= count($widgetTypes); $i++) {
      $weight['top'][$i] = [];
      $weight['bottom'][$i] = [];
    }
    $slide_count = count($view->result);
    if ($slide_count && $vars['view']->style_plugin->options['slideshow_type'] == 'views_slideshow_cycle') {
      $items_per_slide = $vars['view']->style_plugin->options['views_slideshow_cycle']['items_per_slide'];
      $slide_count = $slide_count / $items_per_slide;
    }
    foreach ($widgetTypes as $widgetTypeId => $widgetTypeName) {
      foreach ($weight as $location => $order) {
        if ($options['widgets'][$location][$widgetTypeId]['enable']) {

          // If hide on single slide and only a single slide skip rendering.
          if ($options['widgets'][$location][$widgetTypeId]['hide_on_single_slide'] && $slide_count <= 1) {
            continue;
          }
          $widgetWeight = $options['widgets'][$location][$widgetTypeId]['weight'] > count($widgetTypes) ? count($widgetTypes) : $options['widgets'][$location][$widgetTypeId]['weight'];
          $weight[$location][$widgetWeight][] = [
            'widgetId' => $widgetTypeId,
            'widgetSettings' => $options['widgets'][$location][$widgetTypeId],
          ];
        }
      }
    }

    // Build our widgets.
    foreach ($weight as $location => $order) {
      $vars[$location . '_widget_rendered'] = [];
      foreach ($order as $widgets) {
        if (is_array($widgets)) {
          foreach ($widgets as $widgetData) {
            $vars[$location . '_widget_rendered'][] = [
              '#theme' => $view
                ->buildThemeFunctions($widgetData['widgetId'] . '_widget'),
              '#vss_id' => $vss_id,
              '#view' => $view,
              '#settings' => $widgetData['widgetSettings'],
              '#location' => $location,
              '#rows' => $rows,
            ];
          }
        }
      }
    }

    // Process Slideshow.
    $slides = [
      '#theme' => $view
        ->buildThemeFunctions($main_frame_module . '_main_frame'),
      '#vss_id' => $vss_id,
      '#view' => $view,
      '#settings' => $settings,
      '#rows' => $rows,
    ];
    $vars['slideshow'] = [
      '#theme' => $view
        ->buildThemeFunctions('views_slideshow_main_section'),
      '#vss_id' => $vss_id,
      '#slides' => $slides,
      '#plugin' => $main_frame_module,
    ];
  }
}