You are here

example.inc.txt in Embedded Media Field 6.3

Same filename and directory in other branches
  1. 6 contrib/emvideo/providers/example.inc.txt

This is an example provider include file for Embedded Media Video. Use this as a base for creating new provider files.

When using this, first make the following global replacements:

  • Replace EXAMPLE with the name of your provider in all caps.
  • Replace example with the name of your provider in all lower case.
  • Replace Example with the name (to be translated) of your provider in uppercase.

You then need to go through each function and modify according to the requirements of your provider's API.

File

contrib/emvideo/providers/example.inc.txt
View source
  1. /**
  2. * @file
  3. * This is an example provider include file for Embedded Media Video.
  4. * Use this as a base for creating new provider files.
  5. *
  6. * When using this, first make the following global replacements:
  7. * * Replace EXAMPLE with the name of your provider in all caps.
  8. * * Replace example with the name of your provider in all lower case.
  9. * * Replace Example with the name (to be translated) of your provider in
  10. * uppercase.
  11. *
  12. * You then need to go through each function and modify according to the
  13. * requirements of your provider's API.
  14. */
  15. /**
  16. * This is the main URL for your provider.
  17. */
  18. define('EMVIDEO_EXAMPLE_MAIN_URL', 'http://www.example.com/');
  19. /**
  20. * This is the URL to the API of your provider, if this exists.
  21. */
  22. define('EMVIDEO_EXAMPLE_API_URL', 'http://www.example.com/api');
  23. /**
  24. * This defines the version of the content data array that we serialize
  25. * in emvideo_example_data(). If we change the expected keys of that array,
  26. * we must increment this value, which will allow older content to be updated
  27. * to the new version automatically.
  28. */
  29. define('EMVIDEO_EXAMPLE_DATA_VERSION', 1);
  30. /**
  31. * hook emvideo_PROVIDER_info
  32. * This returns information relevant to a specific 3rd party video provider.
  33. *
  34. * @return
  35. * A keyed array of strings requested by various admin and other forms.
  36. * 'provider' => The machine name of the provider. This must be the same as
  37. * the base name of this filename, before the .inc extension.
  38. * 'name' => The translated name of the provider.
  39. * 'url' => The url to the main page for the provider.
  40. * 'settings_description' => A description of the provider that will be
  41. * posted in the admin settings form.
  42. * 'supported_features' => An array of rows describing the state of certain
  43. * supported features by the provider. These will be rendered in a table,
  44. * with the columns being 'Feature', 'Supported', 'Notes'. In general,
  45. * the 'Feature' column will give the name of the feature, 'Supported'
  46. * will be Yes or No, and 'Notes' will give an optional description or
  47. * caveats to the feature.
  48. */
  49. function emvideo_example_info() {
  50. $features = array(
  51. array(t('Autoplay'), t('Yes'), ''),
  52. array(t('RSS Attachment'), t('Yes'), ''),
  53. array(t('Thumbnails'), t('Yes'), t('')),
  54. array(t('Full screen mode'), t('Yes'), t('You may customize the player to enable or disable full screen playback. Full screen mode is enabled by default.')),
  55. );
  56. return array(
  57. 'provider' => 'example',
  58. 'name' => t('Example'),
  59. 'url' => EMVIDEO_EXAMPLE_MAIN_URL,
  60. 'settings_description' => t('These settings specifically affect videos displayed from !example. You can also read more about its !api.', array('!example' => l(t('Example.com'), EMVIDEO_EXAMPLE_MAIN_URL), '!api' => l(t("developer's API"), EMVIDEO_EXAMPLE_API_URL))),
  61. 'supported_features' => $features,
  62. );
  63. }
  64. /**
  65. * hook emvideo_PROVIDER_settings
  66. * This should return a subform to be added to the emvideo_settings() admin
  67. * settings page.
  68. *
  69. * Note that a form field set will already be provided at $form['example'],
  70. * so if you want specific provider settings within that field set, you should
  71. * add the elements to that form array element.
  72. */
  73. function emvideo_example_settings() {
  74. // We'll add a field set of player options here. You may add other options
  75. // to this element, or remove the field set entirely if there are no
  76. // user-configurable options allowed by the example provider.
  77. $form['example']['player_options'] = array(
  78. '#type' => 'fieldset',
  79. '#title' => t('Embedded video player options'),
  80. '#collapsible' => TRUE,
  81. '#collapsed' => TRUE,
  82. );
  83. // This is an option to set the video to full screen. You should remove this
  84. // option if it is not provided by the example provider.
  85. $form['example']['player_options']['emvideo_example_full_screen'] = array(
  86. '#type' => 'checkbox',
  87. '#title' => t('Allow fullscreen'),
  88. '#default_value' => variable_get('emvideo_example_full_screen', 1),
  89. '#description' => t('Allow users to view video using the entire computer screen.'),
  90. );
  91. return $form;
  92. }
  93. /**
  94. * hook emvideo_PROVIDER_extract
  95. *
  96. * This is called to extract the video code from a pasted URL or embed code.
  97. *
  98. * We'll be passed a URL or the embed code from a video when an editor pastes
  99. * that in the field's textfield. We'll need to either pass back an array of
  100. * regex expressions to match, or do the matching ourselves and return the
  101. * resulting video code.
  102. *
  103. * @param $parse
  104. * An optional string with the pasted URL or embed code.
  105. * @return
  106. * Either an array of regex expressions to be tested, or a string with the
  107. * video code to be used. If the hook tests the code itself, it should
  108. * return either the string of the video code (if matched), or an empty
  109. * array. Otherwise, the calling function will handle testing the embed code
  110. * against each regex string in the returned array.
  111. */
  112. function emvideo_example_extract($parse = '') {
  113. // Here we assume that a URL will be passed in the form of
  114. // http://www.example.com/video/text-video-title
  115. // or embed code in the form of
  116. // We'll simply return an array of regular expressions for Embedded Media
  117. // Field to handle for us.
  118. return array(
  119. // In this expression, we're looking first for text matching the expression
  120. // between the @ signs. The 'i' at the end means we don't care about the
  121. // case. Thus, if someone enters http://www.Example.com, it will still
  122. // match. We escape periods as \., as otherwise they match any character.
  123. // The text in parentheses () will be returned as the provider video code,
  124. // if there's a match for the entire expression. In this particular case,
  125. // ([^?]+) means to match one more more characters (+) that are not a
  126. // question mark ([^\?]), which would denote a query in the URL.
  127. '@example\.com/video/([^\?]+)@i',
  128. // Now we test for embedded video code, which is similar in this case to
  129. // the above expression, except that we can safely assume we won't have a
  130. // query in the URL, and that the URL will be surrounded by quotation marks,
  131. // and have /embed/ rather than /video/ in the URL. Note that regular
  132. // expressions will be tested for matches in the order provided, so you
  133. // may need to move this value above the other in some cases. Obviously,
  134. // in the case of this example provider, you could easily improve the
  135. // regular expression to match against either a URL or embed code with
  136. // one expression, such as '@example\.com/[watch|embed]/([^"\?]+)@i'.
  137. // However, many other providers have more complex requirements, so
  138. // we split them up for this demonstration.
  139. '@example\.com/embed/([^"]+)=@i',
  140. );
  141. }
  142. /**
  143. * hook emvideo_PROVIDER_data
  144. *
  145. * Provides an array to be serialised and made available with $item elsewhere.
  146. *
  147. * This data can be used to store any extraneous information available
  148. * specifically to the example provider.
  149. */
  150. function emvideo_example_data($field, $item) {
  151. // Initialize the data array.
  152. $data = array();
  153. // Create some version control. Thus if we make changes to the data array
  154. // down the road, we can respect older content. If allowed by Embedded Media
  155. // Field, any older content will automatically update this array as needed.
  156. // In any case, you should account for the version if you increment it.
  157. $data['emvideo_example_version'] = EMVIDEO_EXAMPLE_DATA_VERSION;
  158. // We are using oEmbed to retrieve a standard set of data from the provider.
  159. // You should change the URL as specified by the example provider.
  160. // If the example provider does not support oEmbed, you must remove this
  161. // section entirely, or rewrite it to use their API.
  162. // See http://oembed.com/ for for information.
  163. $xml = emfield_request_xml('example', 'http://www.example.com/api/oembed.xml?url=http%3A//www.example.com/video/'. $item['value'], array(), TRUE, FALSE, $item['value']);
  164. // This stores a URL to the video's thumbnail.
  165. $data['thumbnail'] = $xml['OEMBED']['THUMBNAIL_URL'][0];
  166. return $data;
  167. }
  168. /**
  169. * hook emvideo_PROVIDER_rss
  170. *
  171. * This attaches a file to an RSS feed.
  172. */
  173. function emvideo_example_rss($item, $teaser = NULL) {
  174. if ($item['value']) {
  175. $file['thumbnail']['filepath'] = $item['data']['thumbnail'];
  176. return $file;
  177. }
  178. }
  179. /**
  180. * hook emvideo_PROVIDER_embedded_link($video_code)
  181. * returns a link to view the video at the provider's site.
  182. * @param $video_code
  183. * The string containing the video to watch.
  184. * @return
  185. * A string containing the URL to view the video at the original provider's site.
  186. */
  187. function emvideo_example_embedded_link($video_code) {
  188. return 'http://www.example.com/video/'. $video_code;
  189. }
  190. /**
  191. * The embedded flash displaying the example video.
  192. */
  193. function theme_emvideo_example_flash($item, $width, $height, $autoplay) {
  194. $output = '';
  195. if ($item['embed']) {
  196. $autoplay = $autoplay ? 'true' : 'false';
  197. $fullscreen = variable_get('emvideo_example_full_screen', 1) ? 'true' : 'false';
  198. $output = '';
  199. $output .= '';
  200. $output .= '';
  201. $output .= '';
  202. $output .= '';
  203. $output .= '';
  204. $output .= '';
  205. $output .= '';
  206. $output .= '
  207. ';
  208. }
  209. return $output;
  210. }
  211. /**
  212. * hook emvideo_PROVIDER_thumbnail
  213. * Returns the external url for a thumbnail of a specific video.
  214. * @param $field
  215. * The field of the requesting node.
  216. * @param $item
  217. * The actual content of the field from the requesting node.
  218. * @return
  219. * A URL pointing to the thumbnail.
  220. */
  221. function emvideo_example_thumbnail($field, $item, $formatter, $node, $width, $height) {
  222. // In this demonstration, we previously retrieved a thumbnail using oEmbed
  223. // during the data hook.
  224. return $data['thumbnail'];
  225. }
  226. /**
  227. * hook emvideo_PROVIDER_video
  228. * This actually displays the full/normal-sized video we want, usually on the
  229. * default page view.
  230. * @param $embed
  231. * The video code for the video to embed.
  232. * @param $width
  233. * The width to display the video.
  234. * @param $height
  235. * The height to display the video.
  236. * @param $field
  237. * The field info from the requesting node.
  238. * @param $item
  239. * The actual content from the field.
  240. * @return
  241. * The html of the embedded video.
  242. */
  243. function emvideo_example_video($embed, $width, $height, $field, $item, $node, $autoplay) {
  244. $output = theme('emvideo_example_flash', $item, $width, $height, $autoplay);
  245. return $output;
  246. }
  247. /**
  248. * hook emvideo_PROVIDER_video
  249. *
  250. * This actually displays the preview-sized video we want, commonly for the
  251. * teaser.
  252. * @param $embed
  253. * The video code for the video to embed.
  254. * @param $width
  255. * The width to display the video.
  256. * @param $height
  257. * The height to display the video.
  258. * @param $field
  259. * The field info from the requesting node.
  260. * @param $item
  261. * The actual content from the field.
  262. * @return
  263. * The html of the embedded video.
  264. */
  265. function emvideo_example_preview($embed, $width, $height, $field, $item, $node, $autoplay) {
  266. $output = theme('emvideo_example_flash', $item, $width, $height, $autoplay);
  267. return $output;
  268. }
  269. /**
  270. * Implementation of hook_emfield_subtheme.
  271. * This returns any theme functions defined by this provider.
  272. */
  273. function emvideo_example_emfield_subtheme() {
  274. $themes = array(
  275. 'emvideo_example_flash' => array(
  276. 'arguments' => array('item' => NULL, 'width' => NULL, 'height' => NULL, 'autoplay' => NULL),
  277. 'file' => 'providers/example.inc',
  278. // If you don't provide a 'path' value, then it will default to
  279. // the emvideo.module path. Obviously, replace 'emexample' with
  280. // the actual name of your custom module.
  281. 'path' => drupal_get_path('module', 'emexample'),
  282. )
  283. );
  284. return $themes;
  285. }