You are here

function scald_galleria_scald_prerender in Scald: Gallery 7.2

Implements hook_scald_prerender().

File

scald_galleria/scald_galleria.module, line 157
Scald Galleria is a player for Scald Gallery.

Code

function scald_galleria_scald_prerender($atom, $context, $options, $mode) {
  if ($mode == 'player') {
    if (!($items = field_get_items('scald_atom', $atom, 'gallery_items'))) {
      return;
    }

    // Galleria display settings.
    $players = scald_players();
    $player = $players['galleria'];
    $config = scald_context_config_load($context);
    $settings = isset($config->player[$atom->type]['settings']) ? $config->player[$atom->type]['settings'] : array();
    if (isset($player['settings'])) {

      // Add default settings.
      $settings += $player['settings'];
    }
    $normal_style = $settings['normal_style'];
    $thumb_style = $settings['thumb_style'];
    $big_style = $settings['big_style'];
    if (empty($config->data['width'])) {
      $width = $settings['width'];
    }
    else {
      $width = $config->data['width'];
    }
    if (is_numeric($width)) {
      $width .= 'px';
    }
    if (empty($config->data['height'])) {
      $height = $settings['height'];
    }
    else {
      $height = $config->data['height'];
    }
    if (is_numeric($height)) {
      $height .= 'px';
    }
    $galleria_opts = array(
      'width' => $width,
      'height' => $height,
      'theme' => $settings['theme'],
      'options' => $settings['options'],
      'fullscreen_link' => $settings['fullscreen_link'],
    );

    // Load URLs to thumb, normal and fullscreen images.
    $images = array();
    foreach ($items as $item) {
      if (!($item_atom = scald_atom_load($item['sid']))) {
        continue;
      }
      $thumbnail = field_get_items('scald_atom', $item_atom, 'scald_thumbnail');
      if (!empty($thumbnail[0]['fid'])) {
        $file = file_load($thumbnail[0]['fid']);
        if ($file) {
          $image = array(
            'thumb' => $thumb_style == 'original' ? file_create_url($file->uri) : image_style_url($thumb_style, $file->uri),
            'normal' => $normal_style == 'original' ? file_create_url($file->uri) : image_style_url($normal_style, $file->uri),
            'big' => $big_style == 'original' ? file_create_url($file->uri) : image_style_url($big_style, $file->uri),
            'title' => check_plain($item_atom->title),
          );
          if (!empty($thumbnail[0]['alt'])) {
            $image['alt_attr'] = check_plain($thumbnail[0]['alt']);
          }
          if (!empty($thumbnail[0]['title'])) {
            $image['title_attr'] = check_plain($thumbnail[0]['title']);
          }
          $field = field_get_items('scald_atom', $item_atom, 'scald_authors');
          if (!empty($field)) {
            $authors = field_view_value('scald_atom', $item_atom, 'scald_authors', $field[0], array(
              'type' => 'taxonomy_term_reference_plain',
            ));
            $image['authors'] = $authors['#markup'];
          }
          if (isset($atom->data['items'])) {
            $item_data = $atom->data['items'][$item['sid']];
            if (!empty($item_data['title_overriden'])) {
              $image['title'] = $item_data['title'];
            }
            $image['description'] = $item_data['description'];
          }

          // Hardcode for some video providers. However the $atom->file_source
          // should be used.
          if ($item_atom->provider == 'scald_youtube') {
            $image['iframe'] = 'https://www.youtube.com/embed/' . $item_atom->base_id;
          }
          elseif ($item_atom->provider == 'scald_vimeo') {
            $image['iframe'] = 'https://player.vimeo.com/video/' . $item_atom->base_id;
          }
          elseif ($item_atom->provider == 'scald_dailymotion') {
            $image['iframe'] = 'https://www.dailymotion.com/embed/video/' . $item_atom->base_id;
          }
          elseif ($item_atom->type == 'video') {
            $image['iframe'] = file_create_url($item_atom->file_source);
          }
          $images[] = $image;
        }
      }
    }
    $galleria_id = 'scald-gallery-' . $atom->sid;
    $options = empty($galleria_opts['options']) ? '' : ', ' . $galleria_opts['options'];

    // Prevents multiple loading of the same theme.
    static $processed_themes = array();
    $inline_js = '';
    if (!in_array($galleria_opts['theme'], $processed_themes)) {
      $processed_themes[] = $galleria_opts['theme'];
      $inline_js = "Galleria.loadTheme('" . url(scald_galleria_get_library_file($galleria_opts['theme']), array(
        'language' => (object) array(
          'language' => LANGUAGE_NONE,
        ),
      )) . "');";
    }
    $inline_js .= "if (jQuery('." . $galleria_id . "').length > 0) { Galleria.run('." . $galleria_id . "'" . $options . "); }";
    $atom->rendered->player = array(
      '#theme' => 'scald_galleria',
      '#atom' => $atom,
      '#images' => $images,
      '#options' => $galleria_opts,
      '#attached' => array(
        'js' => array(
          scald_galleria_get_library_file() => array(),
          $inline_js => array(
            'type' => 'inline',
            'scope' => 'footer',
          ),
          // @codingStandardsIgnoreStart
          drupal_get_path('module', 'scald_galleria') . '/scald_galleria.js',
        ),
      ),
    );
    if (!empty($settings['fullscreen_link'])) {
      $fullscreen_toggle_js = drupal_get_path('module', 'scald_galleria') . '/scald_galleria_fullscreen_toggle.js';
      $atom->rendered->player['#attached']['js'][$fullscreen_toggle_js] = array();
    }
  }
}