book.admin.inc

  1. drupal
    1. 6 modules/book/book.admin.inc
    2. 7 modules/book/book.admin.inc
    3. 8 core/modules/book/book.admin.inc

Admin page callbacks for the book module.

Functions & methods

NameDescription
book_admin_editForm constructor for administering a single book's hierarchy.
book_admin_edit_submitForm submission handler for book_admin_edit().
book_admin_edit_validateForm validation handler for book_admin_edit().
book_admin_overviewPage callback: Returns an administrative overview of all books.
book_admin_settingsForm constructor for the book settings form.
book_admin_settings_validateForm validation handler for book_admin_settings().
theme_book_admin_tableReturns HTML for a book administration form.
_book_admin_tableBuilds the table portion of the form for the book administration page.
_book_admin_table_treeHelps build the main table in the book administration page form.

File

core/modules/book/book.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the book module.
  5. */
  6. use DrupalnodeNode;
  7. /**
  8. * Page callback: Returns an administrative overview of all books.
  9. *
  10. * @see book_menu()
  11. */
  12. function book_admin_overview() {
  13. $rows = array();
  14. $headers = array(t('Book'), t('Operations'));
  15. // Add any recognized books to the table list.
  16. foreach (book_get_books() as $book) {
  17. $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), 'admin/content/book/' . $book['nid']));
  18. }
  19. return theme('table', array('header' => $headers, 'rows' => $rows, 'empty' => t('No books available.')));
  20. }
  21. /**
  22. * Form constructor for the book settings form.
  23. *
  24. * @see book_menu()
  25. * @see book_admin_settings_validate()
  26. * @ingroup forms
  27. */
  28. function book_admin_settings() {
  29. $types = node_type_get_names();
  30. $form['book_allowed_types'] = array(
  31. '#type' => 'checkboxes',
  32. '#title' => t('Content types allowed in book outlines'),
  33. '#default_value' => variable_get('book_allowed_types', array('book')),
  34. '#options' => $types,
  35. '#description' => t('Users with the %outline-perm permission can add all content types.', array('%outline-perm' => t('Administer book outlines'))),
  36. '#required' => TRUE,
  37. );
  38. $form['book_child_type'] = array(
  39. '#type' => 'radios',
  40. '#title' => t('Content type for child pages'),
  41. '#default_value' => variable_get('book_child_type', 'book'),
  42. '#options' => $types,
  43. '#required' => TRUE,
  44. );
  45. $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
  46. $form['#validate'][] = 'book_admin_settings_validate';
  47. return system_settings_form($form);
  48. }
  49. /**
  50. * Form validation handler for book_admin_settings().
  51. */
  52. function book_admin_settings_validate($form, &$form_state) {
  53. $child_type = $form_state['values']['book_child_type'];
  54. if (empty($form_state['values']['book_allowed_types'][$child_type])) {
  55. form_set_error('book_child_type', t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', array('%add-child' => t('Add child page'))));
  56. }
  57. }
  58. /**
  59. * Form constructor for administering a single book's hierarchy.
  60. *
  61. * @param Drupal\node\Node $node
  62. * The node of the top-level page in the book.
  63. *
  64. * @see book_menu()
  65. * @see book_admin_edit_validate()
  66. * @see book_admin_edit_submit()
  67. * @ingroup forms
  68. */
  69. function book_admin_edit($form, $form_state, Node $node) {
  70. drupal_set_title($node->title);
  71. $form['#node'] = $node;
  72. _book_admin_table($node, $form);
  73. $form['save'] = array(
  74. '#type' => 'submit',
  75. '#value' => t('Save book pages'),
  76. );
  77. return $form;
  78. }
  79. /**
  80. * Form validation handler for book_admin_edit().
  81. *
  82. * Checks that the book has not been changed while using the form.
  83. *
  84. * @see book_admin_edit_submit()
  85. */
  86. function book_admin_edit_validate($form, &$form_state) {
  87. if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
  88. form_set_error('', t('This book has been modified by another user, the changes could not be saved.'));
  89. }
  90. }
  91. /**
  92. * Form submission handler for book_admin_edit().
  93. *
  94. * This function takes care to save parent menu items before their children.
  95. * Saving menu items in the incorrect order can break the menu tree.
  96. *
  97. * @see book_admin_edit_validate()
  98. * @see menu_overview_form_submit()
  99. */
  100. function book_admin_edit_submit($form, &$form_state) {
  101. // Save elements in the same order as defined in post rather than the form.
  102. // This ensures parents are updated before their children, preventing orphans.
  103. $order = array_flip(array_keys($form_state['input']['table']));
  104. $form['table'] = array_merge($order, $form['table']);
  105. foreach (element_children($form['table']) as $key) {
  106. if ($form['table'][$key]['#item']) {
  107. $row = $form['table'][$key];
  108. $values = $form_state['values']['table'][$key];
  109. // Update menu item if moved.
  110. if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
  111. $row['#item']['plid'] = $values['plid'];
  112. $row['#item']['weight'] = $values['weight'];
  113. menu_link_save($row['#item']);
  114. }
  115. // Update the title if changed.
  116. if ($row['title']['#default_value'] != $values['title']) {
  117. $node = node_load($values['nid']);
  118. $langcode = LANGUAGE_NOT_SPECIFIED;
  119. $node->title = $values['title'];
  120. $node->book['link_title'] = $values['title'];
  121. $node->revision = 1;
  122. $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
  123. $node->save();
  124. watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
  125. }
  126. }
  127. }
  128. drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title)));
  129. }
  130. /**
  131. * Builds the table portion of the form for the book administration page.
  132. *
  133. * @param Drupal\node\Node $node
  134. * The node of the top-level page in the book.
  135. * @param $form
  136. * The form that is being modified.
  137. *
  138. * @see book_admin_edit()
  139. */
  140. function _book_admin_table(Node $node, &$form) {
  141. $form['table'] = array(
  142. '#theme' => 'book_admin_table',
  143. '#tree' => TRUE,
  144. );
  145. $tree = book_menu_subtree_data($node->book);
  146. $tree = array_shift($tree); // Do not include the book item itself.
  147. if ($tree['below']) {
  148. $hash = drupal_hash_base64(serialize($tree['below']));
  149. // Store the hash value as a hidden form element so that we can detect
  150. // if another user changed the book hierarchy.
  151. $form['tree_hash'] = array(
  152. '#type' => 'hidden',
  153. '#default_value' => $hash,
  154. );
  155. $form['tree_current_hash'] = array(
  156. '#type' => 'value',
  157. '#value' => $hash,
  158. );
  159. _book_admin_table_tree($tree['below'], $form['table']);
  160. }
  161. }
  162. /**
  163. * Helps build the main table in the book administration page form.
  164. *
  165. * @param $tree
  166. * A subtree of the book menu hierarchy.
  167. * @param $form
  168. * The form that is being modified.
  169. *
  170. * @return
  171. * The form that is being modified.
  172. *
  173. * @see book_admin_edit()
  174. */
  175. function _book_admin_table_tree($tree, &$form) {
  176. // The delta must be big enough to give each node a distinct value.
  177. $count = count($tree);
  178. $delta = ($count < 30) ? 15 : intval($count / 2) + 1;
  179. foreach ($tree as $data) {
  180. $form['book-admin-' . $data['link']['nid']] = array(
  181. '#item' => $data['link'],
  182. 'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
  183. 'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
  184. 'href' => array('#type' => 'value', '#value' => $data['link']['href']),
  185. 'title' => array(
  186. '#type' => 'textfield',
  187. '#default_value' => $data['link']['link_title'],
  188. '#maxlength' => 255,
  189. '#size' => 40,
  190. ),
  191. 'weight' => array(
  192. '#type' => 'weight',
  193. '#default_value' => $data['link']['weight'],
  194. '#delta' => max($delta, abs($data['link']['weight'])),
  195. '#title' => t('Weight for @title', array('@title' => $data['link']['title'])),
  196. '#title_display' => 'invisible',
  197. ),
  198. 'plid' => array(
  199. '#type' => 'hidden',
  200. '#default_value' => $data['link']['plid'],
  201. ),
  202. 'mlid' => array(
  203. '#type' => 'hidden',
  204. '#default_value' => $data['link']['mlid'],
  205. ),
  206. );
  207. if ($data['below']) {
  208. _book_admin_table_tree($data['below'], $form);
  209. }
  210. }
  211. return $form;
  212. }
  213. /**
  214. * Returns HTML for a book administration form.
  215. *
  216. * @param $variables
  217. * An associative array containing:
  218. * - form: A render element representing the form.
  219. *
  220. * @see book_admin_table()
  221. * @ingroup themeable
  222. */
  223. function theme_book_admin_table($variables) {
  224. $form = $variables['form'];
  225. drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
  226. drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');
  227. $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3'));
  228. $rows = array();
  229. $destination = drupal_get_destination();
  230. $access = user_access('administer nodes');
  231. foreach (element_children($form) as $key) {
  232. $nid = $form[$key]['nid']['#value'];
  233. $href = $form[$key]['href']['#value'];
  234. // Add special classes to be used with tabledrag.js.
  235. $form[$key]['plid']['#attributes']['class'] = array('book-plid');
  236. $form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
  237. $form[$key]['weight']['#attributes']['class'] = array('book-weight');
  238. $data = array(
  239. theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']),
  240. drupal_render($form[$key]['weight']),
  241. drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
  242. l(t('view'), $href),
  243. $access ? l(t('edit'), 'node/' . $nid . '/edit', array('query' => $destination)) : '&nbsp;',
  244. $access ? l(t('delete'), 'node/' . $nid . '/delete', array('query' => $destination) ) : '&nbsp;',
  245. );
  246. $row = array('data' => $data);
  247. if (isset($form[$key]['#attributes'])) {
  248. $row = array_merge($row, $form[$key]['#attributes']);
  249. }
  250. $row['class'][] = 'draggable';
  251. $rows[] = $row;
  252. }
  253. return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'book-outline')));
  254. }
Login or register to post comments