filter.admin.inc

  1. 7.x drupal-7.x/modules/filter/filter.admin.inc
  2. 6.x drupal-6.x/modules/filter/filter.admin.inc

Administrative page callbacks for the Filter module.

File

drupal-7.x/modules/filter/filter.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the Filter module.
  5. */
  6. /**
  7. * Page callback: Form constructor for a form to list and reorder text formats.
  8. *
  9. * @ingroup forms
  10. * @see filter_menu()
  11. * @see filter_admin_overview_submit()
  12. */
  13. function filter_admin_overview($form) {
  14. // Overview of all formats.
  15. $formats = filter_formats();
  16. $fallback_format = filter_fallback_format();
  17. $form['#tree'] = TRUE;
  18. foreach ($formats as $id => $format) {
  19. // Check whether this is the fallback text format. This format is available
  20. // to all roles and cannot be disabled via the admin interface.
  21. $form['formats'][$id]['#is_fallback'] = ($id == $fallback_format);
  22. if ($form['formats'][$id]['#is_fallback']) {
  23. $form['formats'][$id]['name'] = array('#markup' => drupal_placeholder($format->name));
  24. $roles_markup = drupal_placeholder(t('All roles may use this format'));
  25. }
  26. else {
  27. $form['formats'][$id]['name'] = array('#markup' => check_plain($format->name));
  28. $roles = array_map('check_plain', filter_get_roles_by_format($format));
  29. $roles_markup = $roles ? implode(', ', $roles) : t('No roles may use this format');
  30. }
  31. $form['formats'][$id]['roles'] = array('#markup' => $roles_markup);
  32. $form['formats'][$id]['configure'] = array('#type' => 'link', '#title' => t('configure'), '#href' => 'admin/config/content/formats/' . $id);
  33. $form['formats'][$id]['disable'] = array('#type' => 'link', '#title' => t('disable'), '#href' => 'admin/config/content/formats/' . $id . '/disable', '#access' => !$form['formats'][$id]['#is_fallback']);
  34. $form['formats'][$id]['weight'] = array(
  35. '#type' => 'weight',
  36. '#title' => t('Weight for @title', array('@title' => $format->name)),
  37. '#title_display' => 'invisible',
  38. '#default_value' => $format->weight,
  39. );
  40. }
  41. $form['actions'] = array('#type' => 'actions');
  42. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
  43. return $form;
  44. }
  45. /**
  46. * Form submission handler for filter_admin_overview().
  47. */
  48. function filter_admin_overview_submit($form, &$form_state) {
  49. foreach ($form_state['values']['formats'] as $id => $data) {
  50. if (is_array($data) && isset($data['weight'])) {
  51. // Only update if this is a form element with weight.
  52. db_update('filter_format')
  53. ->fields(array('weight' => $data['weight']))
  54. ->condition('format', $id)
  55. ->execute();
  56. }
  57. }
  58. filter_formats_reset();
  59. drupal_set_message(t('The text format ordering has been saved.'));
  60. }
  61. /**
  62. * Returns HTML for the text format administration overview form.
  63. *
  64. * @param $variables
  65. * An associative array containing:
  66. * - form: A render element representing the form.
  67. *
  68. * @ingroup themeable
  69. */
  70. function theme_filter_admin_overview($variables) {
  71. $form = $variables['form'];
  72. $rows = array();
  73. foreach (element_children($form['formats']) as $id) {
  74. $form['formats'][$id]['weight']['#attributes']['class'] = array('text-format-order-weight');
  75. $rows[] = array(
  76. 'data' => array(
  77. drupal_render($form['formats'][$id]['name']),
  78. drupal_render($form['formats'][$id]['roles']),
  79. drupal_render($form['formats'][$id]['weight']),
  80. drupal_render($form['formats'][$id]['configure']),
  81. drupal_render($form['formats'][$id]['disable']),
  82. ),
  83. 'class' => array('draggable'),
  84. );
  85. }
  86. $header = array(t('Name'), t('Roles'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
  87. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-format-order')));
  88. $output .= drupal_render_children($form);
  89. drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');
  90. return $output;
  91. }
  92. /**
  93. * Page callback: Displays the text format add/edit form.
  94. *
  95. * @param object|null $format
  96. * (optional) An object representing a format, with the following properties:
  97. * - format: A machine-readable name representing the ID of the text format
  98. * to save. If this corresponds to an existing text format, that format
  99. * will be updated; otherwise, a new format will be created.
  100. * - name: The title of the text format.
  101. * - cache: (optional) An integer indicating whether the text format is
  102. * cacheable (1) or not (0). Defaults to 1.
  103. * - status: (optional) An integer indicating whether the text format is
  104. * enabled (1) or not (0). Defaults to 1.
  105. * - weight: (optional) The weight of the text format, which controls its
  106. * placement in text format lists. If omitted, the weight is set to 0.
  107. * Defaults to NULL.
  108. *
  109. * @return
  110. * A form array.
  111. *
  112. * @see filter_menu()
  113. */
  114. function filter_admin_format_page($format = NULL) {
  115. if (!isset($format->name)) {
  116. drupal_set_title(t('Add text format'));
  117. $format = (object) array(
  118. 'format' => NULL,
  119. 'name' => '',
  120. );
  121. }
  122. return drupal_get_form('filter_admin_format_form', $format);
  123. }
  124. /**
  125. * Form constructor for the text format add/edit form.
  126. *
  127. * @param $format
  128. * A format object having the properties:
  129. * - format: A machine-readable name representing the ID of the text format to
  130. * save. If this corresponds to an existing text format, that format will be
  131. * updated; otherwise, a new format will be created.
  132. * - name: The title of the text format.
  133. * - cache: An integer indicating whether the text format is cacheable (1) or
  134. * not (0). Defaults to 1.
  135. * - status: (optional) An integer indicating whether the text format is
  136. * enabled (1) or not (0). Defaults to 1.
  137. * - weight: (optional) The weight of the text format, which controls its
  138. * placement in text format lists. If omitted, the weight is set to 0.
  139. *
  140. * @see filter_admin_format_form_validate()
  141. * @see filter_admin_format_form_submit()
  142. * @ingroup forms
  143. */
  144. function filter_admin_format_form($form, &$form_state, $format) {
  145. $is_fallback = ($format->format == filter_fallback_format());
  146. $form['#format'] = $format;
  147. $form['#tree'] = TRUE;
  148. $form['#attached']['js'][] = drupal_get_path('module', 'filter') . '/filter.admin.js';
  149. $form['#attached']['css'][] = drupal_get_path('module', 'filter') . '/filter.css';
  150. $form['name'] = array(
  151. '#type' => 'textfield',
  152. '#title' => t('Name'),
  153. '#default_value' => $format->name,
  154. '#required' => TRUE,
  155. );
  156. $form['format'] = array(
  157. '#type' => 'machine_name',
  158. '#required' => TRUE,
  159. '#default_value' => $format->format,
  160. '#maxlength' => 255,
  161. '#machine_name' => array(
  162. 'exists' => 'filter_format_exists',
  163. ),
  164. '#disabled' => !empty($format->format),
  165. );
  166. // Add user role access selection.
  167. $form['roles'] = array(
  168. '#type' => 'checkboxes',
  169. '#title' => t('Roles'),
  170. '#options' => array_map('check_plain', user_roles()),
  171. '#disabled' => $is_fallback,
  172. );
  173. if ($is_fallback) {
  174. $form['roles']['#description'] = t('All roles for this text format must be enabled and cannot be changed.');
  175. }
  176. if (!empty($format->format)) {
  177. // If editing an existing text format, pre-select its current permissions.
  178. $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format));
  179. }
  180. elseif ($admin_role = variable_get('user_admin_role', 0)) {
  181. // If adding a new text format and the site has an administrative role,
  182. // pre-select that role so as to grant administrators access to the new
  183. // text format permission by default.
  184. $form['roles']['#default_value'] = array($admin_role);
  185. }
  186. // Retrieve available filters and load all configured filters for existing
  187. // text formats.
  188. $filter_info = filter_get_filters();
  189. $filters = !empty($format->format) ? filter_list_format($format->format) : array();
  190. // Prepare filters for form sections.
  191. foreach ($filter_info as $name => $filter) {
  192. // Create an empty filter object for new/unconfigured filters.
  193. if (!isset($filters[$name])) {
  194. $filters[$name] = new stdClass();
  195. $filters[$name]->format = $format->format;
  196. $filters[$name]->module = $filter['module'];
  197. $filters[$name]->name = $name;
  198. $filters[$name]->status = 0;
  199. $filters[$name]->weight = $filter['weight'];
  200. $filters[$name]->settings = array();
  201. }
  202. }
  203. $form['#filters'] = $filters;
  204. // Filter status.
  205. $form['filters']['status'] = array(
  206. '#type' => 'item',
  207. '#title' => t('Enabled filters'),
  208. '#prefix' => '<div id="filters-status-wrapper">',
  209. '#suffix' => '</div>',
  210. );
  211. foreach ($filter_info as $name => $filter) {
  212. $form['filters']['status'][$name] = array(
  213. '#type' => 'checkbox',
  214. '#title' => $filter['title'],
  215. '#default_value' => $filters[$name]->status,
  216. '#parents' => array('filters', $name, 'status'),
  217. '#description' => $filter['description'],
  218. '#weight' => $filter['weight'],
  219. );
  220. }
  221. // Filter order (tabledrag).
  222. $form['filters']['order'] = array(
  223. '#type' => 'item',
  224. '#title' => t('Filter processing order'),
  225. '#theme' => 'filter_admin_format_filter_order',
  226. );
  227. foreach ($filter_info as $name => $filter) {
  228. $form['filters']['order'][$name]['filter'] = array(
  229. '#markup' => $filter['title'],
  230. );
  231. $form['filters']['order'][$name]['weight'] = array(
  232. '#type' => 'weight',
  233. '#title' => t('Weight for @title', array('@title' => $filter['title'])),
  234. '#title_display' => 'invisible',
  235. '#delta' => 50,
  236. '#default_value' => $filters[$name]->weight,
  237. '#parents' => array('filters', $name, 'weight'),
  238. );
  239. $form['filters']['order'][$name]['#weight'] = $filters[$name]->weight;
  240. }
  241. // Filter settings.
  242. $form['filter_settings_title'] = array(
  243. '#type' => 'item',
  244. '#title' => t('Filter settings'),
  245. );
  246. $form['filter_settings'] = array(
  247. '#type' => 'vertical_tabs',
  248. );
  249. foreach ($filter_info as $name => $filter) {
  250. if (isset($filter['settings callback']) && function_exists($filter['settings callback'])) {
  251. $function = $filter['settings callback'];
  252. // Pass along stored filter settings and default settings, but also the
  253. // format object and all filters to allow for complex implementations.
  254. $defaults = (isset($filter['default settings']) ? $filter['default settings'] : array());
  255. $settings_form = $function($form, $form_state, $filters[$name], $format, $defaults, $filters);
  256. if (!empty($settings_form)) {
  257. $form['filters']['settings'][$name] = array(
  258. '#type' => 'fieldset',
  259. '#title' => $filter['title'],
  260. '#parents' => array('filters', $name, 'settings'),
  261. '#weight' => $filter['weight'],
  262. '#group' => 'filter_settings',
  263. );
  264. $form['filters']['settings'][$name] += $settings_form;
  265. }
  266. }
  267. }
  268. $form['actions'] = array('#type' => 'actions');
  269. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  270. return $form;
  271. }
  272. /**
  273. * Returns HTML for a text format's filter order form.
  274. *
  275. * @param $variables
  276. * An associative array containing:
  277. * - element: A render element representing the form.
  278. *
  279. * @ingroup themeable
  280. */
  281. function theme_filter_admin_format_filter_order($variables) {
  282. $element = $variables['element'];
  283. // Filter order (tabledrag).
  284. $rows = array();
  285. foreach (element_children($element, TRUE) as $name) {
  286. $element[$name]['weight']['#attributes']['class'][] = 'filter-order-weight';
  287. $rows[] = array(
  288. 'data' => array(
  289. drupal_render($element[$name]['filter']),
  290. drupal_render($element[$name]['weight']),
  291. ),
  292. 'class' => array('draggable'),
  293. );
  294. }
  295. $output = drupal_render_children($element);
  296. $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'filter-order')));
  297. drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, TRUE);
  298. return $output;
  299. }
  300. /**
  301. * Form validation handler for filter_admin_format_form().
  302. *
  303. * @see filter_admin_format_form_submit()
  304. */
  305. function filter_admin_format_form_validate($form, &$form_state) {
  306. $format_format = trim($form_state['values']['format']);
  307. $format_name = trim($form_state['values']['name']);
  308. // Ensure that the values to be saved later are exactly the ones validated.
  309. form_set_value($form['format'], $format_format, $form_state);
  310. form_set_value($form['name'], $format_name, $form_state);
  311. $result = db_query("SELECT format FROM {filter_format} WHERE name = :name AND format <> :format", array(':name' => $format_name, ':format' => $format_format))->fetchField();
  312. if ($result) {
  313. form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
  314. }
  315. }
  316. /**
  317. * Form submission handler for filter_admin_format_form().
  318. *
  319. * @see filter_admin_format_form_validate()
  320. */
  321. function filter_admin_format_form_submit($form, &$form_state) {
  322. // Remove unnecessary values.
  323. form_state_values_clean($form_state);
  324. // Add the submitted form values to the text format, and save it.
  325. $format = $form['#format'];
  326. foreach ($form_state['values'] as $key => $value) {
  327. $format->$key = $value;
  328. }
  329. $status = filter_format_save($format);
  330. // Save user permissions.
  331. if ($permission = filter_permission_name($format)) {
  332. foreach ($format->roles as $rid => $enabled) {
  333. user_role_change_permissions($rid, array($permission => $enabled));
  334. }
  335. }
  336. switch ($status) {
  337. case SAVED_NEW:
  338. drupal_set_message(t('Added text format %format.', array('%format' => $format->name)));
  339. break;
  340. case SAVED_UPDATED:
  341. drupal_set_message(t('The text format %format has been updated.', array('%format' => $format->name)));
  342. break;
  343. }
  344. }
  345. /**
  346. * Form constructor for the text format deletion confirmation form.
  347. *
  348. * @param $format
  349. * An object representing a text format.
  350. *
  351. * @see filter_menu()
  352. * @see filter_admin_disable_submit()
  353. * @ingroup forms
  354. */
  355. function filter_admin_disable($form, &$form_state, $format) {
  356. $form['#format'] = $format;
  357. return confirm_form($form,
  358. t('Are you sure you want to disable the text format %format?', array('%format' => $format->name)),
  359. 'admin/config/content/formats',
  360. t('Disabled text formats are completely removed from the administrative interface, and any content stored with that format will not be displayed. This action cannot be undone.'),
  361. t('Disable')
  362. );
  363. }
  364. /**
  365. * Form submission handler for filter_admin_disable().
  366. */
  367. function filter_admin_disable_submit($form, &$form_state) {
  368. $format = $form['#format'];
  369. filter_format_disable($format);
  370. drupal_set_message(t('Disabled text format %format.', array('%format' => $format->name)));
  371. $form_state['redirect'] = 'admin/config/content/formats';
  372. }