- <?php
- * @file
- * Example demonstrating a simple (i.e. 'sort' only) tabledrag form
- */
-
- * Build the tabledrag_simple_example_form form.
- *
- * @return array
- * A form array set for theming by theme_tabledrag_example_simple_form()
- *
- * @ingroup tabledrag_example
- */
- function tabledrag_example_simple_form($form_state) {
-
-
- $form['example_items']['#tree'] = TRUE;
-
-
-
-
-
- $result = db_query('SELECT id, name, description, weight FROM {tabledrag_example} WHERE id < 8 ORDER BY weight ASC');
-
-
- foreach ($result as $item) {
-
-
-
-
-
- $form['example_items'][$item->id] = array(
-
-
- 'name' => array(
- '#markup' => check_plain($item->name),
- ),
-
-
-
-
-
- 'description' => array(
- '#type' => 'textfield',
- '#default_value' => check_plain($item->description),
- '#size' => 20,
- '#maxlength' => 255,
- ),
-
-
-
-
- 'weight' => array(
- '#type' => 'weight',
- '#title' => t('Weight'),
- '#default_value' => $item->weight,
- '#delta' => 10,
- '#title_display' => 'invisible',
- ),
- );
- }
-
-
-
-
-
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save Changes'));
- return $form;
- }
-
- * Theme callback for the tabledrag_example_simple_form form.
- *
- * The theme callback will format the $form data structure into a table and
- * add our tabledrag functionality. (Note that backdrop_add_tabledrag should be
- * called from the theme layer, and not from a form declaration. This helps
- * keep template files clean and readable, and prevents tabledrag.js from
- * being added twice accidentally.
- *
- * @return string
- * The HTML markup for the table-drag form.
- *
- * @ingroup tabledrag_example
- */
- function theme_tabledrag_example_simple_form($variables) {
- $form = $variables['form'];
-
-
- $rows = array();
-
-
- foreach (element_children($form['example_items']) as $id) {
-
-
-
-
-
-
-
-
- $form['example_items'][$id]['weight']['#attributes']['class'] = array('example-item-weight');
-
-
-
-
-
- $rows[] = array(
- 'data' => array(
-
- backdrop_render($form['example_items'][$id]['name']),
-
- backdrop_render($form['example_items'][$id]['description']),
-
- backdrop_render($form['example_items'][$id]['weight']),
- ),
-
-
-
-
- 'class' => array('draggable'),
- );
- }
-
-
-
- $header = array(t('Name'), t('Description'), t('Weight'));
-
-
-
-
-
- $table_id = 'example-items-table';
-
-
- $output = theme('table', array(
- 'header' => $header,
- 'rows' => $rows,
- 'attributes' => array('id' => $table_id),
- ));
-
-
- $output .= backdrop_render_children($form);
-
-
-
-
-
-
-
-
-
- backdrop_add_tabledrag($table_id, 'order', 'sibling', 'example-item-weight');
-
- return $output;
- }
-
- * Submit callback for the tabledrag_example_simple_form form.
- *
- * Updates the 'weight' column for each element in our table, taking into
- * account that item's new order after the drag and drop actions have been
- * performed.
- *
- * @ingroup tabledrag_example
- */
- function tabledrag_example_simple_form_submit($form, &$form_state) {
-
-
- foreach ($form_state['values']['example_items'] as $id => $item) {
- db_query(
- "UPDATE {tabledrag_example} SET weight = :weight WHERE id = :id",
- array(':weight' => $item['weight'], ':id' => $id)
- );
- }
- }