You are here

function scald_image_scald_prerender in Scald: Media Management made easy 7

Same name and namespace in other branches
  1. 6 scald_image/scald_image.module \scald_image_scald_prerender()

Implements hook_scald_prerender().

File

modules/providers/scald_image/scald_image.module, line 213
Scald Image is a Scald Atom Provider for images.

Code

function scald_image_scald_prerender($atom, $context, $options, $mode) {
  $config = scald_context_config_load($context);

  // Find out which transcoder is in use, and checks if it's
  // one of the transcoder provided by Scald Image.
  $style_name = NULL;
  if ($transcoder = $config->transcoder[$atom->type]['*']) {

    // Image style support.
    if (preg_match('/^style-(.*)$/', $transcoder, $match)) {
      $style_name = $match[1];
    }
    elseif (preg_match('/^group-(.*)$/', $transcoder, $match) && module_exists('picture')) {
      $mappings = picture_mapping_load($match[1]);
    }
  }
  if ($mode == 'transcoder') {

    // Scald Image can only do 1:1 transcoding. For Picture integration, it is
    // done in the Atom mode to avoid duplicate code from Picture module.
    if (empty($style_name)) {
      return;
    }
    $preset = image_style_load($style_name);
    if (!empty($atom->file_source)) {
      $atom->file_transcoded = image_style_path($preset['name'], $atom->file_source);
      $atom->rendered->file_transcoded_url = image_style_url($preset['name'], $atom->file_source);
    }
  }
  elseif ($mode == 'player') {
    $settings = $config->player[$atom->type]['settings'];
    $classes = array_merge(array(
      'scald-atom',
      'scald-atom-image',
    ), explode(' ', check_plain($settings['classes'])));
    $caption = token_replace($settings['caption'], array(
      'atom' => $atom,
    ));
    $atom->rendered->player = '
      <figure class="' . implode(' ', $classes) . '">
        ' . $atom->rendered->player . '
        <figcaption>' . filter_xss_admin($caption) . '</figcaption>
      </figure>
    ';
  }
  elseif ($mode == 'atom') {

    // Default attributes, which can be overridden by field settings.
    $attributes = array(
      'alt' => $atom->title,
      'title' => $atom->title,
    );
    $langcode = field_language('scald_atom', $atom, 'scald_thumbnail');
    foreach (array(
      'alt',
      'title',
      'width',
      'height',
    ) as $attribute_name) {
      if (isset($atom->scald_thumbnail[$langcode][0][$attribute_name]) && $atom->scald_thumbnail[$langcode][0][$attribute_name]) {
        $attributes[$attribute_name] = $atom->scald_thumbnail[$langcode][0][$attribute_name];
      }
    }
    if (!empty($style_name)) {
      $atom->rendered->player = theme('image_style', array(
        'path' => $atom->file_source,
        'style_name' => $style_name,
      ) + $attributes);
    }
    elseif (isset($mappings)) {
      foreach ($mappings->mapping as $breakpoint_name => $multipliers) {
        if (!empty($multipliers)) {
          foreach ($multipliers as $multiplier => $image_style) {
            if (!$image_style) {
              continue;
            }

            // $image_style is machine name in Picture 1.x and an array in
            // Picture 2.x.
            if (is_array($image_style) && $image_style['mapping_type'] === '_none') {
              continue;
            }
            $fallback_image_style = is_array($image_style) ? $image_style['image_style'] : $image_style;
            break 2;
          }
        }
      }

      // The fallback_image_style is the first image style we find, and so if it
      // is empty then we do not have any image style.
      if (!empty($fallback_image_style)) {
        $atom->rendered->player = theme('picture', array(
          'uri' => $atom->file_source,
          'style_name' => $fallback_image_style,
          'breakpoints' => $mappings->mapping,
        ) + $attributes);
      }
    }
    else {
      $path = empty($atom->rendered->file_transcoded_url) ? $atom->file_source : $atom->rendered->file_transcoded_url;
      $atom->rendered->player = theme('image', array(
        'path' => $path,
      ) + $attributes);
    }
    if (!empty($options['link'])) {
      $link_options = array(
        'html' => TRUE,
      );
      if (!empty($options['linkTarget'])) {
        $link_options += array(
          'attributes' => array(
            'target' => $options['linkTarget'],
          ),
        );
      }
      $atom->rendered->player = l($atom->rendered->player, urldecode($options['link']), $link_options);
    }
  }
}