file_entity.field.inc

  1. cis7 sites/all/modules/ulmus/file_entity/file_entity.field.inc
  2. cle7 sites/all/modules/ulmus/file_entity/file_entity.field.inc
  3. ecd7 sites/all/modules/ulmus/file_entity/file_entity.field.inc
  4. elmsmedia7 sites/all/modules/ulmus/file_entity/file_entity.field.inc
  5. harmony7 sites/all/modules/ulmus/file_entity/file_entity.field.inc
  6. icor7 sites/all/modules/ulmus/file_entity/file_entity.field.inc
  7. meedjum_blog7 sites/all/modules/ulmus/file_entity/file_entity.field.inc
  8. mooc7 sites/all/modules/ulmus/file_entity/file_entity.field.inc

Field API integration for the file_entity module.

Functions

File

sites/all/modules/ulmus/file_entity/file_entity.field.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Field API integration for the file_entity module.
  5. */
  6. /**
  7. * Implements hook_field_formatter_info().
  8. */
  9. function file_entity_field_formatter_info() {
  10. $info['file_rendered'] = array(
  11. 'label' => t('Rendered file'),
  12. 'description' => t('Display the file in a specific view mode'),
  13. 'field types' => array('file', 'image'),
  14. 'settings' => array(
  15. 'file_view_mode' => 'default',
  16. ),
  17. 'file formatter' => array(
  18. 'hidden' => TRUE,
  19. ),
  20. );
  21. $info['file_download_link'] = array(
  22. 'label' => t('Download link'),
  23. 'description' => t('Displays a link that will force the browser to download the file.'),
  24. 'field types' => array('file', 'image'),
  25. 'settings' => array(
  26. 'text' => t('Download [file:name]'),
  27. ),
  28. );
  29. $info['file_audio'] = array(
  30. 'label' => t('Audio'),
  31. 'description' => t('Render the file using an HTML5 audio tag.'),
  32. 'field types' => array('file'),
  33. 'settings' => array(
  34. 'controls' => TRUE,
  35. 'autoplay' => FALSE,
  36. 'loop' => FALSE,
  37. 'multiple_file_behavior' => 'tags',
  38. ),
  39. 'file formatter' => array(
  40. 'mime types' => array('audio/*'),
  41. ),
  42. );
  43. $info['file_video'] = array(
  44. 'label' => t('Video'),
  45. 'description' => t('Render the file using an HTML5 video tag.'),
  46. 'field types' => array('file'),
  47. 'settings' => array(
  48. 'controls' => TRUE,
  49. 'autoplay' => FALSE,
  50. 'loop' => FALSE,
  51. 'muted' => FALSE,
  52. 'width' => NULL,
  53. 'height' => NULL,
  54. 'multiple_file_behavior' => 'tags',
  55. ),
  56. 'file formatter' => array(
  57. 'mime types' => array('video/*'),
  58. ),
  59. );
  60. return $info;
  61. }
  62. /**
  63. * Implements hook_field_formatter_info_alter().
  64. */
  65. function file_entity_field_formatter_info_alter(&$info) {
  66. // Add descriptions to core formatters.
  67. $descriptions = array(
  68. 'file_default' => t('Create a simple link to the file. The link is prefixed by a file type icon and the name of the file is used as the link text'),
  69. 'file_table' => t('Build a two-column table where the first column contains a generic link to the file and the second column displays the size of the file.'),
  70. 'file_url_plain' => t('Display a plain text URL to the file.'),
  71. 'image' => t('Format the file as an image. The image can be displayed using an image style and can optionally be linked to the image file itself or its parent content.'),
  72. );
  73. foreach ($descriptions as $key => $description) {
  74. if (isset($info[$key]) && empty($info[$key]['description'])) {
  75. $info[$key]['description'] = $description;
  76. }
  77. }
  78. // Formatters that can be used for images but not files, should have a
  79. // default mimetype restriction added to the image/* mime type for use with
  80. // file formatters.
  81. foreach ($info as &$formatter) {
  82. if (!isset($formatter['file formatter']) && in_array('image', $formatter['field types']) && !in_array('file', $formatter['field types'])) {
  83. $formatter['file formatter']['mime types'] = array('image/*');
  84. }
  85. }
  86. }
  87. /**
  88. * Implements hook_field_formatter_settings_form().
  89. */
  90. function file_entity_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  91. $display = $instance['display'][$view_mode];
  92. $settings = $display['settings'];
  93. $element = array();
  94. if ($display['type'] == 'file_rendered') {
  95. $element['file_view_mode'] = array(
  96. '#title' => t('View mode'),
  97. '#type' => 'select',
  98. '#options' => file_entity_view_mode_labels(),
  99. '#default_value' => $settings['file_view_mode'],
  100. // Never empty, so no #empty_option
  101. );
  102. }
  103. elseif ($display['type'] == 'file_download_link') {
  104. $element['text'] = array(
  105. '#type' => 'textfield',
  106. '#title' => t('Link text'),
  107. '#description' => t('This field support tokens.'),
  108. '#default_value' => $settings['text'],
  109. '#required' => TRUE,
  110. );
  111. }
  112. elseif ($display['type'] == 'file_audio') {
  113. $element['controls'] = array(
  114. '#title' => t('Show audio controls'),
  115. '#type' => 'checkbox',
  116. '#default_value' => $settings['controls'],
  117. );
  118. $element['autoplay'] = array(
  119. '#title' => t('Autoplay'),
  120. '#type' => 'checkbox',
  121. '#default_value' => $settings['autoplay'],
  122. );
  123. $element['loop'] = array(
  124. '#title' => t('Loop'),
  125. '#type' => 'checkbox',
  126. '#default_value' => $settings['loop'],
  127. );
  128. $element['multiple_file_behavior'] = array(
  129. '#title' => t('Display of multiple files'),
  130. '#type' => 'radios',
  131. '#options' => array(
  132. 'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<audio>')),
  133. 'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<audio>')),
  134. ),
  135. '#default_value' => $settings['multiple_file_behavior'],
  136. // Hide this setting in the manage file display configuration.
  137. '#access' => !empty($field),
  138. );
  139. }
  140. elseif ($display['type'] == 'file_video') {
  141. $element['controls'] = array(
  142. '#title' => t('Show video controls'),
  143. '#type' => 'checkbox',
  144. '#default_value' => $settings['controls'],
  145. );
  146. $element['autoplay'] = array(
  147. '#title' => t('Autoplay'),
  148. '#type' => 'checkbox',
  149. '#default_value' => $settings['autoplay'],
  150. );
  151. $element['loop'] = array(
  152. '#title' => t('Loop'),
  153. '#type' => 'checkbox',
  154. '#default_value' => $settings['loop'],
  155. );
  156. $element['width'] = array(
  157. '#type' => 'textfield',
  158. '#title' => t('Width'),
  159. '#default_value' => $settings['width'],
  160. '#size' => 5,
  161. '#maxlength' => 5,
  162. '#field_suffix' => t('pixels'),
  163. );
  164. $element['height'] = array(
  165. '#type' => 'textfield',
  166. '#title' => t('Height'),
  167. '#default_value' => $settings['height'],
  168. '#size' => 5,
  169. '#maxlength' => 5,
  170. '#field_suffix' => t('pixels'),
  171. );
  172. $element['multiple_file_behavior'] = array(
  173. '#title' => t('Display of multiple files'),
  174. '#type' => 'radios',
  175. '#options' => array(
  176. 'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<video>')),
  177. 'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<video>')),
  178. ),
  179. '#default_value' => $settings['multiple_file_behavior'],
  180. // Hide this setting in the manage file display configuration.
  181. '#access' => !empty($field),
  182. );
  183. }
  184. return $element;
  185. }
  186. /**
  187. * Implements hook_field_formatter_settings_summary().
  188. */
  189. function file_entity_field_formatter_settings_summary($field, $instance, $view_mode) {
  190. $display = $instance['display'][$view_mode];
  191. $settings = $display['settings'];
  192. $summary = array();
  193. if ($display['type'] === 'file_rendered') {
  194. $view_mode_label = file_entity_view_mode_label($settings['file_view_mode'], t('Unknown'));
  195. $summary[] = t('View mode: %mode', array('%mode' => $view_mode_label));
  196. }
  197. elseif ($display['type'] == 'file_download_link') {
  198. $summary[] = t('Link text: %text', array('%text' => $settings['text']));
  199. }
  200. elseif ($display['type'] === 'file_audio') {
  201. if (isset($settings['controls'])) {
  202. $summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
  203. }
  204. if (isset($settings['autoplay'])) {
  205. $summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
  206. }
  207. if (isset($settings['loop'])) {
  208. $summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
  209. }
  210. if (isset($settings['multiple_file_behavior'])) {
  211. $summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
  212. }
  213. }
  214. elseif ($display['type'] === 'file_video') {
  215. $summary_items = array();
  216. if (isset($settings['controls'])) {
  217. $summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
  218. }
  219. if (isset($settings['autoplay'])) {
  220. $summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
  221. }
  222. if (isset($settings['loop'])) {
  223. $summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
  224. }
  225. if (isset($settings['muted'])) {
  226. $summary[] = t('Muted: %muted', array('%muted' => $settings['muted'] ? t('yes') : t('no')));
  227. }
  228. if ($settings['width'] && $settings['height']) {
  229. $summary[] = t('Size: %width x %height', array('%width' => $settings['width'], '%height' => $settings['height']));
  230. }
  231. if (isset($settings['multiple_file_behavior'])) {
  232. $summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
  233. }
  234. }
  235. return implode('<br />', $summary);
  236. }
  237. /**
  238. * Implements hook_field_formatter_view().
  239. */
  240. function file_entity_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  241. $settings = $display['settings'];
  242. $element = array();
  243. if ($display['type'] == 'file_rendered') {
  244. $view_mode = $settings['file_view_mode'];
  245. // To prevent infinite recursion caused by reference cycles, we store
  246. // diplayed nodes in a recursion queue.
  247. $recursion_queue = &drupal_static(__FUNCTION__, array());
  248. // If no 'referencing entity' is set, we are starting a new 'reference
  249. // thread' and need to reset the queue.
  250. // @todo Bug: $entity->referencing_entity on files referenced in a different
  251. // thread on the page.
  252. // E.g: 1 references 1+2 / 2 references 1+2 / visit homepage.
  253. // We'd need a more accurate way...
  254. if (!isset($entity->referencing_entity)) {
  255. $recursion_queue = array();
  256. }
  257. // The recursion queue only needs to track files.
  258. if ($entity_type == 'file') {
  259. list($id) = entity_extract_ids($entity_type, $entity);
  260. $recursion_queue[$id] = $id;
  261. }
  262. // Prevent 'empty' fields from causing a WSOD.
  263. $items = array_filter($items);
  264. // Check the recursion queue to determine which nodes should be fully
  265. // displayed, and which nodes will only be displayed as a title.
  266. $files_display = array();
  267. foreach ($items as $delta => $item) {
  268. if (!isset($recursion_queue[$item['fid']])) {
  269. $files_display[$item['fid']] = file_load($item['fid']);
  270. if (!empty($item['description'])) {
  271. $files_display[$item['fid']]->description = $item['description'];
  272. }
  273. }
  274. }
  275. // Load and build the fully displayed nodes.
  276. if ($files_display) {
  277. foreach ($files_display as $fid => $file) {
  278. $files_display[$fid]->referencing_entity = $entity;
  279. $files_display[$fid]->referencing_field = $field['field_name'];
  280. }
  281. $output = file_view_multiple($files_display, $view_mode);
  282. // Remove the first level from the output array.
  283. $files_built = reset($output);
  284. }
  285. // Assemble the render array.
  286. foreach ($items as $delta => $item) {
  287. if (isset($files_built[$item['fid']])) {
  288. $element[$delta] = $files_built[$item['fid']];
  289. }
  290. }
  291. }
  292. elseif ($display['type'] == 'file_download_link') {
  293. foreach ($items as $delta => $item) {
  294. $file = (object) $item;
  295. if (file_entity_file_is_local($file) && file_entity_access('download', $file)) {
  296. $element[$delta] = array(
  297. '#theme' => 'file_entity_download_link',
  298. '#file' => $file,
  299. '#text' => $settings['text'],
  300. );
  301. }
  302. }
  303. }
  304. elseif ($display['type'] == 'file_audio') {
  305. $multiple_file_behavior = $settings['multiple_file_behavior'];
  306. // Prevent 'empty' fields from causing a WSOD.
  307. $items = array_filter($items);
  308. // Build an array of sources for each <audio> element.
  309. $source_lists = array();
  310. if ($multiple_file_behavior == 'tags') {
  311. foreach ($items as $delta => $item) {
  312. if ($item['type'] == 'audio') {
  313. $source_lists[] = array($item);
  314. }
  315. }
  316. }
  317. else {
  318. foreach ($items as $delta => $item) {
  319. if ($item['type'] == 'audio') {
  320. $source_lists[0][] = $item;
  321. }
  322. }
  323. }
  324. // Render each source list as an <audio> element.
  325. foreach ($source_lists as $delta => $sources) {
  326. $element[$delta] = array(
  327. '#theme' => 'file_entity_file_audio',
  328. '#files' => $sources,
  329. '#controls' => $settings['controls'],
  330. '#autoplay' => $settings['autoplay'],
  331. '#loop' => $settings['loop'],
  332. );
  333. }
  334. }
  335. elseif ($display['type'] == 'file_video') {
  336. $multiple_file_behavior = $settings['multiple_file_behavior'];
  337. // Prevent 'empty' fields from causing a WSOD.
  338. $items = array_filter($items);
  339. // Build an array of sources for each <video> element.
  340. $source_lists = array();
  341. if ($multiple_file_behavior == 'tags') {
  342. foreach ($items as $delta => $item) {
  343. if ($item['type'] == 'video') {
  344. $source_lists[] = array($item);
  345. }
  346. }
  347. }
  348. else {
  349. foreach ($items as $delta => $item) {
  350. if ($item['type'] == 'video') {
  351. $source_lists[0][] = $item;
  352. }
  353. }
  354. }
  355. // Render each source list as an <video> element.
  356. foreach ($source_lists as $delta => $sources) {
  357. $width = $settings['width'];
  358. $height = $settings['height'];
  359. $element[$delta] = array(
  360. '#theme' => 'file_entity_file_video',
  361. '#files' => $sources,
  362. '#controls' => $settings['controls'],
  363. '#autoplay' => $settings['autoplay'],
  364. '#loop' => $settings['loop'],
  365. '#muted' => $settings['muted'],
  366. '#width' => ($width && $height) ? $width : NULL,
  367. '#height' => ($width && $height) ? $height : NULL,
  368. );
  369. }
  370. }
  371. return $element;
  372. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.