video_embed_field.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. <?php
  2. /**
  3. * @file
  4. * Provides a simple field for easily embedding videos from youtube or vimeo
  5. *
  6. * This module is not intended to replace media or video - it does not allow for
  7. * any local storage of videos, custom players or anything else.
  8. * It simply allows users to embed videos from youtube and vimeo - and provides
  9. * hooks to allow other modules to provide more providers.
  10. *
  11. * Uses CTools Export UI to manage settings.
  12. *
  13. * @see ./plugins/export_ui/video_embed_field_export_ui.inc
  14. *
  15. * @author jec006, jdelaune
  16. */
  17. // Load all Field module hooks.
  18. module_load_include('inc', 'video_embed_field', 'video_embed_field.field');
  19. // Load the admin forms.
  20. module_load_include('inc', 'video_embed_field', 'video_embed_field.admin');
  21. // Load our default handlers.
  22. module_load_include('inc', 'video_embed_field', 'video_embed_field.handlers');
  23. // Load feeds mapping hooks.
  24. module_load_include('inc', 'video_embed_field', 'video_embed_field.feeds');
  25. /**
  26. * Implements hook_ctools_plugin_directory().
  27. */
  28. function video_embed_field_ctools_plugin_directory($module, $type) {
  29. // Load the export_ui plugin.
  30. if ($type == 'export_ui') {
  31. return 'plugins/export_ui';
  32. }
  33. }
  34. /**
  35. * Implements hook_ctools_plugin_api().
  36. *
  37. * Tell CTools that we support the default_mymodule_presets API.
  38. */
  39. function video_embed_field_ctools_plugin_api($owner, $api) {
  40. if ($owner == 'video_embed_field' && $api == 'default_video_embed_styles') {
  41. return array('version' => 1);
  42. }
  43. }
  44. /**
  45. * Implements hook_default_video_styles().
  46. */
  47. function video_embed_field_default_video_embed_styles() {
  48. $styles = array();
  49. $handlers = video_embed_get_handlers();
  50. // Create the normal handler.
  51. $normal = new stdClass();
  52. $normal->disabled = FALSE; /* Edit this to true to make a default video_embed_style disabled initially */
  53. $normal->api_version = 1;
  54. $normal->name = 'normal';
  55. $normal->title = 'Normal';
  56. $normal->data = array();
  57. $teaser = new stdClass();
  58. $teaser->disabled = FALSE; /* Edit this to true to make a default video_embed_style disabled initially */
  59. $teaser->api_version = 1;
  60. $teaser->name = 'teaser';
  61. $teaser->title = 'Teaser';
  62. $teaser->data = array();
  63. // Add in our settings for each of the handlers.
  64. foreach ($handlers as $name => $handler) {
  65. $normal->data[$name] = $handler['defaults'];
  66. $teaser->data[$name] = $handler['defaults'];
  67. $teaser->data[$name]['width'] = 480;
  68. $teaser->data[$name]['height'] = 270;
  69. }
  70. return array($normal, $teaser);
  71. }
  72. /**
  73. * Implements hook_menu().
  74. */
  75. function video_embed_field_menu() {
  76. $items = array();
  77. $items['vef/load/%'] = array(
  78. 'title' => 'Video Embed Field - Load Video',
  79. 'page callback' => '_video_embed_field_show_video',
  80. 'page arguments' => array(2),
  81. 'access callback' => TRUE,
  82. 'type' => MENU_CALLBACK,
  83. );
  84. return $items;
  85. }
  86. /**
  87. * Implements hook_permission().
  88. */
  89. function video_embed_field_permission() {
  90. return array(
  91. 'administer video styles' => array(
  92. 'title' => t('Administer video styles'),
  93. 'description' => t('Create and modify styles for embedded videos.'),
  94. ),
  95. );
  96. }
  97. /**
  98. * Implements hook_theme().
  99. */
  100. function video_embed_field_theme() {
  101. return array(
  102. // Theme functions in video_embed_field.admin.inc.
  103. 'video_embed_field_video_style_list' => array(
  104. 'variables' => array('styles' => NULL),
  105. ),
  106. 'video_embed_field_embed_code' => array(
  107. 'template' => 'video-embed-field-embed-code',
  108. 'variables' => array(
  109. 'url' => NULL,
  110. 'style' => 'normal',
  111. 'video_data' => array(),
  112. ),
  113. ),
  114. 'video_embed_field_colorbox_code' => array(
  115. 'variables' => array(
  116. 'image_url' => NULL,
  117. 'image_style' => 'normal',
  118. 'image_alt' => NULL,
  119. 'video_url' => NULL,
  120. 'video_style' => NULL,
  121. 'video_data' => array(),
  122. ),
  123. ),
  124. );
  125. }
  126. /**
  127. * Implements hook_views_api().
  128. */
  129. function video_embed_field_views_api() {
  130. return array(
  131. 'api' => 3,
  132. 'path' => drupal_get_path('module', 'video_embed_field') . '/views',
  133. );
  134. }
  135. /**
  136. * Get an array of all styles and their settings.
  137. *
  138. * @return array
  139. * An array of styles keyed by the video style name (name).
  140. *
  141. * @see video_embed_field_video_style_load()
  142. */
  143. function video_embed_field_video_styles() {
  144. $styles = &drupal_static(__FUNCTION__);
  145. // Grab from cache or build the array.
  146. if (!isset($styles)) {
  147. // Load the style via ctools - which will handle all the caching for us -
  148. // Because it does a bit more logic, lets still statically cache this.
  149. ctools_include('export');
  150. $styles = ctools_export_load_object('vef_video_styles');
  151. }
  152. return $styles;
  153. }
  154. /**
  155. * Load a style by style name. May be used as a loader for menu items.
  156. *
  157. * Note that you may also use ctools_export_load_object with the key being
  158. * vef_video_styles
  159. *
  160. * @param string $name
  161. * The name of the style.
  162. *
  163. * @return array
  164. * An video style array containing the following keys:
  165. * - "name": The unique video style ID.
  166. * - "title": The human readable video style name.
  167. * - "data": An array of video settings within this video style.
  168. * If the video style name or ID is not valid, an empty array is returned.
  169. */
  170. function video_embed_field_video_style_load($name) {
  171. $styles = video_embed_field_video_styles();
  172. return isset($styles[$name]) ? $styles[$name] : FALSE;
  173. }
  174. /**
  175. * Creates a hook that other modules can implement to get handlers.
  176. *
  177. * Can be used to add more handlers if needed - from other modules and such.
  178. *
  179. * @see hook_video_embed_handler_info
  180. * @see video_embed_field.api.php
  181. */
  182. function video_embed_get_handlers() {
  183. $handlers = &drupal_static(__FUNCTION__);
  184. if (!isset($handlers)) {
  185. if ($handlers = cache_get('video_embed_field_handlers')) {
  186. $handlers = $handlers->data;
  187. }
  188. else {
  189. $handlers = module_invoke_all('video_embed_handler_info');
  190. drupal_alter('video_embed_handler_info', $handlers);
  191. cache_set('video_embed_field_handlers', $handlers);
  192. }
  193. }
  194. return $handlers;
  195. }
  196. /**
  197. * Retrieves the video handler for a video URL.
  198. *
  199. * @param string $url
  200. * The video URL.
  201. *
  202. * @return string|bool
  203. * The handler name for the URL, FALSE in case there is no handler.
  204. */
  205. function video_embed_get_handler($url) {
  206. // Process video URL.
  207. if (!stristr($url, 'http://') && !stristr($url, 'https://')) {
  208. $url = 'http://' . $url;
  209. }
  210. $parts = parse_url($url);
  211. if (!isset($parts['host'])) {
  212. return FALSE;
  213. }
  214. $host = $parts['host'];
  215. if (stripos($host, 'www.') > -1) {
  216. $host = substr($host, 4);
  217. }
  218. $domains = _video_embed_field_get_provider_domains();
  219. $handlers = video_embed_get_handlers();
  220. if (isset($domains[$host])) {
  221. $handler_name = $domains[$host];
  222. $handler = $handlers[$handler_name];
  223. $handler['name'] = $handler_name;
  224. return $handler;
  225. }
  226. else {
  227. return FALSE;
  228. }
  229. }
  230. /**
  231. * Creates a form from the player configuration options.
  232. *
  233. * @param array $defaults
  234. * The default settings for the various fields.
  235. *
  236. * @return array
  237. * The configuration form array.
  238. */
  239. function video_embed_field_get_form($defaults) {
  240. $form = array();
  241. $handlers = video_embed_get_handlers();
  242. foreach ($handlers as $name => $handler) {
  243. if (isset($handler['form']) && function_exists($handler['form'])) {
  244. $handler_defaults = isset($defaults[$name]) ? $defaults[$name] : array();
  245. $handler_defaults = array_merge($handler['defaults'], $handler_defaults);
  246. $form[$name] = call_user_func($handler['form'], $handler_defaults);
  247. $form[$name] += array(
  248. '#type' => 'fieldset',
  249. '#title' => t('@provider settings', array('@provider' => $handler['title'])),
  250. '#tree' => TRUE,
  251. '#element_validate' => isset($handler['form_validate']) ? array($handler['form_validate']) : array(),
  252. );
  253. }
  254. }
  255. return $form;
  256. }
  257. /**
  258. * Validates the iframe CSS dimensions.
  259. *
  260. * @param array $element
  261. * The element to validate.
  262. */
  263. function video_embed_field_validate_dimensions($element) {
  264. if (!preg_match('/^(\d*)(px|%)?$/', $element['width']['#value'], $results)) {
  265. form_error($element['width'], t('You should use a valid CSS value for width in @plugin plugin', array('@plugin' => $element['#title'])));
  266. }
  267. if (!preg_match('/^(\d*)(px|%)?$/', $element['height']['#value'], $results)) {
  268. form_error($element['height'], t('You should use a valid CSS value for height in @plugin plugin', array('@plugin' => $element['#title'])));
  269. }
  270. }
  271. /**
  272. * Get an array of image styles suitable for using as select list options.
  273. *
  274. * @param bool $include_empty
  275. * If TRUE a <none> option will be inserted in the options array.
  276. *
  277. * @return array
  278. * Array of image styles both key and value are set to style name.
  279. */
  280. function video_embed_field_video_style_options($include_empty = TRUE) {
  281. $styles = video_embed_field_video_styles();
  282. $options = array();
  283. if ($include_empty && !empty($styles)) {
  284. $options[''] = t('<none>');
  285. }
  286. foreach ($styles as $style) {
  287. $options[$style->name] = $style->title;
  288. }
  289. if (empty($options)) {
  290. $options[''] = t('No defined styles');
  291. }
  292. return $options;
  293. }
  294. /**
  295. * Implements hook_filter_info().
  296. */
  297. function video_embed_field_filter_info() {
  298. $filters['video_embed_field'] = array(
  299. 'title' => t('Video Embedding'),
  300. 'description' => t('Replaces [VIDEO::http://www.youtube.com/watch?v=someVideoID::aVideoStyle] tags with embedded videos.'),
  301. 'process callback' => 'video_embed_field_filter_process',
  302. 'tips callback' => '_filter_video_embed_tips',
  303. );
  304. return $filters;
  305. }
  306. /**
  307. * Implements callback_filter_tips().
  308. *
  309. * Provides help for the URL filter.
  310. *
  311. * @see filter_filter_info()
  312. */
  313. function _filter_video_embed_tips($filter, $format, $long = FALSE) {
  314. return t('Replaces [VIDEO::http://www.youtube.com/watch?v=someVideoID::aVideoStyle] tags with embedded videos.');
  315. }
  316. /**
  317. * Video Embed Field filter process callback.
  318. */
  319. function video_embed_field_filter_process($text, $filter, $format) {
  320. preg_match_all('/ \[VIDEO:: ( [^\[\]]+ )* \] /x', $text, $matches);
  321. $tag_match = (array) array_unique($matches[1]);
  322. foreach ($tag_match as $tag) {
  323. $parts = explode('::', $tag);
  324. // Get video style.
  325. if (isset($parts[1])) {
  326. $style = $parts[1];
  327. }
  328. else {
  329. $style = 'normal';
  330. }
  331. $embed_code = theme('video_embed_field_embed_code', array('url' => $parts[0], 'style' => $style));
  332. $text = str_replace('[VIDEO::' . $tag . ']', $embed_code, $text);
  333. }
  334. return $text;
  335. }
  336. /**
  337. * Processes variables to format a video player.
  338. *
  339. * @param array $variables
  340. * Contains the following information:
  341. * - $url
  342. * - $style
  343. * - $video_data
  344. *
  345. * @see video-embed.tpl.php
  346. */
  347. function template_preprocess_video_embed_field_embed_code(&$variables) {
  348. // Get the handler.
  349. $handler = video_embed_get_handler($variables['url']);
  350. $variables['handler'] = $handler['name'];
  351. // Load the style.
  352. $style = video_embed_field_video_style_load($variables['style']);
  353. // If there was an issue load in the default style.
  354. if ($style == FALSE) {
  355. $style = video_embed_field_video_style_load('normal');
  356. }
  357. if (isset($style->data[$variables['handler']])) {
  358. $variables['style_settings'] = $style->data[$variables['handler']];
  359. }
  360. // Safety value for when we add new handlers and there are styles stored.
  361. else {
  362. $variables['style_settings'] = $handler['defaults'];
  363. }
  364. // Prepare the URL.
  365. if (!stristr($variables['url'], 'http://') && !stristr($variables['url'], 'https://')) {
  366. $variables['url'] = 'http://' . $variables['url'];
  367. }
  368. // Prepare embed code.
  369. if ($handler && isset($handler['function']) && function_exists($handler['function'])) {
  370. $embed_code = call_user_func($handler['function'], $variables['url'], $variables['style_settings']);
  371. $variables['embed_code'] = drupal_render($embed_code);
  372. }
  373. else {
  374. $variables['embed_code'] = l($variables['url'], $variables['url']);
  375. }
  376. // Prepare video data.
  377. $variables['data'] = $variables['video_data'];
  378. unset($variables['video_data']);
  379. }
  380. /**
  381. * Returns image style image with a link to an embedded video in colorbox.
  382. *
  383. * @param array $variables
  384. * An associative array containing:
  385. * - image_url: The image URL.
  386. * - image_style: The image style to use.
  387. * - image_alt: The image ALT attribute.
  388. * - video_url: The video URL.
  389. * - video_style: The video style to use.
  390. * - video_data: An array of data about the video.
  391. *
  392. * @return string
  393. * The themed output.
  394. *
  395. * @ingroup themeable
  396. */
  397. function theme_video_embed_field_colorbox_code($variables) {
  398. $path = video_embed_field_get_ajax_url($variables['video_url'], $variables['video_style']);
  399. $image = array(
  400. '#theme' => 'image_formatter',
  401. '#item' => array('uri' => $variables['image_url'], 'alt' => $variables['image_alt']),
  402. '#image_style' => $variables['image_style'],
  403. '#path' => $path,
  404. );
  405. return drupal_render($image);
  406. }
  407. /**
  408. * Gets the thumbnail url for a given video url.
  409. *
  410. * @param string $url
  411. * The url of the video.
  412. *
  413. * @return string
  414. * String representing the url of the thumbnail, or FALSE on error.
  415. */
  416. function video_embed_field_thumbnail_url($url) {
  417. $info = FALSE;
  418. if ($handler = video_embed_get_handler($url)) {
  419. if (isset($handler['thumbnail_function']) && function_exists($handler['thumbnail_function'])) {
  420. $info = call_user_func($handler['thumbnail_function'], $url);
  421. $info['handler'] = $handler['name'];
  422. }
  423. if (empty($info['url']) && isset($handler['thumbnail_default']) && file_exists($handler['thumbnail_default'])) {
  424. $info = array(
  425. 'handler' => $handler['name'],
  426. 'id' => 'default_thumbnail',
  427. 'url' => $handler['thumbnail_default'],
  428. );
  429. }
  430. }
  431. return $info;
  432. }
  433. /**
  434. * Gets a video data array for a given video url.
  435. *
  436. * @param string $url
  437. * A video URL of the data array you want returned.
  438. *
  439. * @return array|false
  440. * An array of video data, or FALSE on error.
  441. */
  442. function video_embed_field_get_video_data($url) {
  443. $handler = video_embed_get_handler($url);
  444. if ($handler && isset($handler['data_function']) && function_exists($handler['data_function'])) {
  445. $data = call_user_func($handler['data_function'], $url);
  446. $data['handler'] = $handler['name'];
  447. return $data;
  448. }
  449. return FALSE;
  450. }
  451. /**
  452. * Generates the AJAX path array from the video URL and the video_style.
  453. *
  454. * @param string $video_url
  455. * The URL to the video.
  456. * @param string $video_style
  457. * The video style to render the video.
  458. *
  459. * @return array
  460. * The AJAX path array.
  461. */
  462. function video_embed_field_get_ajax_url($video_url, $video_style) {
  463. $style = video_embed_field_video_style_load($video_style);
  464. // If there was an issue load in the default style.
  465. if ($style == FALSE) {
  466. $style = video_embed_field_video_style_load('normal');
  467. }
  468. $handler = video_embed_get_handler($video_url);
  469. $data = $style->data[$handler['name']];
  470. // Write values for later AJAX load.
  471. $hash = _video_embed_field_store_video($video_url, $video_style);
  472. return array(
  473. 'path' => 'vef/load/' . $hash,
  474. 'options' => array(
  475. 'attributes' => array(
  476. 'class' => array(
  477. 'colorbox-load',
  478. 'colorbox'
  479. ),
  480. ),
  481. 'query' => array(
  482. 'width' => $data['width'],
  483. 'height' => $data['height'] + 5,
  484. ),
  485. ),
  486. );
  487. }
  488. /**
  489. * Fetches all available provider domains.
  490. *
  491. * @return array
  492. * An array containing the allowed video domains.
  493. */
  494. function _video_embed_field_get_provider_domains() {
  495. $domains = array();
  496. $handlers = video_embed_get_handlers();
  497. foreach ($handlers as $name => $handler) {
  498. if (isset($handler['function']) && function_exists($handler['function'])) {
  499. foreach ($handler['domains'] as $domain) {
  500. $domains[$domain] = $name;
  501. }
  502. }
  503. }
  504. return $domains;
  505. }
  506. /**
  507. * Fetches all available provider domains for certain field instance.
  508. *
  509. * @param array $instance
  510. * The instance definition.
  511. *
  512. * @return array
  513. * An array containing the allowed video domains.
  514. */
  515. function _video_embed_field_get_instance_provider_domains($instance) {
  516. $domains = _video_embed_field_get_provider_domains();
  517. foreach ($domains as $domain => $provider) {
  518. if (empty($instance['settings']['allowed_providers'][$provider])) {
  519. unset($domains[$domain]);
  520. }
  521. }
  522. return $domains;
  523. }
  524. /**
  525. * Fetches settings string.
  526. *
  527. * @param array $settings
  528. * The settings array.
  529. *
  530. * @return string
  531. * The settings string generated from the settings array.
  532. */
  533. function _video_embed_code_get_settings_str($settings = array()) {
  534. $values = array();
  535. foreach ($settings as $name => $value) {
  536. if (!isset($value)) {
  537. $values[] = $name;
  538. }
  539. else {
  540. $values[] = $name . '=' . $value;
  541. }
  542. }
  543. return implode('&amp;', $values);
  544. }
  545. /**
  546. * Stores a video to be loaded later from an _video_embed_field_load_video.
  547. *
  548. * @param string $video_url
  549. * The video URL.
  550. * @param string $video_style
  551. * The video style.
  552. *
  553. * @return string
  554. * The hash generated for the video URL and the given style.
  555. */
  556. function _video_embed_field_store_video($video_url, $video_style) {
  557. // Create a hash key.
  558. $hash = _video_embed_field_hash($video_url, $video_style);
  559. // Check that this record doesn't already exist before saving it.
  560. if (!_video_embed_field_load_video($hash)) {
  561. $record = array(
  562. 'vhash' => $hash,
  563. 'video_url' => $video_url,
  564. 'video_style' => $video_style,
  565. );
  566. cache_set('vef-store-' . $hash, $record);
  567. // Add it to our static cache so we won't have to go to the database.
  568. $static_cache = &drupal_static('vef_video_store', array());
  569. $static_cache[$hash] = $record;
  570. }
  571. return $hash;
  572. }
  573. /**
  574. * Renders a video for an AJAX call.
  575. *
  576. * @param string $hash
  577. * The video hash.
  578. *
  579. * @return Null
  580. * Null because prints an AJAX output.
  581. */
  582. function _video_embed_field_show_video($hash) {
  583. $data = _video_embed_field_load_video($hash);
  584. $video = array(
  585. '#theme' => 'video_embed_field_embed_code',
  586. '#style' => $data['video_style'],
  587. '#url' => $data['video_url'],
  588. );
  589. print drupal_render($video);
  590. return NULL;
  591. }
  592. /**
  593. * Loads a video from the video store given its hash.
  594. *
  595. * @param string $hash
  596. * The video hash.
  597. *
  598. * @return array|bool
  599. * An array with video definition, FALSE if the hash does not exist.
  600. */
  601. function _video_embed_field_load_video($hash) {
  602. $static_cache = &drupal_static('vef_video_store', array());
  603. // Check if we've already loaded it.
  604. if (isset($static_cache[$hash])) {
  605. return $static_cache[$hash];
  606. }
  607. else {
  608. $result = cache_get('vef-store-' . $hash);
  609. if ($result) {
  610. // Cache it before returning.
  611. $data = $result->data;
  612. $static_cache[$hash] = $data;
  613. return $data;
  614. }
  615. else {
  616. return FALSE;
  617. }
  618. }
  619. }
  620. /**
  621. * Creates a hash for storing or looking up a video in the store table.
  622. *
  623. * @param string $video_url
  624. * The video URL.
  625. * @param string $video_style
  626. * The video style.
  627. *
  628. * @return string
  629. * The hash generated for the video URL and the given style.
  630. */
  631. function _video_embed_field_hash($video_url, $video_style) {
  632. return md5('vef' . $video_url . $video_style);
  633. }