entity_view_mode.module 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Implements hook_permission().
  4. */
  5. function entity_view_mode_permission() {
  6. return array(
  7. 'administer entity view modes' => array(
  8. 'title' => t('Add, edit and delete custom entity view modes.'),
  9. ),
  10. );
  11. }
  12. /**
  13. * Implements hook_entity_info_alter().
  14. */
  15. function entity_view_mode_entity_info_alter(&$info) {
  16. foreach (variable_get('entity_view_modes', array()) as $entity_type => $view_modes) {
  17. if (isset($info[$entity_type]['view modes'])) {
  18. $info[$entity_type]['view modes'] += $view_modes;
  19. }
  20. }
  21. }
  22. /**
  23. * Implements hook_menu().
  24. */
  25. function entity_view_mode_menu() {
  26. $items['admin/config/system/entity-view-modes'] = array(
  27. 'title' => 'Entity view modes',
  28. 'page callback' => 'entity_view_mode_list',
  29. 'access arguments' => array('administer entity view modes'),
  30. 'file' => 'entity_view_mode.admin.inc',
  31. );
  32. $items['admin/config/system/entity-view-modes/add/%'] = array(
  33. 'title' => 'Add view mode',
  34. 'page callback' => 'drupal_get_form',
  35. 'page arguments' => array('entity_view_mode_edit_form', 5),
  36. 'access arguments' => array('administer entity view modes'),
  37. 'file' => 'entity_view_mode.admin.inc',
  38. );
  39. $items['admin/config/system/entity-view-modes/edit/%/%'] = array(
  40. 'title' => 'Edit view mode',
  41. 'page callback' => 'drupal_get_form',
  42. 'page arguments' => array('entity_view_mode_edit_form', 5, 6),
  43. 'access arguments' => array('administer entity view modes'),
  44. 'file' => 'entity_view_mode.admin.inc',
  45. );
  46. $items['admin/config/system/entity-view-modes/delete/%/%'] = array(
  47. 'title' => 'Edit view mode',
  48. 'page callback' => 'drupal_get_form',
  49. 'page arguments' => array('entity_view_mode_delete_form', 5, 6),
  50. 'access arguments' => array('administer entity view modes'),
  51. 'file' => 'entity_view_mode.admin.inc',
  52. );
  53. return $items;
  54. }
  55. /**
  56. * Implements hook_menu_local_tasks_alter() {
  57. */
  58. //function entity_view_mode_local_tasks_alter(&$data, $router_item, $root_path) {
  59. //}
  60. /**
  61. * Implements hook_form_FORM_ID_alter().
  62. */
  63. //function entity_view_mode_field_ui_display_overview_form_alter(&$form, &$form_state) {
  64. //}
  65. /**
  66. * Load a custom entity view mode by entity type and machine name.
  67. */
  68. function entity_view_mode_load($entity_type, $machine_name) {
  69. $view_modes = variable_get('entity_view_modes', array());
  70. return !empty($view_modes[$entity_type][$machine_name]) ? $view_modes[$entity_type][$machine_name] : FALSE;
  71. }
  72. /**
  73. * Save a custom entity view mode.
  74. */
  75. function entity_view_mode_save($entity_type, $view_mode) {
  76. $existing_view_mode = !empty($view_mode['old_machine_name']) ? $view_mode['old_machine_name'] : $view_mode['machine_name'];
  77. // Load the stored view mode, if any.
  78. if (!isset($view_mode['original'])) {
  79. $view_mode['original'] = entity_view_mode_load($entity_type, $existing_view_mode);
  80. }
  81. // Determine if we will be inserting a new view mode.
  82. if (!isset($view_mode['is_new'])) {
  83. $view_mode['is_new'] = !empty($view_mode['original']);
  84. }
  85. // Let modules modify the view mode before it is saved.
  86. module_invoke_all('entity_view_mode_presave', $view_mode, $entity_type);
  87. // Save the view mode.
  88. $view_modes = variable_get('entity_view_modes', array());
  89. unset($view_modes[$entity_type][$existing_view_mode]);
  90. $view_modes[$entity_type][$view_mode['machine_name']] = array_intersect_key($view_mode, drupal_map_assoc(array('label'))) + array('custom settings' => FALSE);
  91. variable_set('entity_view_modes', $view_modes);
  92. // Allow modules to respond after the view mode is saved.
  93. if ($view_mode['is_new']) {
  94. module_invoke_all('entity_view_mode_insert', $view_mode, $entity_type);
  95. }
  96. else {
  97. module_invoke_all('entity_view_mode_update', $view_mode, $entity_type);
  98. }
  99. // Clear internal properties.
  100. unset($view_mode['original']);
  101. unset($view_mode['is_new']);
  102. // Clear the static entity info cache.
  103. entity_info_cache_clear();
  104. }
  105. function entity_view_mode_delete($entity_type, $machine_name) {
  106. if ($view_mode = entity_view_mode_load($entity_type, $machine_name)) {
  107. module_invoke_all('entity_view_mode_delete', $view_mode, $entity_type);
  108. $view_modes = variable_get('entity_view_modes', array());
  109. unset($view_modes[$entity_type][$machine_name]);
  110. variable_set('entity_view_modes', $view_modes);
  111. // Clear the static entity info cache.
  112. entity_info_cache_clear();
  113. }
  114. }