You are here

function _jcarousel_generate_attributes in jCarousel 8.4

Same name and namespace in other branches
  1. 8.5 jcarousel.module \_jcarousel_generate_attributes()

Helper to build attributes array for carousel items.

Parameters

array $items: The carousel items.

array $options: The options for carousel.

Return value

array An array of attributes.

2 calls to _jcarousel_generate_attributes()
template_preprocess_jcarousel in ./jcarousel.module
Preprocess function for jcarousel.html.twig.
template_preprocess_views_view_jcarousel in ./jcarousel.module

File

./jcarousel.module, line 604
Provides integration with 3rd party modules and the jCarousel library.

Code

function _jcarousel_generate_attributes($items, $options) {
  $attributes = [];
  $attributes['attributes'] = new Attribute();
  $attributes['attributes_wrapper'] = new Attribute();
  $attributes['attributes_previous'] = new Attribute();
  $attributes['attributes_next'] = new Attribute();
  $attributes['attributes_content'] = new Attribute();

  // Build the list of classes for the carousel.
  $attributes['attributes']
    ->addClass('jcarousel');
  $attributes['attributes']
    ->setAttribute('data-jcarousel', 'true');

  // Scroll all visible elements at a time if scroll = 'auto'
  $scroll = !empty($options['scroll']) ? $options['scroll'] : $options['visible'];
  if ($options['auto'] > 0) {
    $attributes['attributes']
      ->setAttribute('data-autoscroll-interval', $options['auto'] * 1000);
    $attributes['attributes']
      ->setAttribute('data-autoscroll-autostart', 'true');
    $attributes['attributes']
      ->setAttribute('data-autoscroll-target', '+=' . $scroll);
    if ($options['autoPause'] && empty($options['events']['jcarousel']['jcarousel:createend'])) {
      $options['events']['jcarousel']['jcarousel:createend'] = 'jcarousel.autoPauseCallback';
    }
  }
  if ($options['responsive'] && empty($options['events']['jcarousel']['jcarousel:reload'])) {
    $options['vertical'] = FALSE;
    unset($options['visible']);
    $options['events']['jcarousel']['jcarousel:reload'] = 'jcarousel.reloadCallback';
  }
  if (!empty($options['events']['jcarousel'])) {
    $attributes['attributes']
      ->setAttribute('data-events', Json::encode($options['events']['jcarousel']));
  }
  if (isset($options['wrap'])) {
    $attributes['attributes']
      ->setAttribute('data-wrap', $options['wrap']);
  }
  if (isset($options['ajax']) && $options['ajax']) {
    $attributes['attributes']
      ->setAttribute('data-ajax', 'true');
    $attributes['attributes']
      ->setAttribute('data-ajax-path', $options['ajax_path']);
    if ($options['preload_page'] != 0) {
      $attributes['attributes']
        ->setAttribute('data-preload-page', $options['preload_page']);
    }
  }
  if (isset($options['vertical']) && $options['vertical']) {
    $attributes['attributes']
      ->setAttribute('data-vertical', 'true');
  }
  $orientation = !empty($options['vertical']) && $options['vertical'] ? 'vertical' : 'horizontal';
  $attributes['attributes_wrapper']
    ->addClass('jcarousel-wrapper');
  if (!empty($options['visible'])) {
    $attributes['attributes_wrapper']
      ->addClass('jcarousel-visible-' . $options['visible']);
  }
  if (!empty($options['skin'])) {
    $attributes['attributes_wrapper']
      ->addClass('jcarousel-skin-' . $options['skin']);
  }
  $attributes['attributes_content']
    ->addClass('jcarousel-container-' . $orientation);
  $attributes['attributes_previous']
    ->setAttribute('data-jcarousel-control', 'true');
  if (!empty($options['events']['jcarouselcontrol'])) {
    $attributes['attributes_previous']
      ->setAttribute('data-events', Json::encode($options['events']['jcarouselcontrol']));
  }
  $attributes['attributes_previous']
    ->setAttribute('data-target', '-=' . $scroll);
  $attributes['attributes_previous']
    ->addClass('jcarousel-control-prev');
  $attributes['attributes_next']
    ->setAttribute('data-jcarousel-control', 'true');
  if (!empty($options['events']['jcarouselcontrol'])) {
    $attributes['attributes_next']
      ->setAttribute('data-events', Json::encode($options['events']['jcarouselcontrol']));
  }
  $attributes['attributes_next']
    ->setAttribute('data-target', '+=' . $scroll);
  $attributes['attributes_next']
    ->addClass('jcarousel-control-next');
  $attributes['attributes_wrapper']
    ->addClass('jcarousel-' . $orientation);

  // Give each item a class to identify where in the carousel it belongs.
  foreach ($items as $id => $row) {
    $row_attribute = new Attribute();
    $attributes['attributes_row'][$id] = $row_attribute;
  }
  return $attributes;
}