entity_view_mode.admin.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * List all custom view modes.
  4. */
  5. function entity_view_mode_list() {
  6. $custom_view_modes = variable_get('entity_view_modes', array());
  7. $entity_info = entity_get_info();
  8. $build = array(
  9. '#prefix' => '<div id="view-modes-accordion">',
  10. '#suffix' => '</div>',
  11. '#attached' => array(
  12. 'library' => array(
  13. //array('system', 'ui.accordion'),
  14. ),
  15. 'js' => array(
  16. //drupal_get_path('module', 'entity_view_mode') . '/entity_view_mode.admin.js',
  17. ),
  18. ),
  19. );
  20. foreach (array_keys($entity_info) as $entity_type) {
  21. if (empty($entity_info[$entity_type]['view modes'])) {
  22. continue;
  23. }
  24. $build[$entity_type] = array(
  25. '#type' => 'fieldset',
  26. '#title' => $entity_info[$entity_type]['label'],
  27. '#collapsible' => TRUE,
  28. );
  29. // Sort view modes by machine name.
  30. ksort($entity_info[$entity_type]['view modes']);
  31. $rows = array();
  32. foreach ($entity_info[$entity_type]['view modes'] as $view_mode => $view_mode_info) {
  33. $rows[$view_mode]['label'] = check_plain($view_mode_info['label']);
  34. $operations = array();
  35. if (isset($custom_view_modes[$entity_type][$view_mode])) {
  36. $operations['edit'] = array(
  37. 'title' => t('Edit'),
  38. 'href' => "admin/config/system/entity-view-modes/edit/{$entity_type}/{$view_mode}",
  39. );
  40. $operations['delete'] = array(
  41. 'title' => t('Delete'),
  42. 'href' => "admin/config/system/entity-view-modes/delete/{$entity_type}/{$view_mode}",
  43. );
  44. }
  45. if (!empty($operations)) {
  46. $rows[$view_mode]['operations'] = array(
  47. 'data' => array(
  48. '#theme' => 'links',
  49. '#links' => $operations,
  50. '#attributes' => array('class' => array('links', 'inline')),
  51. ),
  52. );
  53. }
  54. else {
  55. $rows[$view_mode]['operations'] = t('None (view mode locked)');
  56. }
  57. }
  58. $rows['_add_new'][] = array(
  59. 'data' => l(t('Add new view mode'), "admin/config/system/entity-view-modes/add/{$entity_type}"),
  60. 'colspan' => 2,
  61. );
  62. $build[$entity_type]['view_modes'] = array(
  63. '#theme' => 'table',
  64. '#header' => array(t('View mode'), t('Operations')),
  65. '#rows' => $rows,
  66. );
  67. }
  68. return $build;
  69. }
  70. /**
  71. * Form builder; edit a custom view mode.
  72. */
  73. function entity_view_mode_edit_form($form, &$form_state, $entity_type, $machine_name = NULL) {
  74. $form['#entity_type'] = $entity_type;
  75. $form['#entity_info'] = $entity_info = entity_get_info($entity_type);
  76. $view_mode = entity_view_mode_load($entity_type, $machine_name);
  77. if (empty($entity_info['view modes']) || (isset($machine_name) && empty($view_mode))) {
  78. drupal_not_found();
  79. drupal_exit();
  80. }
  81. $form['entity_type'] = array(
  82. '#type' => 'item',
  83. '#title' => t('Entity type'),
  84. '#markup' => $entity_info['label'],
  85. );
  86. $form['label'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('Label'),
  89. '#default_value' => isset($view_mode['label']) ? $view_mode['label'] : '',
  90. '#required' => TRUE,
  91. );
  92. $form['machine_name'] = array(
  93. '#type' => 'machine_name',
  94. '#machine_name' => array(
  95. 'source' => array('label'),
  96. 'exists' => 'entity_view_mode_exists',
  97. ),
  98. '#default_value' => $machine_name,
  99. '#entity_type' => $entity_type,
  100. );
  101. if (isset($machine_name)) {
  102. $form['old_machine_name'] = array(
  103. '#type' => 'value',
  104. '#value' => $machine_name,
  105. );
  106. }
  107. $form['actions'] = array('#type' => 'actions');
  108. $form['actions']['save'] = array(
  109. '#type' => 'submit',
  110. '#value' => t('Save'),
  111. );
  112. return $form;
  113. }
  114. function entity_view_mode_exists($machine_name, $element, $form_state) {
  115. $entity_info = entity_get_info($element['#entity_type']);
  116. return !empty($entity_info['view modes'][$machine_name]);
  117. }
  118. function entity_view_mode_edit_form_submit($form, &$form_state) {
  119. // Save the view mode.
  120. form_state_values_clean($form_state);
  121. $view_mode = $form_state['values'];
  122. entity_view_mode_save($form['#entity_type'], $view_mode);
  123. drupal_set_message(t('Saved the %view-mode @entity-type view mode.', array(
  124. '@entity-type' => drupal_strtolower($form['#entity_info']['label']),
  125. '%view-mode' => $view_mode['label'],
  126. )));
  127. $form_state['redirect'] = 'admin/config/system/entity-view-modes';
  128. }
  129. function entity_view_mode_delete_form(array $form, $form_state, $entity_type, $machine_name) {
  130. $form['#entity_type'] = $entity_type;
  131. $form['#entity_info'] = $entity_info = entity_get_info($entity_type);
  132. $form['#view_mode'] = $view_mode = entity_view_mode_load($entity_type, $machine_name);
  133. if (empty($form['#view_mode'])) {
  134. drupal_not_found();
  135. drupal_exit();
  136. }
  137. $form['machine_name'] = array(
  138. '#type' => 'value',
  139. '#value' => $machine_name,
  140. );
  141. return confirm_form(
  142. $form,
  143. t('Are you sure you want to delete the %view-mode @entity-type view mode?', array(
  144. '@entity-type' => drupal_strtolower($entity_info['label']),
  145. '%view-mode' => $view_mode['label'],
  146. )),
  147. 'admin/config/system/entity-view-modes',
  148. t('Deleting a view mode will cause any output still requesting to use that view mode to use the default display settings.'),
  149. t('Delete'),
  150. t('Cancel')
  151. );
  152. }
  153. function entity_view_mode_delete_form_submit($form, &$form_state) {
  154. entity_view_mode_delete($form['#entity_type'], $form_state['values']['machine_name']);
  155. drupal_set_message(t('Deleted the %view-mode @entity-type view mode.', array(
  156. '@entity-type' => drupal_strtolower($form['#entity_info']['label']),
  157. '%view-mode' => $form['#view_mode']['label'],
  158. )));
  159. $form_state['redirect'] = 'admin/config/system/entity-view-modes';
  160. }