file_entity.field.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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('file_view_mode' => 'default'),
  15. );
  16. return $info;
  17. }
  18. /**
  19. * Implements hook_field_formatter_settings_form().
  20. */
  21. function file_entity_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  22. $display = $instance['display'][$view_mode];
  23. $settings = $display['settings'];
  24. $element = array();
  25. if ($display['type'] == 'file_rendered') {
  26. $element['file_view_mode'] = array(
  27. '#title' => t('View mode'),
  28. '#type' => 'select',
  29. '#options' => file_entity_view_mode_labels(),
  30. '#default_value' => $settings['file_view_mode'],
  31. // Never empty, so no #empty_option
  32. );
  33. }
  34. return $element;
  35. }
  36. /**
  37. * Implements hook_field_formatter_settings_summary().
  38. */
  39. function file_entity_field_formatter_settings_summary($field, $instance, $view_mode) {
  40. $display = $instance['display'][$view_mode];
  41. $settings = $display['settings'];
  42. $summary = NULL;
  43. if ($display['type'] === 'file_rendered') {
  44. $view_mode_label = file_entity_view_mode_label($settings['file_view_mode'], t('Unknown'));
  45. $summary = t('View mode: %mode', array('%mode' => $view_mode_label));
  46. }
  47. return $summary;
  48. }
  49. /**
  50. * Implements hook_field_formatter_view().
  51. */
  52. function file_entity_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  53. $settings = $display['settings'];
  54. $element = array();
  55. if ($display['type'] == 'file_rendered') {
  56. $view_mode = $settings['file_view_mode'];
  57. // To prevent infinite recursion caused by reference cycles, we store
  58. // diplayed nodes in a recursion queue.
  59. $recursion_queue = &drupal_static(__FUNCTION__, array());
  60. // If no 'referencing entity' is set, we are starting a new 'reference
  61. // thread' and need to reset the queue.
  62. // @todo Bug: $entity->referencing_entity on files referenced in a different
  63. // thread on the page. E.g: 1 references 1+2 / 2 references 1+2 / visit homepage.
  64. // We'd need a more accurate way...
  65. if (!isset($entity->referencing_entity)) {
  66. $recursion_queue = array();
  67. }
  68. // The recursion queue only needs to track files.
  69. if ($entity_type == 'file') {
  70. list($id) = entity_extract_ids($entity_type, $entity);
  71. $recursion_queue[$id] = $id;
  72. }
  73. // Prevent 'empty' fields from causing a WSOD.
  74. $items = array_filter($items);
  75. // Check the recursion queue to determine which nodes should be fully
  76. // displayed, and which nodes will only be displayed as a title.
  77. $files_display = array();
  78. foreach ($items as $delta => $item) {
  79. if (!isset($recursion_queue[$item['fid']])) {
  80. $files_display[$item['fid']] = file_load($item['fid']);
  81. }
  82. }
  83. // Load and build the fully displayed nodes.
  84. if ($files_display) {
  85. foreach ($files_display as $fid => $file) {
  86. $files_display[$fid]->referencing_entity = $entity;
  87. $files_display[$fid]->referencing_field = $field['field_name'];
  88. }
  89. $files_built = file_view_multiple($files_display, $view_mode);
  90. }
  91. // Assemble the render array.
  92. foreach ($items as $delta => $item) {
  93. if (isset($files_built[$item['fid']])) {
  94. $element[$delta] = $files_built[$item['fid']];
  95. }
  96. }
  97. }
  98. return $element;
  99. }