video_embed_field.module 16 KB

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