You are here

video_embed_youku.module in Video Embed Youku 7

Adds a handler for Youku videos to Video Embed Field.

See also

video_embed_field.api.php for more documentation

File

video_embed_youku.module
View source
<?php

/**
 * @file
 * Adds a handler for Youku videos to Video Embed Field.
 *
 * @see video_embed_field.api.php for more documentation
 */

/**
 * Implements hook_video_embed_handler_info().
 */
function video_embed_youku_video_embed_handler_info() {
  $handlers = array();
  $handlers['youku'] = array(
    'title' => 'Youku Video',
    'function' => 'video_embed_youku_handle_video',
    'thumbnail_function' => 'video_embed_youku_handle_thumbnail',
    'thumbnail_default' => drupal_get_path('module', 'video_embed_youku') . '/img/youku.jpg',
    'form' => 'video_embed_youku_form',
    'form_validate' => 'video_embed_field_handler_youku_form_validate',
    'domains' => array(
      'youku.com',
      'www.youku.com',
      'v.youku.com',
    ),
    'defaults' => array(
      'width' => 640,
      'height' => 360,
    ),
  );
  return $handlers;
}

/**
 * Defines the form elements for the Youku videos configuration form.
 *
 * @param array $defaults
 *   The form default values.
 *
 * @return array
 *   The provider settings form array.
 */
function video_embed_youku_form($defaults) {
  $form = array();
  $form['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Player Width'),
    '#description' => t('The width of the player.'),
    '#default_value' => $defaults['width'],
  );
  $form['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Player Height'),
    '#description' => t('The height of the player.'),
    '#default_value' => $defaults['height'],
  );
  return $form;
}

/**
 * Validates the form elements for the Youku video configuration form.
 *
 * @param array $element
 *   The form element to validate.
 * @param array $form_state
 *   The form to validate state.
 * @param array $form
 *   The form to validate structure.
 */
function video_embed_field_handler_youku_form_validate($element, &$form_state, $form) {
  video_embed_field_validate_dimensions($element);
}

/**
 * Handler for Youku videos.
 *
 * @param string $url
 *   The video URL.
 * @param array $settings
 *   The settings array.
 *
 * @return string|bool
 *   The video iframe, or FALSE in case the ID can't be retrieved from the URL.
 */
function video_embed_youku_handle_video($url, $settings) {
  $id = _video_embed_youku_get_video_id($url);
  if ($id) {

    // Our embed code.
    $embed = '<iframe src="https://player.youku.com/embed/!id" width="!width" height="!height" frameborder=0 allowfullscreen></iframe> ';

    // Use format_string to replace our placeholders with the settings values.
    $embed = format_string($embed, array(
      '!id' => $id,
      '!width' => $settings['width'],
      '!height' => $settings['height'],
    ));
    $video = array(
      '#markup' => $embed,
    );
    return $video;
  }
  return FALSE;
}

/**
 * Gets the thumbnail url for Youku videos.
 *
 * @param string $url
 *   The video URL.
 *
 * @return array
 *   The video thumbnail information.
 */
function video_embed_youku_handle_thumbnail($url) {
  $id = _video_embed_youku_get_video_id($url);
  $id = str_replace('.html', '', $id);
  $link = 'https://api.youku.com/videos/show.json?client_id=' . variable_get('video_embed_field_youku_client_id', '8d025b9c897b22a8') . '&video_id=' . $id;
  $http = drupal_http_request($link);
  $json = drupal_json_decode($http->data);
  return array(
    'id' => $json['id'],
    'url' => $json['bigThumbnail'],
  );
}

/**
 * Helper function to get the Youku video's id.
 *
 * @param string $url
 *   The video URL.
 *
 * @return string|bool
 *   The video ID, or FALSE in case the ID can't be retrieved from the URL.
 */
function _video_embed_youku_get_video_id($url) {
  $id = FALSE;

  // Parse_url is an easy way to break a url into its components.
  $parsed = parse_url($url);
  $path = $parsed['path'];
  $parts = explode('/', $path);
  foreach ($parts as $part) {
    if (strstr($part, 'id_')) {
      $id = str_replace('id_', '', $part);
      $id = str_replace('.html', '', $id);
      return $id;
    }
  }
  return $id;
}

/**
 * Implements hook_form_alter().
 */
function video_embed_youku_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'video_embed_field_settings_form') {
    $form['video_embed_field_youku_client_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Youku Client Id'),
      '#default_value' => variable_get('video_embed_field_youku_client_id', ''),
    );
  }
}

Functions

Namesort descending Description
video_embed_field_handler_youku_form_validate Validates the form elements for the Youku video configuration form.
video_embed_youku_form Defines the form elements for the Youku videos configuration form.
video_embed_youku_form_alter Implements hook_form_alter().
video_embed_youku_handle_thumbnail Gets the thumbnail url for Youku videos.
video_embed_youku_handle_video Handler for Youku videos.
video_embed_youku_video_embed_handler_info Implements hook_video_embed_handler_info().
_video_embed_youku_get_video_id Helper function to get the Youku video's id.