123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- * @file
- * Adds a handler for Facebook videos to Video Embed Field.
- *
- * @see video_embed_field.api.php for more documentation
- */
- /**
- * Implements hook_video_embed_handler_info().
- */
- function video_embed_facebook_video_embed_handler_info() {
- $handlers = array();
- $handlers['facebook'] = array(
- 'title' => 'Facebook Video',
- 'function' => 'video_embed_facebook_handle_video',
- 'thumbnail_function' => 'video_embed_facebook_handle_thumbnail',
- 'thumbnail_default' => drupal_get_path('module', 'video_embed_facebook') . '/img/facebook.jpg',
- 'form' => 'video_embed_facebook_form',
- 'form_validate' => 'video_embed_field_handler_youtube_form_validate',
- 'domains' => array(
- 'facebook.com',
- ),
- 'defaults' => array(
- 'width' => 640,
- 'height' => 360,
- ),
- );
- return $handlers;
- }
- /**
- * Defines the form elements for the Facebook videos configuration form.
- *
- * @param array $defaults
- * The form default values.
- *
- * @return array
- * The provider settings form array.
- */
- function video_embed_facebook_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 Facebook 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_facebook_form_validate($element, &$form_state, $form) {
- video_embed_field_validate_dimensions($element);
- }
- /**
- * Handler for Facebook 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_facebook_handle_video($url, $settings) {
- $id = _video_embed_facebook_get_video_id($url);
- if ($id) {
- // Our embed code.
- $embed='<iframe src="//www.facebook.com/video/embed?video_id=!id" width="!width" height="!height"></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 Facebook videos.
- *
- * @param string $url
- * The video URL.
- *
- * @return array
- * The video thumbnail information.
- */
- function video_embed_facebook_handle_thumbnail($url) {
- $id = _video_embed_facebook_get_video_id($url);
- return array(
- 'id' => $id,
- 'url' => 'https://graph.facebook.com/' . $id . '/picture',
- );
- }
- /**
- * Helper function to get the Facebook 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_facebook_get_video_id($url) {
- // Parse_url is an easy way to break a url into its components.
- $matches = array();
- preg_match('/(.*)?[v|video_id]=([^&#]*)/', $url, $matches);
- // If the v or video_id get parameters are set, return it.
- if ($matches && !empty($matches[2])) {
- return $matches[2];
- }
- // Otherwise return false.
- return FALSE;
- }
|