video_embed_field.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <?php
  2. /**
  3. * Provides a simple field for easily embedding videos from youtube or vimeo
  4. *
  5. * This module is not intended to replace media or video - it does not allow for any local storage of videos, custom players or anything else
  6. * It simply allows users to embed videos from youtube and vimeo - and provides a hook to allow other modules to provide more providers.
  7. *
  8. * Uses CTools Export UI to manage settings. @see ./plugins/export_ui/video_embed_field_export_ui.inc
  9. *
  10. * @author jec006, jdelaune
  11. */
  12. // Load all Field module hooks.
  13. module_load_include('inc', 'video_embed_field', 'video_embed_field.field');
  14. // Load the admin forms
  15. module_load_include('inc', 'video_embed_field', 'video_embed_field.admin');
  16. // Load our default handlers
  17. module_load_include('inc', 'video_embed_field', 'video_embed_field.handlers');
  18. /**
  19. * Implementation of hook_ctools_plugin_directory().
  20. */
  21. function video_embed_field_ctools_plugin_directory($module, $type) {
  22. // Load the export_ui plugin.
  23. if ($type =='export_ui') {
  24. return 'plugins/export_ui';
  25. }
  26. }
  27. /**
  28. * Implementation of hook_ctools_plugin_api().
  29. *
  30. * Tell CTools that we support the default_mymodule_presets API.
  31. */
  32. function video_embed_field_ctools_plugin_api($owner, $api) {
  33. if ($owner == 'video_embed_field' && $api == 'default_video_embed_styles') {
  34. return array('version' => 1);
  35. }
  36. }
  37. /**
  38. * Implements hook_default_video_styles().
  39. */
  40. function video_embed_field_default_video_embed_styles() {
  41. $styles = array();
  42. $handlers = video_embed_get_handlers();
  43. //create the normal handler
  44. $normal = new stdClass;
  45. $normal->disabled = FALSE; /* Edit this to true to make a default video_embed_style disabled initially */
  46. $normal->api_version = 1;
  47. $normal->name = 'normal';
  48. $normal->data = array();
  49. $teaser = new stdClass;
  50. $teaser->disabled = FALSE; /* Edit this to true to make a default video_embed_style disabled initially */
  51. $teaser->api_version = 1;
  52. $teaser->name = 'teaser';
  53. $teaser->data = array();
  54. //add in our settings for each of the handlers
  55. foreach ($handlers as $name => $handler) {
  56. $normal->data[$name] = $handler['defaults'];
  57. $teaser->data[$name] = $handler['defaults'];
  58. $teaser->data[$name]['width'] = 480;
  59. $teaser->data[$name]['height'] = 270;
  60. }
  61. return array($normal, $teaser);
  62. }
  63. /**
  64. * Implements hook_menu().
  65. */
  66. function video_embed_field_menu() {
  67. $items = array();
  68. $items['vef/load/%'] = array(
  69. 'title' => 'Video Embed Field - Load Video',
  70. 'page callback' => '_video_embed_field_show_video',
  71. 'page arguments' => array(2),
  72. 'access callback' => TRUE,
  73. 'type' => MENU_CALLBACK,
  74. );
  75. return $items;
  76. }
  77. /**
  78. * Get an array of all styles and their settings.
  79. *
  80. * @return
  81. * An array of styles keyed by the video style ID (vsid).
  82. * @see video_embed_field_video_style_load()
  83. */
  84. function video_embed_field_video_styles() {
  85. $styles = &drupal_static(__FUNCTION__);
  86. // Grab from cache or build the array.
  87. if (!isset($styles)) {
  88. // load the style via ctools - which will handle all the caching for us -
  89. // however, because it does a bit more logic, lets still statically cache this function
  90. ctools_include('export');
  91. $styles = ctools_export_load_object('vef_video_styles');
  92. }
  93. return $styles;
  94. }
  95. /**
  96. * Load a style by style name or ID. May be used as a loader for menu items.
  97. *
  98. * Note that you may also use ctools_export_load_object with the key being vef_video_styles
  99. *
  100. * @param $name
  101. * The name of the style.
  102. * @param $isid
  103. * Optional. The numeric id of a style if the name is not known.
  104. * @return
  105. * An video style array containing the following keys:
  106. * - "vsid": The unique image style ID.
  107. * - "name": The unique image style name.
  108. * - "data": An array of video settings within this video style.
  109. * If the video style name or ID is not valid, an empty array is returned.
  110. */
  111. function video_embed_field_video_style_load($name = NULL, $vsid = NULL) {
  112. $styles = video_embed_field_video_styles();
  113. // If retrieving by name.
  114. if (isset($name) && isset($styles[$name])) {
  115. $style = $styles[$name];
  116. } // If retrieving by image style id.
  117. else if (!isset($name) && isset($vsid)) {
  118. foreach ($styles as $name => $database_style) {
  119. if (isset($database_style['vsid']) && $database_style['vsid'] == $vsid) {
  120. $style = $database_style;
  121. break;
  122. }
  123. }
  124. }
  125. //if we found a style return it
  126. if (isset($style)) {
  127. return $style;
  128. }
  129. // Otherwise the style was not found.
  130. return FALSE;
  131. }
  132. /**
  133. * Implements hook_permission().
  134. */
  135. function video_embed_field_permission() {
  136. return array(
  137. 'administer video styles' => array(
  138. 'title' => t('Administer video styles'),
  139. 'description' => t('Create and modify styles for embedded videos.'),
  140. ),
  141. );
  142. }
  143. /**
  144. * Implements hook_theme().
  145. */
  146. function video_embed_field_theme() {
  147. return array(
  148. // Theme functions in video_embed_field.admin.inc.
  149. 'video_embed_field_video_style_list' => array(
  150. 'variables' => array('styles' => NULL),
  151. ),
  152. 'video_embed_field_embed_code' => array(
  153. 'template' => 'video-embed-field-embed-code',
  154. 'variables' => array('url' => NULL, 'style' => 'normal', 'video_data' => array()),
  155. ),
  156. 'video_embed_field_colorbox_code' => array(
  157. 'variables' => array('image_url' => NULL, 'image_style' => 'normal', 'video_url' => NULL, 'video_style' => NULL, 'video_data' => array()),
  158. ),
  159. );
  160. }
  161. /**
  162. * Creates a hook that other modules can implement to get handlers - hook_video_embed_handler_info
  163. * Can be used to add more handlers if needed - from other modules and such
  164. * @see video_embed_field.api.php for more information
  165. */
  166. function video_embed_get_handlers() {
  167. $handlers = cache_get('video_embed_field_handlers');
  168. if ($handlers === FALSE) {
  169. $handlers = module_invoke_all('video_embed_handler_info');
  170. drupal_alter('video_embed_field_handlers', $handlers);
  171. cache_set('video_embed_field_handlers', $handlers);
  172. }
  173. else {
  174. $handlers = $handlers->data;
  175. }
  176. return $handlers;
  177. }
  178. function video_embed_get_handler($url){
  179. // Process video URL
  180. if (!stristr($url, 'http://') && !stristr($url, 'https://')) {
  181. $url = 'http://' . $url;
  182. }
  183. $parts = parse_url($url);
  184. if (!isset($parts['host'])) {
  185. return FALSE;
  186. }
  187. $host = $parts['host'];
  188. if (stripos($host, 'www.') > -1) {
  189. $host = substr($host, 4);
  190. }
  191. $domains = _video_embed_field_get_provider_domains();
  192. $handlers = video_embed_get_handlers();
  193. if (isset($domains[$host])) {
  194. $handler_name = $domains[$host];
  195. $handler = $handlers[$handler_name];
  196. $handler['name'] = $handler_name;
  197. return $handler;
  198. } else {
  199. return FALSE;
  200. }
  201. }
  202. /**
  203. * Create a form from the player configuration options
  204. * $defaults will be passed in with the default settings for the various fields
  205. */
  206. function video_embed_field_get_form($defaults) {
  207. $form = array();
  208. $handlers = video_embed_get_handlers();
  209. foreach ($handlers as $name => $handler) {
  210. if (isset($handler['form']) && function_exists($handler['form'])) {
  211. $handler_defaults = isset($defaults[$name]) ? $defaults[$name] : array();
  212. $handler_defaults = array_merge($handler['defaults'], $handler_defaults);
  213. $form[$name] = call_user_func($handler['form'], $handler_defaults);
  214. $form[$name] += array(
  215. '#type' => 'fieldset',
  216. '#title' => t($handler['title']),
  217. '#tree' => TRUE,
  218. );
  219. }
  220. }
  221. return $form;
  222. }
  223. /**
  224. * Get an array of image styles suitable for using as select list options.
  225. *
  226. * @param $include_empty
  227. * If TRUE a <none> option will be inserted in the options array.
  228. * @return
  229. * Array of image styles both key and value are set to style name.
  230. */
  231. function video_embed_field_video_style_options($include_empty = TRUE) {
  232. $styles = video_embed_field_video_styles();
  233. $options = array();
  234. if ($include_empty && !empty($styles)) {
  235. $options[''] = t('<none>');
  236. }
  237. $options = array_merge($options, drupal_map_assoc(array_keys($styles)));
  238. if (empty($options)) {
  239. $options[''] = t('No defined styles');
  240. }
  241. return $options;
  242. }
  243. /**
  244. * Implements hook_filter_info().
  245. */
  246. function video_embed_field_filter_info() {
  247. $filters['video_embed_field'] = array(
  248. 'title' => t('Video Embedding'),
  249. 'description' => t('Replaces [VIDEO::http://www.youtube.com/watch?v=someVideoID::aVideoStyle] tags with embedded videos.'),
  250. 'process callback' => 'video_embed_field_filter_process',
  251. );
  252. return $filters;
  253. }
  254. /**
  255. * Video Embed Field filter process callback.
  256. */
  257. function video_embed_field_filter_process($text, $filter, $format) {
  258. preg_match_all('/ \[VIDEO:: ( [^\[\]]+ )* \] /x', $text, $matches);
  259. $tag_match = (array) array_unique($matches[1]);
  260. $handlers = video_embed_get_handlers();
  261. foreach ($tag_match as $tag) {
  262. $parts = explode('::', $tag);
  263. // Get video style
  264. if (isset($parts[1])) {
  265. $style = $parts[1];
  266. }
  267. else {
  268. $style = 'normal';
  269. }
  270. $embed_code = theme('video_embed_field_embed_code', array('url' => $parts[0], 'style' => $style));
  271. $text = str_replace('[VIDEO::' . $tag . ']', $embed_code, $text);
  272. }
  273. return $text;
  274. }
  275. /**
  276. * Process variables to format a video player.
  277. *
  278. * $variables contains the following information:
  279. * - $url
  280. * - $style
  281. * - $video_data
  282. *
  283. * @see video-embed.tpl.php
  284. */
  285. function template_preprocess_video_embed_field_embed_code(&$variables) {
  286. // Get the handler
  287. $handler = video_embed_get_handler($variables['url']);
  288. $variables['handler'] = $handler['name'];
  289. // Load the style
  290. $style = video_embed_field_video_style_load($variables['style']);
  291. // If there was an issue load in the default style
  292. if ($style == FALSE) {
  293. $style = video_embed_field_video_style_load('normal');
  294. }
  295. if (isset($style->data[$variables['handler']])) {
  296. $variables['style_settings'] = $style->data[$variables['handler']];
  297. } //safety valve for when we add new handlers and there are styles in the database.
  298. else {
  299. $variables['style_settings'] = $handler['defaults'];
  300. }
  301. // Prepare the URL
  302. if (!stristr($variables['url'], 'http://') && !stristr($variables['url'], 'https://')) {
  303. $variables['url'] = 'http://' . $variables['url'];
  304. }
  305. // Prepare embed code
  306. if ($handler && isset($handler['function']) && function_exists($handler['function'])) {
  307. $variables['embed_code'] = drupal_render(call_user_func($handler['function'], $variables['url'], $variables['style_settings']));
  308. }
  309. else {
  310. $variables['embed_code'] = l($variables['url'], $variables['url']);
  311. }
  312. // Prepare video data
  313. $variables['data'] = $variables['video_data'];
  314. unset($variables['video_data']);
  315. }
  316. /**
  317. * Returns image style image with a link to
  318. * an embedded video in colorbox.
  319. *
  320. * @param $variables
  321. * An associative array containing:
  322. * - image_url: The image URL.
  323. * - image_style: The image style to use.
  324. * - video_url: The video URL.
  325. * - video_style: The video style to use.
  326. * - video_data: An array of data about the video.
  327. *
  328. * @ingroup themeable
  329. */
  330. function theme_video_embed_field_colorbox_code($variables) {
  331. $style = video_embed_field_video_style_load($variables['video_style']);
  332. // If there was an issue load in the default style
  333. if ($style == FALSE) {
  334. $style = video_embed_field_video_style_load('normal');
  335. }
  336. $handler = video_embed_get_handler($variables['video_url']);
  337. $data = $style->data[$handler['name']];
  338. //Create a unique ID for colorbox inline
  339. $id = uniqid('video_embed_field-' . rand());
  340. if($variables['image_style'] == 'none'){
  341. $image = array(
  342. array(
  343. '#theme' => 'image',
  344. '#path' => $variables['image_url'],
  345. ),
  346. );
  347. }
  348. else {
  349. $image = array(
  350. '#theme' => 'image_style',
  351. '#path' => $variables['image_url'],
  352. '#style_name' => $variables['image_style'],
  353. );
  354. }
  355. $image = drupal_render($image);
  356. // Write values for later AJAX load
  357. $hash = _video_embed_field_store_video($variables['video_url'], $variables['video_style']);
  358. $output = l($image, base_path() . '?q=vef/load/' . $hash . '&width=' . ($data['width']) . '&height=' . ($data['height']+3), array('html' => true, 'external' => true, 'attributes' => array('class' => array('colorbox-load'))));
  359. return $output;
  360. }
  361. /**
  362. * Get the thumbnail url for a given video url
  363. * @param $url - the url of the video
  364. * @return a string representing the url of the thumbnail, or FALSE on error
  365. */
  366. function video_embed_field_thumbnail_url($url){
  367. $handler = video_embed_get_handler($url);
  368. if ($handler && isset($handler['thumbnail_function']) && function_exists($handler['thumbnail_function'])) {
  369. $info = call_user_func($handler['thumbnail_function'], $url);
  370. $info['handler'] = $handler['name'];
  371. return $info;
  372. }
  373. return FALSE;
  374. }
  375. /**
  376. * Get a video data array for a given video url
  377. *
  378. * @param string $url
  379. * A video URL of the data array you want returned
  380. *
  381. * @return array|false $data
  382. * An array of video data, or FALSE on error
  383. */
  384. function video_embed_field_get_video_data($url) {
  385. $handler = video_embed_get_handler($url);
  386. if ($handler && isset($handler['data_function']) && function_exists($handler['data_function'])) {
  387. $data = call_user_func($handler['data_function'], $url);
  388. $data['handler'] = $handler['name'];
  389. return $data;
  390. }
  391. return FALSE;
  392. }
  393. /**
  394. * Fetch all available provider domains.
  395. */
  396. function _video_embed_field_get_provider_domains() {
  397. $domains = array();
  398. $handlers = video_embed_get_handlers();
  399. foreach ($handlers as $name => $handler) {
  400. if (isset($handler['function']) && function_exists($handler['function'])) {
  401. foreach ($handler['domains'] as $domain) {
  402. $domains[$domain] = $name;
  403. }
  404. }
  405. }
  406. return $domains;
  407. }
  408. /**
  409. * Fetch settings string
  410. */
  411. function _video_embed_code_get_settings_str($settings = array()) {
  412. $values = array();
  413. foreach ($settings as $name => $value) {
  414. $values[] = $name . '=' . $value;
  415. }
  416. return implode('&amp;', $values);
  417. }
  418. //used to array filter in video_embed_field_requirements
  419. function _video_embed_field_array_filter($item) {
  420. return (isset($item['type']) && $item['type'] == 'video_embed_field');
  421. }
  422. /**
  423. * Store a video to be loaded later from an _video_embed_field_load_video
  424. */
  425. function _video_embed_field_store_video($video_url, $video_style) {
  426. //create a hash key
  427. $hash = _video_embed_field_hash($video_url, $video_style);
  428. //check that is record doesn't already exist before saving it
  429. if(!_video_embed_field_load_video($hash)){
  430. $record = array(
  431. 'vhash' => $hash,
  432. 'video_url' => $video_url,
  433. 'video_style' => $video_style,
  434. );
  435. cache_set('vef-store-'.$hash, $record);
  436. //add it to our static cache so we won't have to go to the database
  437. $static_cache = &drupal_static('vef_video_store', array());
  438. $static_cache[$hash] = $record;
  439. }
  440. return $hash;
  441. }
  442. /**
  443. * Callback to render a video for an Ajax call
  444. */
  445. function _video_embed_field_show_video($hash){
  446. $data = _video_embed_field_load_video($hash);
  447. $video = array(
  448. '#theme' => 'video_embed_field_embed_code',
  449. '#style' => $data['video_style'],
  450. '#url' => $data['video_url'],
  451. );
  452. print drupal_render($video);
  453. return NULL;
  454. }
  455. /**
  456. * Loads a video from the video store given its hash
  457. * Returns either the data - an array with hash, video_url and video_style keys
  458. */
  459. function _video_embed_field_load_video($hash) {
  460. $static_cache = &drupal_static('vef_video_store', array());
  461. //check if we've already loaded it
  462. if (isset($static_cache[$hash])) {
  463. return $static_cache[$hash];
  464. } else {
  465. $result = cache_get('vef-store-'.$hash);
  466. if ($result) {
  467. //cache it before returning
  468. $data = $result->data;
  469. $static_cache[$hash] = $data;
  470. return $data;
  471. } else {
  472. return FALSE;
  473. }
  474. }
  475. }
  476. /**
  477. * Creates a hash for storing or looking up a video in the store table
  478. */
  479. function _video_embed_field_hash($video_url, $video_style){
  480. return md5('vef' . $video_url . $video_style);
  481. }