video_embed_field.module 20 KB

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