You are here

function youtube_tokens in YouTube Field 7

Same name and namespace in other branches
  1. 8 youtube.module \youtube_tokens()

Implements hook_tokens().

See also

youtube_tokens_info_alter()

File

./youtube.inc, line 384
YouTube field helper functions.

Code

function youtube_tokens($type, $tokens, array $data = array(), array $options = array()) {
  global $base_url;
  global $base_path;
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      if (!strpos($name, '__youtube_')) {

        // This isn't a YouTube Field token!
        continue;
      }
      $token_pieces = explode('__', $name);
      if (count($token_pieces) != 2) {
        continue;
      }
      $field_name = $token_pieces[0];
      $token_name = $token_pieces[1];
      switch ($token_name) {
        case 'youtube_video_url':
          $replacements[$original] = '';
          if ($field_value = field_get_items('node', $node, $field_name)) {
            $replacements[$original] = 'http://www.youtube.com/watch?v=' . $field_value[0]['video_id'];
          }
          break;
        case 'youtube_image_url':
          $replacements[$original] = '';
          if ($field_value = field_get_items('node', $node, $field_name)) {
            $video_id = $field_value[0]['video_id'];
            $file_uri = youtube_build_thumbnail_uri($video_id);
            $file_url = file_create_url($file_uri);
            if (file_exists($file_uri) || youtube_get_remote_image($video_id)) {
              $replacements[$original] = $file_url;
              if ($style_name = variable_get('youtube_thumb_token_image_style', NULL)) {
                $derivative_uri = image_style_path($style_name, $file_uri);
                if (!file_exists($derivative_uri)) {
                  $image_style = image_style_load($style_name);
                  image_style_create_derivative($image_style, $file_uri, $derivative_uri);
                }
                $replacements[$original] = file_create_url($derivative_uri);
              }
            }
          }
          break;
      }
    }
  }
  return $replacements;
}