You are here

video_embed_dailymotion.module in Video Embed Dailymotion 7

Add a handler for dailymotion videos to Video Embed Field with this module you can add videos from dailymotion.com

File

video_embed_dailymotion.module
View source
<?php

/**
 * @file
 * Add a handler for dailymotion videos to Video Embed Field
 * with this module you can add videos from dailymotion.com
 */

/**
 * Implements hook_video_embed_handler_info().
 */
function video_embed_dailymotion_video_embed_handler_info() {
  $handlers = array();
  $handlers['dailymotion'] = array(
    'title' => 'dailymotion Video',
    'function' => 'video_embed_dailymotion_handle_video',
    'thumbnail_function' => 'video_embed_dailymotion_handle_thumbnail',
    'form' => 'video_embed_dailymotion_form',
    'domains' => array(
      'dailymotion.com',
    ),
    // Defaults are the defaults to provide to your form.
    'defaults' => array(
      'width' => '640px',
      'height' => '480px',
      'allowfullscreen' => TRUE,
      'allowautoplay' => FALSE,
    ),
  );
  return $handlers;
}

/**
 * Provide a form to configure out video settings.
 *
 * @param array $defaults
 *   default/current values for your provider, the currently saved settings
 *   with empty values filled with the defaults provided in info hook
 *
 * @return array
 *   form as defined by forms api
 */
function video_embed_dailymotion_form($defaults) {
  $form = array();
  $form['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Player Height'),
    '#description' => t('The height of the player.'),
    '#default_value' => $defaults['height'],
    '#element_validate' => array(
      '_video_embed_dailymotion_element_validate',
    ),
  );
  $form['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Player Width'),
    '#description' => t('The width of the player.'),
    '#default_value' => $defaults['width'],
    '#element_validate' => array(
      '_video_embed_dailymotion_element_validate',
    ),
  );
  $form['allowfullscreen'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow Fullscreen'),
    '#desecription' => t('This will allow the video to be fullscreened.'),
    '#default_value' => $defaults['allowfullscreen'],
  );
  $form['allowautoplay'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow Autoplay'),
    '#desecription' => t('This will allow the video to autoplay.'),
    '#default_value' => $defaults['allowautoplay'],
  );
  return $form;
}

/**
 * This is the video handler (the 'function' key from handler_info).
 *
 * @param string $url
 *   the full video url
 *
 * @param array $settings
 *   an associative array of this handlers settings, from the settings form
 *
 * @return string
 *   the embed code for the video
 */
function video_embed_dailymotion_handle_video($url, $settings) {
  $id = _video_embed_dailymotion_get_video_id($url);
  if ($id) {
    $embed = '<object width="!width" height="!height">
    <param name="movie" value="http://www.dailymotion.com/swf/video/!id?autoPlay=!autoplay"></param>
    <param name="allowFullScreen" value="!fullscreen"></param>
    <param name="allowScriptAccess" value="always"></param>
    <embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/!id?autoPlay=!autoplay"' . 'width="!width" height="!height" allowfullscreen="!fullscreen" allowscriptaccess="always"></embed>
    </object>';
    $embed = format_string($embed, array(
      '!id' => $id,
      '!fullscreen' => $settings['allowfullscreen'] ? 'true' : 'false',
      '!autoplay' => $settings['allowautoplay'] ? 'true' : 'false',
      '!width' => $settings['width'],
      '!height' => $settings['height'],
    ));
    $video = array(
      '#markup' => $embed,
    );
    return $video;
  }

  // Return an empty string if there is no id, so we don't have broken embeds.
  return '';
}

/**
 * Retreive the thumbnail for the dailymotion video.
 *
 * @param string $url
 *   the url of the video as entered by the user
 *
 * @return array
 *   an array with the keys:
 *     'id' => an id for the video which is unique to your provider
 *     'url' => the url to retrieve the thumbnail from
 */
function video_embed_dailymotion_handle_thumbnail($url) {
  $id = _video_embed_dailymotion_get_video_id($url);
  $thumbnail = 'http://www.dailymotion.com/thumbnail/video/' . $id;
  return array(
    'id' => $id,
    'url' => $thumbnail,
  );
}

/**
 * Helper function to take a dailymotion video url and return its id.
 *
 * @param string $url
 *   the full dailymotion video url
 *
 * @return bool
 *   the id for the video or FALSE if there is no match
 */
function _video_embed_dailymotion_get_video_id($url) {
  $matches = array();
  $patterns = array(
    '@dailymotion\\.com/swf/([^"\\&]+)@i',
    '@dailymotion\\.com/video/([^/_]+)_@i',
  );
  foreach ($patterns as $pattern) {
    preg_match($pattern, $url, $matches);
    if ($matches && !empty($matches[1])) {
      return $matches[1];
    }
  }
  return FALSE;
}

/**
 * Function for validating width and height in setting form.
 */
function _video_embed_dailymotion_element_validate($element, &$form_state) {
  $check = _video_embed_dailymotion_check_input($element['#value']);
  if (!empty($element['#value']) && !$check) {
    form_error($element, t('The "!name" option must contain a valid value. You may either leave the text field empty or enter a string like "640px" or "640"', array(
      '!name' => t($element['#title']),
    )));
  }
}

/**
 * Helper function to check for a valid input in height and width textfields.
 *
 * @param string $input
 *   the string that contains input from setting form
 *
 * @return bool
 *   if input is valid returns true else false
 */
function _video_embed_dailymotion_check_input($input) {
  $input = check_plain($input);
  if ($input == '') {
    return FALSE;
  }
  $size = drupal_strlen($input);
  $cur_char = '';
  for ($i = 0; $i < $size; $i++) {
    $cur_char = drupal_substr($input, $i, 1);
    if ($cur_char >= '0' && $cur_char <= '9') {
      continue;
    }
    break;
  }
  if ($i == $size) {
    return TRUE;
  }
  elseif ($i > 0 && $i == $size - 2) {
    if (drupal_substr($input, $i, 2) == 'px') {
      return TRUE;
    }
  }
  return FALSE;
}

Functions

Namesort descending Description
video_embed_dailymotion_form Provide a form to configure out video settings.
video_embed_dailymotion_handle_thumbnail Retreive the thumbnail for the dailymotion video.
video_embed_dailymotion_handle_video This is the video handler (the 'function' key from handler_info).
video_embed_dailymotion_video_embed_handler_info Implements hook_video_embed_handler_info().
_video_embed_dailymotion_check_input Helper function to check for a valid input in height and width textfields.
_video_embed_dailymotion_element_validate Function for validating width and height in setting form.
_video_embed_dailymotion_get_video_id Helper function to take a dailymotion video url and return its id.