media.fields.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * @file: Provides a "Multimedia asset" field to the fields API
  4. */
  5. /**
  6. * Implement hook_field_info().
  7. */
  8. function media_field_info() {
  9. return array(
  10. 'media' => array(
  11. 'label' => t('Multimedia asset (deprecated)'),
  12. 'description' => t('This field stores a reference to a multimedia asset.'),
  13. 'settings' => array(),
  14. 'instance_settings' => array(
  15. 'file_extensions' => media_variable_get('file_extensions'),
  16. ),
  17. 'default_widget' => 'media_generic',
  18. 'default_formatter' => 'media_large',
  19. 'property_type' => 'field_item_file',
  20. 'property_callbacks' => array('entity_metadata_field_file_callback'),
  21. ),
  22. );
  23. }
  24. /**
  25. * Implements hook_field_is_empty().
  26. */
  27. function media_field_is_empty($item, $field) {
  28. return empty($item['fid']);
  29. }
  30. /**
  31. * Implements hook_field_instance_settings_form().
  32. */
  33. function media_field_instance_settings_form($field, $instance) {
  34. $settings = $instance['settings'];
  35. // Make the extension list a little more human-friendly by comma-separation.
  36. $extensions = str_replace(' ', ', ', $settings['file_extensions']);
  37. $form['file_extensions'] = array(
  38. '#type' => 'textfield',
  39. '#title' => t('Allowed file extensions for uploaded files'),
  40. '#default_value' => $extensions,
  41. '#description' => t('Separate extensions with a space or comma and do not include the leading dot.'),
  42. '#element_validate' => array('_file_generic_settings_extensions'),
  43. // By making this field required, we prevent a potential security issue
  44. // that would allow files of any type to be uploaded.
  45. '#required' => TRUE,
  46. '#maxlength' => 255,
  47. );
  48. return $form;
  49. }
  50. /**
  51. * Implement hook_field_widget_info().
  52. */
  53. function media_field_widget_info() {
  54. return array(
  55. 'media_generic' => array(
  56. 'label' => t('Media file selector'),
  57. 'field types' => array('media', 'file', 'image'),
  58. 'settings' => array(
  59. 'progress_indicator' => 'throbber',
  60. 'allowed_types' => array('image'),
  61. 'allowed_schemes' => array('public', 'private'),
  62. ),
  63. 'behaviors' => array(
  64. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  65. 'default value' => FIELD_BEHAVIOR_NONE,
  66. ),
  67. ),
  68. );
  69. }
  70. /**
  71. * Implements hook_field_formatter_info().
  72. */
  73. function media_field_formatter_info() {
  74. $formatters = array(
  75. 'media_large_icon' => array(
  76. 'label' => t('Large filetype icon'),
  77. 'field types' => array('file'),
  78. ),
  79. // This was originally used when media entities contained file fields. The
  80. // current file entity architecture no longer needs this, but people may
  81. // have used this formatter for other file fields on their website.
  82. // @todo Some day, remove this.
  83. 'media' => array(
  84. 'label' => t('Media'),
  85. 'field types' => array('media'),
  86. 'settings' => array('file_view_mode' => 'default'),
  87. ),
  88. );
  89. return $formatters;
  90. }
  91. /**
  92. * Implements hook_field_formatter_settings_form().
  93. */
  94. function media_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  95. $display = $instance['display'][$view_mode];
  96. $settings = $display['settings'];
  97. $element = array();
  98. if ($display['type'] == 'media') {
  99. $entity_info = entity_get_info('file');
  100. $options = array('default' => t('Default'));
  101. foreach ($entity_info['view modes'] as $file_view_mode => $file_view_mode_info) {
  102. $options[$file_view_mode] = $file_view_mode_info['label'];
  103. }
  104. $element['file_view_mode'] = array(
  105. '#title' => t('File view mode'),
  106. '#type' => 'select',
  107. '#default_value' => $settings['file_view_mode'],
  108. '#options' => $options,
  109. );
  110. }
  111. return $element;
  112. }
  113. /**
  114. * Implements hook_field_formatter_settings_summary().
  115. */
  116. function media_field_formatter_settings_summary($field, $instance, $view_mode) {
  117. $display = $instance['display'][$view_mode];
  118. $settings = $display['settings'];
  119. $summary = '';
  120. if ($display['type'] == 'media') {
  121. $entity_info = entity_get_info('file');
  122. $file_view_mode_label = isset($entity_info['view modes'][$settings['file_view_mode']]) ? $entity_info['view modes'][$settings['file_view_mode']]['label'] : t('Default');
  123. $summary = t('File view mode: @view_mode', array('@view_mode' => $file_view_mode_label));
  124. }
  125. return $summary;
  126. }
  127. /**
  128. * Implements hook_field_prepare_view().
  129. */
  130. function media_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
  131. // Collect all file IDs that need loading.
  132. $fids = array();
  133. foreach ($entities as $id => $entity) {
  134. // Load the files from the files table.
  135. foreach ($items[$id] as $delta => $item) {
  136. if (!empty($item['fid'])) {
  137. $fids[] = $item['fid'];
  138. }
  139. }
  140. }
  141. // Load the file entities.
  142. $files = file_load_multiple($fids);
  143. // Add the loaded file entities to the field item array.
  144. foreach ($entities as $id => $entity) {
  145. foreach ($items[$id] as $delta => $item) {
  146. // If the file does not exist, mark the entire item as empty.
  147. if (empty($files[$item['fid']])) {
  148. unset($items[$id][$delta]);
  149. }
  150. else {
  151. $items[$id][$delta]['file'] = $files[$item['fid']];
  152. }
  153. }
  154. }
  155. }
  156. /**
  157. * Implement hook_field_formatter_view().
  158. */
  159. function media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  160. $element = array();
  161. if ($display['type'] == 'media_large_icon') {
  162. foreach ($items as $delta => $item) {
  163. $element[$delta] = array(
  164. '#theme' => 'media_formatter_large_icon',
  165. '#file' => (object) $item,
  166. );
  167. }
  168. }
  169. // Legacy support for the extra formatter added to file fields. See
  170. // media_field_formatter_info().
  171. if ($display['type'] == 'media') {
  172. $files = array();
  173. foreach ($items as $delta => $item) {
  174. if (!empty($item['file'])) {
  175. $files[$item['fid']] = $item['file'];
  176. }
  177. }
  178. if (!empty($files)) {
  179. $element = file_view_multiple($files, $display['settings']['file_view_mode'], 0, $langcode);
  180. }
  181. }
  182. return $element;
  183. }
  184. /**
  185. * Implement hook_field_widget_settings_form().
  186. */
  187. function media_field_widget_settings_form($field, $instance) {
  188. $widget = $instance['widget'];
  189. $settings = $widget['settings'];
  190. $form = array();
  191. // Setup type selection form
  192. $types = media_type_get_types();
  193. $options = array();
  194. foreach ($types as $key => $definition) {
  195. $options[$key] = $definition->label;
  196. }
  197. $streams = file_get_stream_wrappers();
  198. $form['allowed_types'] = array(
  199. '#type' => 'checkboxes',
  200. '#title' => t('Allowed remote media types'),
  201. '#options' => $options,
  202. '#default_value' => $settings['allowed_types'],
  203. '#description' => t('Media types which are allowed for this field when using remote streams.'),
  204. '#weight' => 1,
  205. '#access' => count(file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL)) != count($streams),
  206. );
  207. $options = array();
  208. unset($streams['temporary']);
  209. foreach ($streams as $scheme => $data) {
  210. $options[$scheme] = t('@scheme (@name)', array('@scheme' => $scheme . '://', '@name' => $data['name']));
  211. }
  212. $form['allowed_schemes'] = array(
  213. '#type' => 'checkboxes',
  214. '#title' => t('Allowed URI schemes'),
  215. '#options' => $options,
  216. '#default_value' => $settings['allowed_schemes'],
  217. '#description' => t('URI schemes include public:// and private:// which are the Drupal files directories, and may also refer to remote sites.'),
  218. '#weight' => 2,
  219. );
  220. return $form;
  221. }
  222. /**
  223. * Implements hook_field_widget_form().
  224. */
  225. function media_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  226. $field_settings = $instance['settings'];
  227. $widget_settings = $instance['widget']['settings'];
  228. // @todo The Field API supports automatic serialization / unserialization, so
  229. // this should no longer be needed. After verifying with a module that uses
  230. // the 'data' column, remove this.
  231. // @see media_field_widget_value()
  232. $current_value = array();
  233. if (isset($items[$delta])) {
  234. $current_value = $items[$delta];
  235. // @todo $items[$delta] is sometimes a loaded media entity (an object)
  236. // rather than an array. This conflicts with Field API expectations (for
  237. // example, it results in fatal errors when previewing a node with a
  238. // multi-valued media field), so should be fixed. In the meantime, don't
  239. // assume that $current_value is an array.
  240. if (is_array($current_value) && isset($current_value['data']) && is_string($current_value['data'])) {
  241. $current_value['data'] = unserialize($current_value['data']);
  242. }
  243. }
  244. $element += array(
  245. '#type' => 'media', // Would like to make this a fieldset, but throws some weird warning about element_children... not sure what it is about yet.
  246. '#collapsed' => TRUE,
  247. '#default_value' => $current_value,
  248. '#required' => $instance['required'],
  249. '#media_options' => array(
  250. 'global' => array(
  251. 'types' => array_filter($widget_settings['allowed_types']),
  252. 'schemes' => $widget_settings['allowed_schemes'],
  253. 'file_directory' => isset($field_settings['file_directory']) ? $field_settings['file_directory'] : '',
  254. 'file_extensions' => isset($field_settings['file_extensions']) ? $field_settings['file_extensions'] : media_variable_get('file_extensions'),
  255. 'max_filesize' => isset($field_settings['max_filesize']) ? $field_settings['max_filesize'] : 0,
  256. 'uri_scheme' => !empty($field['settings']['uri_scheme']) ? $field['settings']['uri_scheme'] : file_default_scheme(),
  257. ),
  258. ),
  259. );
  260. if ($field['type'] == 'file') {
  261. $element['display'] = array(
  262. '#type' => 'value',
  263. '#value' => 1,
  264. );
  265. }
  266. // Add image field specific validators.
  267. if ($field['type'] == 'image') {
  268. if ($field_settings['min_resolution'] || $field_settings['max_resolution']) {
  269. $element['#media_options']['global']['min_resolution'] = $field_settings['min_resolution'];
  270. $element['#media_options']['global']['max_resolution'] = $field_settings['max_resolution'];
  271. }
  272. }
  273. return $element;
  274. }
  275. /**
  276. * Implements hook_field_validate().
  277. *
  278. * Possible error codes:
  279. * - 'media_remote_file_type_not_allowed': The remote file is not an allowed
  280. * file type.
  281. */
  282. function media_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {
  283. $allowed_types = array_keys(array_filter($instance['widget']['settings']['allowed_types']));
  284. // @TODO: merge in stuff from media_uri_value
  285. foreach ($items as $delta => $item) {
  286. if (empty($item['fid'])) {
  287. return TRUE;
  288. //@TODO: make support for submiting with just a URI here?
  289. }
  290. $file = file_load($item['fid']);
  291. // Only validate allowed types if the file is remote and not local.
  292. if (!file_entity_file_is_local($file)) {
  293. if (!in_array($file->type, $allowed_types)) {
  294. $errors[$field['field_name']][$langcode][$delta][] = array(
  295. 'error' => 'media_remote_file_type_not_allowed',
  296. 'message' => t('%name: Only remote files with the following types are allowed: %types-allowed.', array('%name' => t($instance['label']), '%types-allowed' => !empty($allowed_types) ? implode(', ', $allowed_types) : t('no file types selected'))),
  297. );
  298. }
  299. }
  300. }
  301. }
  302. /**
  303. * Implements_hook_field_widget_error().
  304. */
  305. function media_field_widget_error($element, $error, $form, &$form_state) {
  306. form_error($element['fid'], $error['message']);
  307. }
  308. /**
  309. * @todo Is this function ever called? If not, remove it. The Field API now
  310. * supports automatic serialization / unserialization, so this should no
  311. * longer be needed. After verifying with a module that uses the 'data'
  312. * column, remove this.
  313. *
  314. * @see media_field_widget_form()
  315. */
  316. function media_field_widget_value($element, $input, $form_state) {
  317. $return = $input;
  318. if (!is_array($return)) {
  319. $return = array();
  320. }
  321. if (isset($return['data'])) {
  322. $return['data'] = serialize($return['data']);
  323. }
  324. $return += array(
  325. 'fid' => 0,
  326. 'title' => '',
  327. 'data' => NULL,
  328. );
  329. return $return;
  330. }
  331. /**
  332. * @todo The following hook_field_(insert|update|delete|delete_revision)
  333. * implementations are nearly identical to the File module implementations of
  334. * the same field hooks. The only differences are:
  335. * - We pass 'media' rather than 'file' as the module argument to the
  336. * file_usage_(add|delete)() functions.
  337. * - We do not delete the file / media entity when its usage count goes to 0.
  338. * We should submit a core patch to File module to make it flexible with
  339. * respect to the above, so that we can reuse its implementation rather than
  340. * duplicating it.
  341. */
  342. /**
  343. * Implements hook_field_insert().
  344. */
  345. function media_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  346. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  347. // Add a new usage of each uploaded file.
  348. foreach ($items as $item) {
  349. $file = (object) $item;
  350. file_usage_add($file, 'media', $entity_type, $id);
  351. }
  352. }
  353. /**
  354. * Implements hook_field_update().
  355. *
  356. * Checks for files that have been removed from the object.
  357. */
  358. function media_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  359. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  360. // On new revisions, all files are considered to be a new usage and no
  361. // deletion of previous file usages are necessary.
  362. if (!empty($entity->revision)) {
  363. foreach ($items as $item) {
  364. $file = (object) $item;
  365. file_usage_add($file, 'media', $entity_type, $id);
  366. }
  367. return;
  368. }
  369. // Build a display of the current FIDs.
  370. $current_fids = array();
  371. foreach ($items as $item) {
  372. $current_fids[] = $item['fid'];
  373. }
  374. // Compare the original field values with the ones that are being saved.
  375. $original_fids = array();
  376. if (!empty($entity->original->{$field['field_name']}[$langcode])) {
  377. foreach ($entity->original->{$field['field_name']}[$langcode] as $original_item) {
  378. $original_fids[] = $original_item['fid'];
  379. if (isset($original_item['fid']) && !in_array($original_item['fid'], $current_fids)) {
  380. // Decrement the file usage count by 1.
  381. $file = (object) $original_item;
  382. file_usage_delete($file, 'media', $entity_type, $id, 1);
  383. }
  384. }
  385. }
  386. // Add new usage entries for newly added files.
  387. foreach ($items as $item) {
  388. if (!in_array($item['fid'], $original_fids)) {
  389. $file = (object) $item;
  390. file_usage_add($file, 'media', $entity_type, $id);
  391. }
  392. }
  393. }
  394. /**
  395. * Implements hook_field_delete().
  396. */
  397. function media_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  398. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  399. // Delete all file usages within this entity.
  400. foreach ($items as $delta => $item) {
  401. $file = (object) $item;
  402. file_usage_delete($file, 'media', $entity_type, $id, 0);
  403. }
  404. }
  405. /**
  406. * Implements hook_field_delete_revision().
  407. */
  408. function media_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
  409. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  410. foreach ($items as $delta => $item) {
  411. // @TODO: Not sure if this is correct
  412. $file = (object)$item;
  413. if (file_usage_delete($file, 'media', $entity_type, $id, 1)) {
  414. $items[$delta] = NULL;
  415. }
  416. }
  417. }
  418. /**
  419. * Implements hook_field_instance_update().
  420. */
  421. function media_field_update_instance($instance, $prior_instance) {
  422. // Clear the filter cache when updating instance settings for a media entity.
  423. if ($instance['entity_type'] == 'media') {
  424. media_filter_invalidate_caches();
  425. }
  426. }