video_filter_field.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /**
  3. * @file video_filter_field.module
  4. */
  5. /**
  6. * Implements hook_field_info().
  7. */
  8. function video_filter_field_field_info() {
  9. return array(
  10. 'video_filter' => array(
  11. 'label' => t('Video Filter'),
  12. 'description' => t('This field stores URLs and settings to be '
  13. . 'handled by the Video Filter module.'),
  14. 'settings' => array(),
  15. 'instance_settings' => array(
  16. 'max_height' => 400,
  17. 'max_width' => 400,
  18. 'autoplay' => 0,
  19. ),
  20. 'default_widget' => 'video_filter_field_default',
  21. 'default_formatter' => 'video_filter_field_default',
  22. ),
  23. );
  24. }
  25. /**
  26. * Implements magic callback / psuedo-hook mymodule_field_schema().
  27. */
  28. function video_filter_field_field_schema($field) {
  29. if ($field['type'] == 'video_filter') {
  30. $schema['columns']['url'] = array(
  31. 'type' => 'varchar',
  32. 'length' => 255,
  33. 'not null' => FALSE,
  34. );
  35. $schema['columns']['height'] = array(
  36. 'type' => 'int',
  37. 'not null' => FALSE,
  38. );
  39. $schema['columns']['width'] = array(
  40. 'type' => 'int',
  41. 'not null' => FALSE,
  42. );
  43. $schema['columns']['autoplay'] = array(
  44. 'type' => 'int',
  45. 'not null' => FALSE,
  46. );
  47. $schema['indexes'] = array(
  48. 'height' => array('height'),
  49. 'width' => array('width'),
  50. );
  51. return $schema;
  52. }
  53. }
  54. /**
  55. * Implements magic callback / psuedo-hook mymodule_field_is_empty().
  56. */
  57. function video_filter_field_field_is_empty($item, $field) {
  58. if ($field['type'] == 'video_filter') {
  59. if (empty($item['url']) && empty($item['height'])
  60. && empty($item['width']) && empty($item['autoplay']))
  61. {
  62. return TRUE;
  63. } else {
  64. return FALSE;
  65. }
  66. }
  67. }
  68. /**
  69. * Implements magic callback / psuedo-hook mymodule_field_settings_form().
  70. */
  71. /*
  72. function video_filter_field_settings_form($field, $instance, $has_data) {
  73. if ($field['type'] == 'video_filter') {
  74. $settings = $field['setings'];
  75. $form = '';
  76. return $form;
  77. }
  78. }
  79. // */
  80. /**
  81. * Implements hook_field_validate().
  82. */
  83. function video_filter_field_field_validate($obj_type, $object, $field,
  84. $instance, $langcode, &$items,
  85. &$errors) {
  86. if ($field['type'] == 'video_filter') {
  87. foreach ($items as $delta => $item) {
  88. // Test URL.
  89. if (!empty($item['url'])) {
  90. //$errors[$field['field_name']][$langcode][$delta][] = array(
  91. // 'error' => 'video_filter_url',
  92. // 'message' => t('Video Filter Field: Invalid video URL'),
  93. //);
  94. // If URL isn't empty, we need a height.
  95. if (empty($item['height']) || !is_numeric($item['height'])) {
  96. $errors[$field['field_name']][$langcode][$delta][] = array(
  97. 'error' => 'video_filter_height',
  98. 'message' => t('Video Filter Field: Invalid video height'),
  99. );
  100. }
  101. // If URL isn't empty, we need a width.
  102. if (empty($item['width']) || !is_numeric($item['width'])) {
  103. $errors[$field['field_name']][$langcode][$delta][] = array(
  104. 'error' => 'video_filter_width',
  105. 'message' => t('Video Filter Field: Invalid video width'),
  106. );
  107. }
  108. }
  109. // URL field is empty. Set everything else to empty.
  110. else {
  111. unset($items);
  112. unset($object->field_media_url);
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * Implements hook_field_widget_info().
  119. */
  120. function video_filter_field_field_widget_info() {
  121. return array(
  122. 'video_filter' => array(
  123. 'label' => t('Video Filter'),
  124. 'description' => t('Enable user to determine height, width, and autoplay settings'),
  125. 'field types' => array('video_filter'),
  126. 'behaviors' => array(
  127. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  128. 'default value' => FIELD_BEHAVIOR_DEFAULT,
  129. ),
  130. ),
  131. );
  132. }
  133. /**
  134. * Implements hook_field_widget_form().
  135. */
  136. function video_filter_field_field_widget_form(&$form, &$form_state, $field,
  137. $instance, $langcode, $items,
  138. $delta, $element) {
  139. $base = $element;
  140. //dsm($element);
  141. //dsm($instance,' instance');
  142. if ($instance['widget']['type'] == 'video_filter') {
  143. $element['url'] = array(
  144. '#type' => 'textfield',
  145. '#title' => t('Video URL'),
  146. '#default_value' => isset($items[$delta]['url']) ?
  147. $items[$delta]['url'] : NULL,
  148. '#weight' => 0,
  149. ) + $base;
  150. $element['height'] = array(
  151. '#type' => 'textfield',
  152. '#title' => t('Height'),
  153. '#default_value' => isset($items[$delta]['height']) ?
  154. $items[$delta]['height'] : NULL,
  155. '#weight' => 1,
  156. ) + $base;
  157. $element['width'] = array(
  158. '#type' => 'textfield',
  159. '#title' => t('Width'),
  160. '#default_value' => isset($items[$delta]['width']) ?
  161. $items[$delta]['width'] : NULL,
  162. '#weight' => 2,
  163. ) + $base;
  164. $element['autoplay'] = array(
  165. '#type' => 'checkbox',
  166. '#title' => t('Autoplay'),
  167. '#default_value' => isset($items[$delta]['autoplay']) ?
  168. $items[$delta]['autoplay'] : FALSE,
  169. '#weight' => 3,
  170. ) + $base;
  171. }
  172. return $element;
  173. }
  174. /**
  175. * Implements hook_field_formatter_info().
  176. */
  177. function video_filter_field_field_formatter_info() {
  178. return array(
  179. 'video_filter_field_default' => array(
  180. 'label' => t('Video Filter'),
  181. 'field types' => array('text', 'video_filter'),
  182. ),
  183. );
  184. }
  185. /**
  186. * Implements hook_field_formatter_view().
  187. */
  188. function video_filter_field_field_formatter_view($obj_type, $object, $field,
  189. $instance, $langcode, $items,
  190. $display) {
  191. /*
  192. dsm($obj_type, 'obj_type');
  193. dsm($object, 'object');
  194. dsm($field, 'field');
  195. dsm($instance,'instance');
  196. dsm($langcode,'langcode');
  197. dsm($display,'display');
  198. dsm($items,'items');
  199. // */
  200. $elements = array();
  201. foreach ($items as $delta => $item) {
  202. $url = url($item['url']);
  203. $text = "[video:$url]";
  204. $elements[$delta] = array(
  205. '#theme' => 'video_filter_field_default_formatter',
  206. '#item' => video_filter_field_process($text, $item),
  207. );
  208. }
  209. return $elements;
  210. }
  211. /**
  212. * Implements hook_theme().
  213. */
  214. function video_filter_field_theme() {
  215. return array(
  216. 'video_filter_field_default_formatter' => array(
  217. 'variables' => array('item' => NULL),
  218. ),
  219. );
  220. }
  221. /**
  222. * Returns HTML for a video_filter_field_formatter
  223. */
  224. function theme_video_filter_field_default_formatter($variables) {
  225. $output = '<div class="video-filter-field">' . $variables['item'] . '</div>';
  226. return $output;
  227. }
  228. /**
  229. * Implement video_filter processing from video_filter module.
  230. *
  231. * @see _video_filter_process()
  232. */
  233. function video_filter_field_process($text, $video) {
  234. $filter = new stdClass();
  235. //$filter->format = '';
  236. //$filter->module = 'video_filter';
  237. //$filter->name = 'video_filter';
  238. //$filter->weight = -1;
  239. $filter->status = 1;
  240. $filter->settings = array(
  241. 'video_filter_width' => 400,
  242. 'video_filter_height' => 400,
  243. 'video_filter_autoplay' => 0,
  244. 'video_filter_related' => 1,
  245. );
  246. $text = (string)$text; // Make sure $text is a string.
  247. if (preg_match_all('/\[video(\:(.+))?( .+)?\]/isU', $text, $matches_code)) {
  248. foreach ($matches_code[0] as $ci => $code) {
  249. $video = (array)$video + array(
  250. 'source' => $matches_code[2][$ci],
  251. 'autoplay' => $filter->settings['video_filter_autoplay'],
  252. 'related' => $filter->settings['video_filter_related'],
  253. );
  254. // Pick random out of multiple sources separated by ','
  255. if (strstr($video['source'], ',')) {
  256. $sources = explode(',', $video['source']);
  257. $random = array_rand($sources, 1);
  258. $video['source'] = $sources[$random];
  259. }
  260. // Load all codecs
  261. $codecs = module_invoke_all('codec_info');
  262. // Find codec
  263. foreach ($codecs AS $codec_name => $codec) {
  264. if (!is_array($codec['regexp'])) {
  265. $codec['regexp'] = array($codec['regexp']);
  266. }
  267. // Try different regular expressions
  268. foreach ($codec['regexp'] as $delta => $regexp) {
  269. if (preg_match($regexp, $video['source'], $matches)) {
  270. $video['codec'] = $codec;
  271. $video['codec']['delta'] = $delta;
  272. $video['codec']['matches'] = $matches;
  273. $video['codec']['codec_name'] = $codec_name; // used in theme function
  274. $video['codec']['control_bar_height'] = 0; // default
  275. break 2;
  276. }
  277. }
  278. }
  279. // Codec found
  280. if (isset($video['codec'])) {
  281. // Override default attributes
  282. if ($matches_code[3][$ci] && preg_match_all('/\s+([a-zA-Z_]+)\:(\s+)?([0-9a-zA-Z\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
  283. foreach ($matches_attributes[0] AS $ai => $attribute) {
  284. $video[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
  285. }
  286. }
  287. // Use configured ratio if present, use that from the codec otherwise
  288. $ratio = 1;
  289. if (isset($video['ratio']) && preg_match('/(\d+)\/(\d+)/', $video['ratio'], $tratio) ) {
  290. //validate given ratio parameter
  291. $ratio = $tratio[1] / $tratio[2];
  292. }
  293. else {
  294. $ratio = $video['codec']['ratio'];
  295. }
  296. // Sets video width & height after any user input has been parsed.
  297. // First, check if user has set a width.
  298. if (isset($video['width']) && !isset($video['height'])) {
  299. $video['height'] = variable_get('video_filter_height_'.$format, 400);
  300. }
  301. // Else, if user has set height.
  302. elseif (isset($video['height']) && !isset($video['width'])) {
  303. $video['width'] = $video['height'] * $ratio;
  304. }
  305. // Maybe both?
  306. elseif (isset($video['height']) && isset($video['width'])) {
  307. $video['width'] = $video['width'];
  308. $video['height'] = $video['height'];
  309. }
  310. // Fall back to defaults.
  311. elseif (!isset($video['height']) && !isset($video['width'])) {
  312. $video['width'] = $filter->settings['video_filter_width'];
  313. $video['height'] = $filter->settings['video_filter_height'];
  314. }
  315. // Default value for control bar height
  316. $control_bar_height = 0;
  317. if (isset($video['control_bar_height'])) {
  318. // respect control_bar_height option if present
  319. $control_bar_height = $video['control_bar_height'];
  320. }
  321. elseif (isset($video['codec']['control_bar_height'])) {
  322. // respect setting provided by codec otherwise
  323. $control_bar_height = $video['codec']['control_bar_height'];
  324. }
  325. // Resize to fit within width and height repecting aspect ratio
  326. if ($ratio) {
  327. $scale_factor = min(array(($video['height']-$control_bar_height), $video['width']/($ratio)));
  328. $video['height'] = round($scale_factor + $control_bar_height);
  329. $video['width'] = round($scale_factor * $ratio);
  330. }
  331. $video['autoplay'] = (bool) $video['autoplay'];
  332. $video['align'] = (isset($video['align']) && in_array($video['align'], array('left', 'right', 'center'))) ? $video['align'] : NULL;
  333. if (is_callable($video['codec']['callback'], FALSE)) {
  334. $replacement = call_user_func($video['codec']['callback'], $video);
  335. }
  336. else {
  337. // Invalid callback
  338. $replacement = '<!-- VIDEO FILTER - INVALID CALLBACK IN: ' . $pattern . ' -->';
  339. }
  340. // Invalid format
  341. }
  342. else {
  343. $replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
  344. }
  345. $text = str_replace($code, $replacement, $text);
  346. }
  347. }
  348. return $text;
  349. }
  350. /**
  351. * Test support for URL.
  352. *
  353. * @todo Implement hook_codec_info() to...
  354. * - add support for whatever media module uses as an alternative to video_filter module.
  355. * - add support for http://embed.ly/.
  356. *
  357. * @param $url
  358. * string, URL which may or may not include media of some kind.
  359. *
  360. * @return
  361. * TRUE, video_filter recognizes and supports media at this URL
  362. * FALSE, video_filter doesn't recognize it. This could be either because
  363. * the URL doesn't actually provide media or because we just doen't have
  364. * the codec for it.
  365. */
  366. function video_filter_field_url_is_supported($url) {
  367. // Load all codecs
  368. module_load_include('inc', 'video_filter', 'video_filter.codecs');
  369. $codecs = module_invoke_all('codec_info');
  370. // Find codec
  371. foreach ($codecs AS $codec_name => $codec) {
  372. if (!is_array($codec['regexp'])) {
  373. $codec['regexp'] = array($codec['regexp']);
  374. }
  375. // Try different regular expressions
  376. $video = array();
  377. foreach ($codec['regexp'] as $delta => $regexp) {
  378. if (preg_match($regexp, $url)) {
  379. $video['codec'] = TRUE;
  380. }
  381. break;
  382. }
  383. break;
  384. }
  385. // If we found a codec, this URL is supported.
  386. $is_supported = (isset($video['codec'])) ? TRUE : FALSE;
  387. return $is_supported;
  388. }