You are here

function template_preprocess_jquery_carousel_field_formatter in jQuery carousel 8

Theme callback for jQuery carousel field formatter.

File

./jquery_carousel.module, line 55
Provide jquery carousel style plugin for views.

Code

function template_preprocess_jquery_carousel_field_formatter(&$vars) {
  if (is_array($vars['elements']) && count($vars['elements']) > 1) {
    $items = [];
    $selector_class = new Attribute([
      'class' => $vars['settings']['selector'],
    ]);
    foreach ((array) $vars['elements'] as $key => $elem) {
      if (isset($vars['settings']['style_name'])) {
        $items[$key] = [
          '#theme' => 'image_style',
          '#style_name' => $vars['settings']['style_name'],
        ];
      }
      else {
        $items[$key] = [
          '#theme' => 'image',
        ];
      }
      $items[$key]['#title'] = $elem->title;
      if (($entity = $elem->entity) && empty($elem->uri)) {
        $items[$key]['#uri'] = $entity
          ->getFileUri();
      }
      else {
        $items[$key]['#uri'] = $elem
          ->getFileUri();
      }
      foreach ([
        'width',
        'height',
        'alt',
      ] as $prop) {
        $items[$key]["#{$prop}"] = $elem->{$prop};
      }
    }
    $output = [
      '#theme' => 'item_list',
      '#items' => $items,
    ];
    _jquery_carousel_settings_format($vars['settings']);
    _jquery_carousel_include_css_js($output, $vars['settings']);
    return '<div ' . $selector_class . '>' . \Drupal::service('renderer')
      ->render($output, FALSE) . '</div>';
  }
  else {
    $elem = $vars['elements'][0];
    if (isset($vars['settings']['style_name'])) {
      $output = [
        '#theme' => 'image_style',
        '#style_name' => $vars['settings']['style_name'],
      ];
    }
    else {
      $output = [
        '#theme' => 'image',
      ];
    }
    $output['#title'] = $elem->title;
    if (($entity = $elem->entity) && empty($elem->uri)) {
      $output['#uri'] = $entity
        ->getFileUri();
    }
    else {
      $output['#uri'] = $elem
        ->getFileUri();
    }
    foreach ([
      'width',
      'height',
      'alt',
    ] as $prop) {
      $output["#{$prop}"] = $elem->{$prop};
    }
    return \Drupal::service('renderer')
      ->render($output, FALSE);
  }
}