file_entity.admin.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @file
  4. * Administrative interface for file type configuration.
  5. */
  6. /**
  7. * Displays the file type admin overview page.
  8. */
  9. function file_entity_list_types_page() {
  10. $types = file_info_file_types();
  11. $entity_info = entity_get_info('file');
  12. $field_ui = module_exists('field_ui');
  13. $header = array(
  14. array('data' => t('Name')),
  15. array('data' => t('Operations'), 'colspan' => $field_ui ? '3' : '1'),
  16. );
  17. $rows = array();
  18. foreach ($types as $type => $info) {
  19. $row = array(array('data' => theme('file_entity_file_type_overview', $info)));
  20. $path = isset($entity_info['bundles'][$type]['admin']['real path']) ? $entity_info['bundles'][$type]['admin']['real path'] : NULL;
  21. if ($field_ui) {
  22. $row[] = array('data' => isset($path) ? l(t('manage fields'), $path . '/fields') : '');
  23. $row[] = array('data' => isset($path) ? l(t('manage display'), $path . '/display') : '');
  24. }
  25. $row[] = array('data' => isset($path) ? l(t('manage file display'), $path . '/file-display') : '');
  26. $rows[] = $row;
  27. }
  28. $build['file_type_table'] = array(
  29. '#theme' => 'table',
  30. '#header' => $header,
  31. '#rows' => $rows,
  32. '#empty' => t('No file types available.'),
  33. );
  34. return $build;
  35. }
  36. /**
  37. * Form callback; presents file display settings for a given view mode.
  38. */
  39. function file_entity_file_display_form($form, &$form_state, $bundle, $view_mode) {
  40. $file_type = field_extract_bundle('file', $bundle);
  41. $form['#file_type'] = $file_type;
  42. $form['#view_mode'] = $view_mode;
  43. $form['#tree'] = TRUE;
  44. $form['#attached']['js'][] = drupal_get_path('module', 'file_entity') . '/file_entity.admin.js';
  45. // Retrieve available formatters for this file type and load all configured
  46. // filters for existing text formats.
  47. $formatters = file_info_formatter_types();
  48. foreach ($formatters as $name => $formatter) {
  49. if (isset($formatter['file types']) && !in_array($file_type, $formatter['file types'])) {
  50. unset ($formatters[$name]);
  51. }
  52. }
  53. $current_displays = file_displays_load($file_type, $view_mode, TRUE);
  54. foreach ($current_displays as $name => $display) {
  55. $current_displays[$name] = (array) $display;
  56. }
  57. // Formatter status.
  58. $form['displays']['status'] = array(
  59. '#type' => 'item',
  60. '#title' => t('Enabled displays'),
  61. '#prefix' => '<div id="file-displays-status-wrapper">',
  62. '#suffix' => '</div>',
  63. );
  64. $i=0;
  65. foreach ($formatters as $name => $formatter) {
  66. $form['displays']['status'][$name] = array(
  67. '#type' => 'checkbox',
  68. '#title' => $formatter['label'],
  69. '#default_value' => !empty($current_displays[$name]['status']),
  70. '#description' => isset($formatter['description']) ? $formatter['description'] : NULL,
  71. '#parents' => array('displays', $name, 'status'),
  72. '#weight' => $formatter['weight'] + $i/1000,
  73. );
  74. $i++;
  75. }
  76. // Formatter order (tabledrag).
  77. $form['displays']['order'] = array(
  78. '#type' => 'item',
  79. '#title' => t('Display precedence order'),
  80. '#theme' => 'file_entity_file_display_order',
  81. );
  82. foreach ($formatters as $name => $formatter) {
  83. $form['displays']['order'][$name]['label'] = array(
  84. '#markup' => check_plain($formatter['label']),
  85. );
  86. $form['displays']['order'][$name]['weight'] = array(
  87. '#type' => 'weight',
  88. '#title' => t('Weight for @title', array('@title' => $formatter['label'])),
  89. '#title_display' => 'invisible',
  90. '#delta' => 50,
  91. '#default_value' => isset($current_displays[$name]['weight']) ? $current_displays[$name]['weight'] : 0,
  92. '#parents' => array('displays', $name, 'weight'),
  93. );
  94. $form['displays']['order'][$name]['#weight'] = $form['displays']['order'][$name]['weight']['#default_value'];
  95. }
  96. // Formatter settings.
  97. $form['display_settings_title'] = array(
  98. '#type' => 'item',
  99. '#title' => t('Display settings'),
  100. );
  101. $form['display_settings'] = array(
  102. '#type' => 'vertical_tabs',
  103. );
  104. $i=0;
  105. foreach ($formatters as $name => $formatter) {
  106. if (isset($formatter['settings callback']) && ($function = $formatter['settings callback']) && function_exists($function)) {
  107. $defaults = !empty($formatter['default settings']) ? $formatter['default settings'] : array();
  108. $settings = !empty($current_displays[$name]['settings']) ? $current_displays[$name]['settings'] : array();
  109. $settings += $defaults;
  110. $settings_form = $function($form, $form_state, $settings, $name, $file_type, $view_mode);
  111. if (!empty($settings_form)) {
  112. $form['displays']['settings'][$name] = array(
  113. '#type' => 'fieldset',
  114. '#title' => $formatter['label'],
  115. '#parents' => array('displays', $name, 'settings'),
  116. '#group' => 'display_settings',
  117. '#weight' => $formatter['weight'] + $i/1000,
  118. ) + $settings_form;
  119. }
  120. }
  121. $i++;
  122. }
  123. $form['actions'] = array('#type' => 'actions');
  124. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  125. return $form;
  126. }
  127. /**
  128. * Process file display settings form submissions.
  129. */
  130. function file_entity_file_display_form_submit($form, &$form_state) {
  131. $file_type = $form['#file_type'];
  132. $view_mode = $form['#view_mode'];
  133. $displays = isset($form_state['values']['displays']) ? $form_state['values']['displays'] : array();
  134. $displays_original = file_displays_load($file_type, $view_mode, TRUE);
  135. foreach ($displays as $formatter_name => $display) {
  136. $display_original = isset($displays_original[$formatter_name]) ? $displays_original[$formatter_name] : file_display_new($file_type, $view_mode, $formatter_name);
  137. $display += (array) $display_original;
  138. file_display_save((object) $display);
  139. }
  140. drupal_set_message(t('Your settings have been saved.'));
  141. }
  142. /**
  143. * Returns HTML for a file type label and description for the file type admin overview page.
  144. */
  145. function theme_file_entity_file_type_overview($variables) {
  146. return check_plain($variables['label']) . '<div class="description">' . $variables['description'] . '</div>';
  147. }
  148. /**
  149. * Returns HTML for a file display's display order table.
  150. */
  151. function theme_file_entity_file_display_order($variables) {
  152. $element = $variables['element'];
  153. $rows = array();
  154. foreach (element_children($element, TRUE) as $name) {
  155. $element[$name]['weight']['#attributes']['class'][] = 'file-display-order-weight';
  156. $rows[] = array(
  157. 'data' => array(
  158. drupal_render($element[$name]['label']),
  159. drupal_render($element[$name]['weight']),
  160. ),
  161. 'class' => array('draggable'),
  162. );
  163. }
  164. $output = drupal_render_children($element);
  165. $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'file-displays-order')));
  166. drupal_add_tabledrag('file-displays-order', 'order', 'sibling', 'file-display-order-weight', NULL, NULL, TRUE);
  167. return $output;
  168. }