You are here

function template_preprocess_flexslider in Flex Slider 8.2

Prepares variables for flexslider template.

Default template: flexslider.html.twig.

File

templates/flexslider.theme.inc, line 21
Theming functions for the flexslider module.

Code

function template_preprocess_flexslider(&$variables) {

  // Reference configuration variables.
  $settings =& $variables['flexslider']['settings'];
  $optionset =& $settings['optionset'];
  $items =& $variables['flexslider']['items'];

  // Set the default container type.
  if (empty($settings['type'])) {
    $settings['type'] = 'ul';
  }

  // Load the selected optionset.
  if (!empty($optionset)) {
    $optionset = Flexslider::load($optionset);
  }

  // Check if an optionset was loaded.
  if (is_null($optionset)) {

    // Fall back to 'default' options.
    $options = FlexsliderDefaults::defaultOptions();
    \Drupal::logger('flexslider')
      ->warning('Fallback to default optionset.', []);
  }
  else {
    $options = $optionset
      ->getOptions();
  }

  // Configure attributes for containing elements.
  $attributes = $variables['attributes'];

  // Merge with defined attributes.
  if (isset($settings['attributes']) && is_array($settings['attributes'])) {
    $attributes = NestedArray::mergeDeep($attributes, $settings['attributes']);
  }

  // Set the ID for each flexslider instance if none is provided.
  if (empty($attributes['id'])) {
    $flexslider_id =& drupal_static('flexslider_id', 0);
    $attributes['id'] = 'flexslider-' . ++$flexslider_id;
  }

  // Add the namespace to any classes.
  // @todo figure out what this is supposed to do
  if (!empty($attributes['class']) && !empty($options['namespace'])) {
    foreach ($attributes['class'] as $key => $value) {
      $attributes['class'][$key] = $options['namespace'] . $value;
    }
  }

  // Add the flexslider class to be namespaced.
  $attributes['class'][] = 'flexslider';

  // Add the optionset name as a class to the container.
  $attributes['class'][] = 'optionset-' . Html::getClass($optionset
    ->id());

  // Add the image style name as a class to the container.
  if (!empty($settings['image_style'])) {
    $attributes['class'][] = 'imagestyle-' . Html::getClass($settings['image_style']);
  }

  // Pass attributes to twig.
  $variables['attributes'] = $attributes;

  // Add the list render array.
  $variables['content']['list'] = [
    '#theme' => 'flexslider_list',
    '#items' => $items,
    '#settings' => $settings,
  ];

  // Finally, add the configuration to the page.
  $attached = flexslider_add($variables['attributes']['id'], $variables['flexslider']['settings']['optionset']);
  $variables['#attached'] = $attached;
}