tabledrag_example_simple_form.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @file
  4. * Example demonstrating a simple (i.e. 'sort' only) tabledrag form
  5. */
  6. /**
  7. * Build the tabledrag_simple_example_form form.
  8. *
  9. * @return array
  10. * A form array set for theming by theme_tabledrag_example_simple_form()
  11. *
  12. * @ingroup tabledrag_example
  13. */
  14. function tabledrag_example_simple_form($form_state) {
  15. // Identify that the elements in 'example_items' are a collection, to
  16. // prevent Form API from flattening the array when submitted.
  17. $form['example_items']['#tree'] = TRUE;
  18. // Fetch the example data from the database, ordered by weight ascending.
  19. //
  20. // This query excludes the last two tabledrag_example database rows, as
  21. // they are intended only for the 'parent/child' tabledrag examples.
  22. $result = db_query('SELECT id, name, description, weight FROM {tabledrag_example} WHERE id < 8 ORDER BY weight ASC');
  23. // Iterate through each database result.
  24. foreach ($result as $item) {
  25. // Create a form entry for this item.
  26. //
  27. // Each entry will be an array using the the unique id for that item as
  28. // the array key, and an array of table row data as the value.
  29. $form['example_items'][$item->id] = array(
  30. // We'll use a form element of type '#markup' to display the item name.
  31. 'name' => array(
  32. '#markup' => check_plain($item->name),
  33. ),
  34. // We'll use a form element of type '#textfield' to display the item
  35. // description, which will allow the value to be changed via the form.
  36. // We limit the input to 255 characters, which is the limit we set on
  37. // the database field.
  38. 'description' => array(
  39. '#type' => 'textfield',
  40. '#default_value' => check_plain($item->description),
  41. '#size' => 20,
  42. '#maxlength' => 255,
  43. ),
  44. // The 'weight' field will be manipulated as we move the items around in
  45. // the table using the tabledrag activity. We use the 'weight' element
  46. // defined in Drupal's Form API.
  47. 'weight' => array(
  48. '#type' => 'weight',
  49. '#title' => t('Weight'),
  50. '#default_value' => $item->weight,
  51. '#delta' => 10,
  52. '#title_display' => 'invisible',
  53. ),
  54. );
  55. }
  56. // Now we add our submit button, for submitting the form results.
  57. //
  58. // The 'actions' wrapper used here isn't strictly necessary for tabledrag,
  59. // but is included as a Form API recommended practice.
  60. $form['actions'] = array('#type' => 'actions');
  61. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save Changes'));
  62. return $form;
  63. }
  64. /**
  65. * Theme callback for the tabledrag_example_simple_form form.
  66. *
  67. * The theme callback will format the $form data structure into a table and
  68. * add our tabledrag functionality. (Note that drupal_add_tabledrag should be
  69. * called from the theme layer, and not from a form declaration. This helps
  70. * keep template files clean and readable, and prevents tabledrag.js from
  71. * being added twice accidently.
  72. *
  73. * @return array
  74. * The rendered tabledrag form
  75. *
  76. * @ingroup tabledrag_example
  77. */
  78. function theme_tabledrag_example_simple_form($variables) {
  79. $form = $variables['form'];
  80. // Initialize the variable which will store our table rows.
  81. $rows = array();
  82. // Iterate over each element in our $form['example_items'] array.
  83. foreach (element_children($form['example_items']) as $id) {
  84. // Before we add our 'weight' column to the row, we need to give the
  85. // element a custom class so that it can be identified in the
  86. // drupal_add_tabledrag call.
  87. //
  88. // This could also have been done during the form declaration by adding
  89. // '#attributes' => array('class' => 'example-item-weight'),
  90. // directy to the 'weight' element in tabledrag_example_simple_form().
  91. $form['example_items'][$id]['weight']['#attributes']['class'] = array('example-item-weight');
  92. // We are now ready to add each element of our $form data to the $rows
  93. // array, so that they end up as individual table cells when rendered
  94. // in the final table. We run each element through the drupal_render()
  95. // function to generate the final html markup for that element.
  96. $rows[] = array(
  97. 'data' => array(
  98. // Add our 'name' column.
  99. drupal_render($form['example_items'][$id]['name']),
  100. // Add our 'description' column.
  101. drupal_render($form['example_items'][$id]['description']),
  102. // Add our 'weight' column.
  103. drupal_render($form['example_items'][$id]['weight']),
  104. ),
  105. // To support the tabledrag behaviour, we need to assign each row of the
  106. // table a class attribute of 'draggable'. This will add the 'draggable'
  107. // class to the <tr> element for that row when the final table is
  108. // rendered.
  109. 'class' => array('draggable'),
  110. );
  111. }
  112. // We now define the table header values. Ensure that the 'header' count
  113. // matches the final column count for your table.
  114. $header = array(t('Name'), t('Description'), t('Weight'));
  115. // We also need to pass the drupal_add_tabledrag() function an id which will
  116. // be used to identify the <table> element containing our tabledrag form.
  117. // Because an element's 'id' should be unique on a page, make sure the value
  118. // you select is NOT the same as the form ID used in your form declaration.
  119. $table_id = 'example-items-table';
  120. // We can render our tabledrag table for output.
  121. $output = theme('table', array(
  122. 'header' => $header,
  123. 'rows' => $rows,
  124. 'attributes' => array('id' => $table_id),
  125. ));
  126. // And then render any remaining form elements (such as our submit button).
  127. $output .= drupal_render_children($form);
  128. // We now call the drupal_add_tabledrag() function in order to add the
  129. // tabledrag.js goodness onto our page.
  130. //
  131. // For a basic sortable table, we need to pass it:
  132. // - the $table_id of our <table> element,
  133. // - the $action to be performed on our form items ('order'),
  134. // - a string describing where $action should be applied ('siblings'),
  135. // - and the class of the element containing our 'weight' element.
  136. drupal_add_tabledrag($table_id, 'order', 'sibling', 'example-item-weight');
  137. return $output;
  138. }
  139. /**
  140. * Submit callback for the tabledrag_example_simple_form form.
  141. *
  142. * Updates the 'weight' column for each element in our table, taking into
  143. * account that item's new order after the drag and drop actions have been
  144. * performed.
  145. *
  146. * @ingroup tabledrag_example
  147. */
  148. function tabledrag_example_simple_form_submit($form, &$form_state) {
  149. // Because the form elements were keyed with the item ids from the database,
  150. // we can simply iterate through the submitted values.
  151. foreach ($form_state['values']['example_items'] as $id => $item) {
  152. db_query(
  153. "UPDATE {tabledrag_example} SET weight = :weight WHERE id = :id",
  154. array(':weight' => $item['weight'], ':id' => $id)
  155. );
  156. }
  157. }