tabledrag_example_parent_form.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * @file
  4. * Example demonstrating a parent/child tabledrag form
  5. */
  6. /**
  7. * Build the parent-child example form.
  8. *
  9. * Tabledrag will take care of updating the parent_id relationship on each
  10. * row of our table when we drag items around, but we need to build out the
  11. * initial tree structure ourselves. This means ordering our items such
  12. * that children items come directly after their parent items, and all items
  13. * are sorted by weight relative to their siblings.
  14. *
  15. * To keep this from cluttering the actual tabledrag code, we have moved
  16. * this to a dedicated function.
  17. *
  18. * @return array
  19. * A form array set for theming by theme_tabledrag_example_parent_form()
  20. *
  21. * @ingroup tabledrag_example
  22. */
  23. function tabledrag_example_parent_form($form_state) {
  24. // Identify that the elements in 'example_items' are a collection, to
  25. // prevent Form API from flattening the array when submitted.
  26. $form['example_items']['#tree'] = TRUE;
  27. // Fetch the example data from the database, ordered by parent/child/weight.
  28. $result = tabledrag_example_parent_get_data();
  29. // Iterate through each database result.
  30. foreach ($result as $item) {
  31. // Create a form entry for this item.
  32. //
  33. // Each entry will be an array using the the unique id for that item as
  34. // the array key, and an array of table row data as the value.
  35. $form['example_items'][$item->id] = array(
  36. // We'll use a form element of type '#markup' to display the item name.
  37. 'name' => array(
  38. '#markup' => $item->name,
  39. ),
  40. // We'll use a form element of type '#textfield' to display the item
  41. // description, to demonstrate that form elements can be included in the
  42. // table. We limit the input to 255 characters, which is the limit we
  43. // set on the database field.
  44. 'description' => array(
  45. '#type' => 'textfield',
  46. '#default_value' => $item->description,
  47. '#size' => 20,
  48. '#maxlength' => 255,
  49. ),
  50. // For parent/child relationships, we also need to add form items to
  51. // store the current item's unique id and parent item's unique id.
  52. //
  53. // We would normally use a hidden element for this, but for this example
  54. // we'll use a disabled textfield element called 'id' so that we can
  55. // display the current item's id in the table.
  56. //
  57. // Because tabledrag modifies the #value of this element, we use
  58. // '#default_value' instead of '#value' when defining a hidden element.
  59. // Also, because tabledrag modifies '#value', we cannot use a markup
  60. // element, which does not support the '#value' property. (Markup
  61. // elements use the '#markup' property instead.)
  62. 'id' => array(
  63. // '#type' => 'hidden',
  64. // '#default_value' => $item->id,
  65. '#type' => 'textfield',
  66. '#size' => 3,
  67. '#default_value' => $item->id,
  68. '#disabled' => TRUE,
  69. ),
  70. // The same information holds true for the parent id field as for the
  71. // item id field, described above.
  72. 'pid' => array(
  73. // '#type' => 'hidden',
  74. // '#default_value' => $item->pid,
  75. '#type' => 'textfield',
  76. '#size' => 3,
  77. '#default_value' => $item->pid,
  78. ),
  79. // The 'weight' field will be manipulated as we move the items around in
  80. // the table using the tabledrag activity. We use the 'weight' element
  81. // defined in Drupal's Form API.
  82. 'weight' => array(
  83. '#type' => 'weight',
  84. '#title' => t('Weight'),
  85. '#default_value' => $item->weight,
  86. '#delta' => 10,
  87. '#title_display' => 'invisible',
  88. ),
  89. // We'll use a hidden form element to pass the current 'depth' of each
  90. // item within our parent/child tree structure to the theme function.
  91. // This will be used to calculate the initial amount of indentation to
  92. // add before displaying any child item rows.
  93. 'depth' => array(
  94. '#type' => 'hidden',
  95. '#value' => $item->depth,
  96. ),
  97. );
  98. }
  99. // Now we add our submit button, for submitting the form results.
  100. //
  101. // The 'actions' wrapper used here isn't strictly necessary for tabledrag,
  102. // but is included as a Form API recommended practice.
  103. $form['actions'] = array('#type' => 'actions');
  104. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save Changes'));
  105. return $form;
  106. }
  107. /**
  108. * Theme callback for the tabledrag_example_parent_form form.
  109. *
  110. * The theme callback will format the $form data structure into a table and
  111. * add our tabledrag functionality. (Note that drupal_add_tabledrag should be
  112. * called from the theme layer, and not from a form declaration. This helps
  113. * keep template files clean and readable, and prevents tabledrag.js from
  114. * being added twice accidently.
  115. *
  116. * @ingroup tabledrag_example
  117. */
  118. function theme_tabledrag_example_parent_form($variables) {
  119. $form = $variables['form'];
  120. // Initialize the variable which will store our table rows.
  121. $rows = array();
  122. // Iterate over each element in our $form['example_items'] array.
  123. foreach (element_children($form['example_items']) as $id) {
  124. // Before we add our 'weight' column to the row, we need to give the
  125. // element a custom class so that it can be identified in the
  126. // drupal_add_tabledrag call.
  127. //
  128. // This could also have been done during the form declaration by adding
  129. // @code
  130. // '#attributes' => array('class' => 'example-item-weight'),
  131. // @endcode
  132. // directly to the 'weight' element in tabledrag_example_simple_form().
  133. $form['example_items'][$id]['weight']['#attributes']['class'] = array('example-item-weight');
  134. // In the parent/child example, we must also set this same custom class on
  135. // our id and parent_id columns (which could also have been done within
  136. // the form declaration, as above).
  137. $form['example_items'][$id]['id']['#attributes']['class'] = array('example-item-id');
  138. $form['example_items'][$id]['pid']['#attributes']['class'] = array('example-item-pid');
  139. // To support the tabledrag behaviour, we need to assign each row of the
  140. // table a class attribute of 'draggable'. This will add the 'draggable'
  141. // class to the <tr> element for that row when the final table is
  142. // rendered.
  143. $class = array('draggable');
  144. // We can add the 'tabledrag-root' class to a row in order to indicate
  145. // that the row may not be nested under a parent row. In our sample data
  146. // for this example, the description for the item with id '8' flags it as
  147. // a 'root' item which should not be nested.
  148. if ($id == '8') {
  149. $class[] = 'tabledrag-root';
  150. }
  151. // We can add the 'tabledrag-leaf' class to a row in order to indicate
  152. // that the row may not contain child rows. In our sample data for this
  153. // example, the description for the item with id '9' flags it as a 'leaf'
  154. // item which can not contain child items.
  155. if ($id == '9') {
  156. $class[] = 'tabledrag-leaf';
  157. }
  158. // If this is a child element, we need to add some indentation to the row,
  159. // so that it appears nested under its parent. Our $depth parameter was
  160. // calculated while building the tree in tabledrag_example_parent_get_data
  161. $indent = theme('indentation', array('size' => $form['example_items'][$id]['depth']['#value']));
  162. unset($form['example_items'][$id]['depth']);
  163. // We are now ready to add each element of our $form data to the $rows
  164. // array, so that they end up as individual table cells when rendered
  165. // in the final table. We run each element through the drupal_render()
  166. // function to generate the final html markup for that element.
  167. $rows[] = array(
  168. 'data' => array(
  169. // Add our 'name' column, being sure to include our indentation.
  170. $indent . drupal_render($form['example_items'][$id]['name']),
  171. // Add our 'description' column.
  172. drupal_render($form['example_items'][$id]['description']),
  173. // Add our 'weight' column.
  174. drupal_render($form['example_items'][$id]['weight']),
  175. // Add our hidden 'id' column.
  176. drupal_render($form['example_items'][$id]['id']),
  177. // Add our hidden 'parent id' column.
  178. drupal_render($form['example_items'][$id]['pid']),
  179. ),
  180. // To support the tabledrag behaviour, we need to assign each row of the
  181. // table a class attribute of 'draggable'. This will add the 'draggable'
  182. // class to the <tr> element for that row when the final table is
  183. // rendered.
  184. 'class' => $class,
  185. );
  186. }
  187. // We now define the table header values. Ensure that the 'header' count
  188. // matches the final column count for your table.
  189. //
  190. // Normally, we would hide the headers on our hidden columns, but we are
  191. // leaving them visible in this example.
  192. // $header = array(t('Name'), t('Description'), '', '', '');
  193. $header = array(t('Name'), t('Description'), t('Weight'), t('ID'), t('PID'));
  194. // We also need to pass the drupal_add_tabledrag() function an id which will
  195. // be used to identify the <table> element containing our tabledrag form.
  196. // Because an element's 'id' should be unique on a page, make sure the value
  197. // you select is NOT the same as the form ID used in your form declaration.
  198. $table_id = 'example-items-table';
  199. // We can render our tabledrag table for output.
  200. $output = theme('table', array(
  201. 'header' => $header,
  202. 'rows' => $rows,
  203. 'attributes' => array('id' => $table_id),
  204. ));
  205. // And then render any remaining form elements (such as our submit button).
  206. $output .= drupal_render_children($form);
  207. // We now call the drupal_add_tabledrag() function in order to add the
  208. // tabledrag.js goodness onto our page.
  209. //
  210. // For our parent/child tree table, we need to pass it:
  211. // - the $table_id of our <table> element (example-items-table),
  212. // - the $action to be performed on our form items ('match'),
  213. // - a string describing where $action should be applied ('parent'),
  214. // - the $group value (pid column) class name ('example-item-pid'),
  215. // - the $subgroup value (pid column) class name ('example-item-pid'),
  216. // - the $source value (id column) class name ('example-item-id'),
  217. // - an optional $hidden flag identifying if the columns should be hidden,
  218. // - an optional $limit parameter to control the max parenting depth.
  219. drupal_add_tabledrag($table_id, 'match', 'parent', 'example-item-pid', 'example-item-pid', 'example-item-id', FALSE);
  220. // Because we also want to sort in addition to providing parenting, we call
  221. // the drupal_add_tabledrag function again, instructing it to update the
  222. // weight field as items at the same level are re-ordered.
  223. drupal_add_tabledrag($table_id, 'order', 'sibling', 'example-item-weight', NULL, NULL, FALSE);
  224. return $output;
  225. }
  226. /**
  227. * Submit callback for the tabledrag_example_parent_form form.
  228. *
  229. * Updates the 'weight' column for each element in our table, taking into
  230. * account that item's new order after the drag and drop actions have been
  231. * performed.
  232. *
  233. * @ingroup tabledrag_example
  234. */
  235. function tabledrag_example_parent_form_submit($form, &$form_state) {
  236. // Because the form elements were keyed with the item ids from the database,
  237. // we can simply iterate through the submitted values.
  238. foreach ($form_state['values']['example_items'] as $id => $item) {
  239. db_query(
  240. "UPDATE {tabledrag_example} SET weight = :weight, pid = :pid WHERE id = :id",
  241. array(':weight' => $item['weight'], ':pid' => $item['pid'], ':id' => $id)
  242. );
  243. }
  244. }
  245. /**
  246. * Retrives the tree structure from database, and sorts by parent/child/weight.
  247. *
  248. * The sorting should result in children items immediately following their
  249. * parent items, with items at the same level of the hierarchy sorted by
  250. * weight.
  251. *
  252. * The approach used here may be considered too database-intensive.
  253. * Optimization of the approach is left as an exercise for the reader. :)
  254. *
  255. * @ingroup tabledrag_example
  256. */
  257. function tabledrag_example_parent_get_data() {
  258. // Get all 'root node' items (items with no parents), sorted by weight.
  259. $rootnodes = db_query('SELECT id, name, description, weight, pid
  260. FROM {tabledrag_example}
  261. WHERE (pid = 0)
  262. ORDER BY weight ASC');
  263. // Initialize a variable to store our ordered tree structure.
  264. $itemtree = array();
  265. // Depth will be incremented in our _get_tree() function for the first
  266. // parent item, so we start it at -1.
  267. $depth = -1;
  268. // Loop through the root nodes, and add their trees to the array.
  269. foreach ($rootnodes as $parent) {
  270. tabledrag_example_get_tree($parent, $itemtree, $depth);
  271. }
  272. return $itemtree;
  273. }
  274. /**
  275. * Recursively adds to the $itemtree array, ordered by parent/child/weight.
  276. *
  277. * @ingroup tabledrag_example
  278. */
  279. function tabledrag_example_get_tree($parentitem, &$itemtree = array(), &$depth = 0) {
  280. // Increase our $depth value by one.
  281. $depth++;
  282. // Set the current tree 'depth' for this item, used to calculate indentation.
  283. $parentitem->depth = $depth;
  284. // Add the parent item to the tree.
  285. $itemtree[$parentitem->id] = $parentitem;
  286. // Retrieve each of the children belonging to this parent.
  287. $children = db_query('SELECT id, name, description, weight, pid
  288. FROM {tabledrag_example}
  289. WHERE (pid = :pid)
  290. ORDER BY weight ASC',
  291. array(':pid' => $parentitem->id));
  292. foreach ($children as $child) {
  293. // Make sure this child does not already exist in the tree, to avoid loops.
  294. if (!in_array($child->id, array_keys($itemtree))) {
  295. // Add this child's tree to the $itemtree array.
  296. tabledrag_example_get_tree($child, $itemtree, $depth);
  297. }
  298. }
  299. // Finished processing this tree branch. Decrease our $depth value by one
  300. // to represent moving to the next branch.
  301. $depth--;
  302. }