You are here

function template_preprocess_views_bootstrap_carousel in Views Bootstrap 8.3

Same name and namespace in other branches
  1. 8.4 views_bootstrap.theme.inc \template_preprocess_views_bootstrap_carousel()

Prepares variables for views carousel template.

Default template: views-bootstrap-carousel.html.twig.

Parameters

array $vars: An associative array containing:

  • view: A ViewExecutable object.
  • rows: The raw row data.
1 string reference to 'template_preprocess_views_bootstrap_carousel'
ViewsBootstrap::getThemeHooks in src/ViewsBootstrap.php
Returns the theme hook definition information.

File

./views_bootstrap.theme.inc, line 122
Preprocessors and helper functions to make theming easier.

Code

function template_preprocess_views_bootstrap_carousel(array &$vars) {
  $view = $vars['view'];
  $vars['id'] = ViewsBootstrap::getUniqueId($view);
  $vars['attributes']['class'][] = 'views-bootstrap-media-object';
  $vars['attributes']['class'][] = 'media-list';

  // Carousel options.
  $vars['interval'] = $view->style_plugin->options['interval'];
  $vars['navigation'] = $view->style_plugin->options['navigation'];
  $vars['indicators'] = $view->style_plugin->options['indicators'];
  $vars['pause'] = $view->style_plugin->options['pause'] ? 'hover' : FALSE;
  $vars['wrap'] = $view->style_plugin->options['wrap'];

  // Carousel rows.
  if ($view->style_plugin->options['display'] != 'content' && $view->style_plugin
    ->usesFields()) {
    $image = $view->style_plugin->options['image'];
    $title = $view->style_plugin->options['title'];
    $description = $view->style_plugin->options['description'];
    $fieldLabels = $view->display_handler
      ->getFieldLabels(TRUE);
    $vars['display'] = 'fields';
  }
  else {
    $vars['display'] = 'content';
  }
  foreach ($vars['rows'] as $id => $row) {
    $vars['rows'][$id] = [];
    $row_attributes = [
      'class' => [],
    ];
    $class = $view->style_plugin->options['row_class'];
    if ($vars['display'] == 'fields') {
      $vars['rows'][$id]['image'] = $view->style_plugin
        ->getField($id, $image);
      $vars['rows'][$id]['title'] = $view->style_plugin
        ->getField($id, $title);
      $vars['rows'][$id]['description'] = $view->style_plugin
        ->getField($id, $description);

      // Add any additional fields to result.
      foreach (array_keys($fieldLabels) as $label) {
        if (!in_array($label, [
          $image,
          $title,
          $description,
        ])) {
          $vars['rows'][$id][$label] = $view->style_plugin
            ->getField($id, $label);
        }
      }
      $class = strip_tags($view->style_plugin
        ->tokenizeValue($class, $id));
      $class = Html::cleanCssIdentifier($class);
    }
    else {
      $vars['rows'][$id]['content'] = $row;
    }
    $classes = explode(' ', $class);
    foreach ($classes as &$class) {
      $class = Html::cleanCssIdentifier($class);
    }
    $row_class = array_filter($classes);
    if (!empty($row_class)) {
      $row_attributes['class'] = array_merge($row_attributes['class'], $row_class);
    }
    $vars['rows'][$id]['attributes'] = new Attribute($row_attributes);
  }
}