You are here

function theme_brightcove_field_image in Brightcove Video Connect 7.7

Same name and namespace in other branches
  1. 7.3 brightcove_field/brightcove_field.formatters.inc \theme_brightcove_field_image()
  2. 7.4 brightcove_field/brightcove_field.formatters.inc \theme_brightcove_field_image()
  3. 7.5 brightcove_field/brightcove_field.formatters.inc \theme_brightcove_field_image()
  4. 7.6 brightcove_field.formatters.inc \theme_brightcove_field_image()

Theme function to render brightcove images.

Parameters

$variables:

Return value

null|string

File

./brightcove_field.formatters.inc, line 74
Formatters for video field.

Code

function theme_brightcove_field_image($variables) {
  global $is_https;
  $output = NULL;
  $elements = $variables['elements'];
  $dialog_width = isset($elements['#width']) ? $elements['#width'] : BRIGHTCOVE_DEFAULT_VIDEO_WIDTH;
  $dialog_height = isset($elements['#height']) ? $elements['#height'] : BRIGHTCOVE_DEFAULT_VIDEO_HEIGHT;
  $entity_type = isset($elements['#entity_type']) ? $elements['#entity_type'] : NULL;
  $delta = isset($elements['#delta']) ? $elements['#delta'] : NULL;
  $field = isset($elements['#field']) ? $elements['#field'] : NULL;
  $entity = isset($elements['#entity']) ? $elements['#entity'] : NULL;
  $entity_info = isset($elements['#entity_type']) ? entity_get_info($entity_type) : NULL;
  $entity_id = is_null($entity) ? NULL : $entity->{$entity_info['entity keys']['id']};
  $image_type = empty($elements['#brightcove_image_type']) ? 'thumbnail' : $elements['#brightcove_image_type'];
  $type = $elements['#brightcove_widget_type'] == BRIGHTCOVE_VIDEO_WIDGET ? 'video' : 'playlist';
  if (!empty($elements["#{$type}"])) {

    /** @var \Brightcove\Object\Video\Video|\Brightcove\Object\Playlist $media */
    $media = $elements["#{$type}"];

    /** @var \Brightcove\Object\Video\Image[] $images */
    $images = $type === 'video' ? $media
      ->getImages() : [];
    if (empty($images[$image_type])) {
      $path = brightcove_get_default_image();
      $image_variables = [
        'path' => $path,
      ];
      if (!empty($elements['#brightcove_image_style'])) {
        $styled_path = $elements['#brightcove_image_style'] ? image_style_path($elements['#brightcove_image_style'], $path) : $path;
        $style = image_style_load($elements['#brightcove_image_style']);
        image_style_create_derivative($style, $path, $styled_path);
        $image_variables['path'] = image_style_path($elements['#brightcove_image_style'], $path);
      }
      else {
        $image_variables['width'] = 222;
        $image_variables['height'] = 222;
      }
      $image = theme('image', $image_variables);
    }
    else {
      $cid = "brightcove:video:{$media->getId()}:{$elements['#element']['bcid']}:{$image_type}";
      $cache = brightcove_cache_get($cid);
      if (!empty($cache)) {
        $remote_file = $cache;
      }
      else {
        $image = $images[$image_type];
        $image_src = $image
          ->getSrc();
        foreach ($image
          ->getSources() as $source) {
          if ($is_https && strpos($source['src'], 'https://') === 0) {
            $image_src = $source['src'];
            break;
          }
        }
        $remote_file = brightcove_remote_image($image_src);
        if ($remote_file) {
          brightcove_cache_set($cid, $remote_file);
        }
      }
      if ($elements['#brightcove_image_style']) {
        $image = theme('image_style', [
          'style_name' => $elements['#brightcove_image_style'],
          'path' => $remote_file,
        ]);
      }
      else {
        $image = theme('image', [
          'path' => $remote_file,
        ]);
      }
    }
    if ($elements['#brightcove_image_link'] == 'none' || empty($elements['#brightcove_image_link'])) {
      return $image;
    }
    elseif ($elements['#brightcove_image_link'] == 'dialog') {
      $destination = "brightcove_dialog/nojs/{$type}/{$dialog_width}/{$dialog_height}/{$entity_type}/{$entity_id}/{$field['field_name']}/{$delta}";
      $output = l($image, $destination, [
        'attributes' => [
          'rel' => $field['field_name'],
          'class' => [
            $field['field_name'],
            'use-ajax',
          ],
          'title' => $elements["#{$type}"] ? check_plain($media
            ->getName()) : '',
        ],
        'html' => TRUE,
      ]);
    }
    elseif ($elements['#brightcove_image_link'] == 'entity' && !is_null($entity_info)) {
      $uri = $entity_info['uri callback']($entity);
      $output = l($image, $uri['path'], [
        'attributes' => [
          'class' => [
            $field['field_name'],
          ],
          'title' => $elements["#{$type}"] ? check_plain($media
            ->getName()) : '',
        ],
        'html' => TRUE,
      ]);
    }
  }
  return $output;
}