drupal_add_tabledrag

Versions
6 – 7
drupal_add_tabledrag($table_id, $action, $relationship, $group, $subgroup = NULL, $source = NULL, $hidden = TRUE, $limit = 0)

Assist in adding the tableDrag JavaScript behavior to a themed table.

Draggable tables should be used wherever an outline or list of sortable items needs to be arranged by an end-user. Draggable tables are very flexible and can manipulate the value of form elements placed within individual columns.

To set up a table to use drag and drop in place of weight select-lists or in place of a form that contains parent relationships, the form must be themed into a table. The table must have an id attribute set. If using theme_table(), the id may be set as such:

<?php

$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'my-module-table')));
return $output;

?>

In the theme function for the form, a special class must be added to each form element within the same column, "grouping" them together.

In a situation where a single weight column is being sorted in the table, the classes could be added like this (in the theme function):

<?php

$form['my_elements'][$delta]['weight']['#attributes']['class'] = array('my-elements-weight');

?>

Each row of the table must also have a class of "draggable" in order to enable the drag handles:

<?php

$row = array(...);
$rows[] = array(
'data' => $row,
'class' => array('draggable'),
);

?>

When tree relationships are present, the two additional classes 'tabledrag-leaf' and 'tabledrag-root' can be used to refine the behavior:

  • Rows with the 'tabledrag-leaf' class cannot have child rows.
  • Rows with the 'tabledrag-root' class cannot be nested under a parent row.

Calling drupal_add_tabledrag() would then be written as such:

<?php

drupal_add_tabledrag('my-module-table', 'order', 'sibling', 'my-elements-weight');

?>

In a more complex case where there are several groups in one column (such as the block regions on the admin/structure/block page), a separate subgroup class must also be added to differentiate the groups.

<?php

$form['my_elements'][$region][$delta]['weight']['#attributes']['class'] = array('my-elements-weight', 'my-elements-weight-' . $region);

?>

$group is still 'my-element-weight', and the additional $subgroup variable will be passed in as 'my-elements-weight-' . $region. This also means that you'll need to call drupal_add_tabledrag() once for every region added.

<?php

foreach ($regions as $region) {
drupal_add_tabledrag('my-module-table', 'order', 'sibling', 'my-elements-weight', 'my-elements-weight-' . $region);
}

?>

In a situation where tree relationships are present, adding multiple subgroups is not necessary, because the table will contain indentations that provide enough information about the sibling and parent relationships. See theme_menu_overview_form() for an example creating a table containing parent relationships.

Please note that this function should be called from the theme layer, such as in a .tpl.php file, theme_ function, or in a template_preprocess function, not in a form declaration. Though the same JavaScript could be added to the page using drupal_add_js() directly, this function helps keep template files clean and readable. It also prevents tabledrag.js from being added twice accidentally.

See also

block-admin-display-form.tpl.php

@see theme_menu_overview_form()

Parameters

$table_id String containing the target table's id attribute. If the table does not have an id, one will need to be set, such as <table id="my-module-table">.

$action String describing the action to be done on the form item. Either 'match' 'depth', or 'order'. Match is typically used for parent relationships. Order is typically used to set weights on other form elements with the same group. Depth updates the target element with the current indentation.

$relationship String describing where the $action variable should be performed. Either 'parent', 'sibling', 'group', or 'self'. Parent will only look for fields up the tree. Sibling will look for fields in the same group in rows above and below it. Self affects the dragged row itself. Group affects the dragged row, plus any children below it (the entire dragged group).

$group A class name applied on all related form elements for this action.

$subgroup (optional) If the group has several subgroups within it, this string should contain the class name identifying fields in the same subgroup.

$source (optional) If the $action is 'match', this string should contain the class name identifying what field will be used as the source value when matching the value in $subgroup.

$hidden (optional) The column containing the field elements may be entirely hidden from view dynamically when the JavaScript is loaded. Set to FALSE if the column should not be hidden.

$limit (optional) Limit the maximum amount of parenting in this table.

▾ 17 functions call drupal_add_tabledrag()

template_preprocess_field_ui_field_overview_form in modules/field_ui/field_ui.admin.inc
Theme preprocess function for field_ui-field-overview-form.tpl.php.
theme_book_admin_table in modules/book/book.admin.inc
Theme function for the book administration page form.
theme_field_multiple_value_form in modules/field/field.form.inc
Theme an individual form element.
theme_file_widget_multiple in modules/file/file.field.inc
Theme a group of file upload widgets.
theme_filter_admin_order in modules/filter/filter.admin.inc
Theme filter order configuration form.
theme_filter_admin_overview in modules/filter/filter.admin.inc
Theme the text format administration overview form.
theme_image_style_effects in modules/image/image.admin.inc
Theme callback for listing the effects within a specific image style.
theme_locale_languages_configure_form in includes/locale.inc
Theme the language configure form.
theme_locale_languages_overview_form in includes/locale.inc
Theme the language overview form.
theme_poll_choices in modules/poll/poll.module
Theme the admin poll form for choices.
theme_profile_admin_overview in modules/profile/profile.admin.inc
Theme the profile field overview into a drag and drop enabled table.
theme_scaffolding_example_overview_form in developer/examples/scaffolding_example/scaffolding_example.admin.inc
Theme the drag-and-drop overview form.
theme_shortcut_set_customize in modules/shortcut/shortcut.admin.inc
Theme function for the shortcut set customization form.
theme_taxonomy_overview_terms in modules/taxonomy/taxonomy.admin.inc
Theme the terms overview as a sortable list of terms.
theme_taxonomy_overview_vocabularies in modules/taxonomy/taxonomy.admin.inc
Theme the vocabulary overview as a sortable list of vocabularies.
theme_upload_form_current in modules/upload/upload.module
Theme the attachments list.
theme_upload_form_new in modules/upload/upload.module
Theme the attachment form. Note: required to output prefix/suffix.

Code

includes/common.inc, line 4240

<?php
function drupal_add_tabledrag($table_id, $action, $relationship, $group, $subgroup = NULL, $source = NULL, $hidden = TRUE, $limit = 0) {
  $js_added = &drupal_static(__FUNCTION__, FALSE);
  if (!$js_added) {
    // Add the table drag JavaScript to the page before the module JavaScript
    // to ensure that table drag behaviors are registered before any module
    // uses it.
    drupal_add_js('misc/tabledrag.js', array('weight' => JS_DEFAULT - 1));
    $js_added = TRUE;
  }

  // If a subgroup or source isn't set, assume it is the same as the group.
  $target = isset($subgroup) ? $subgroup : $group;
  $source = isset($source) ? $source : $target;
  $settings['tableDrag'][$table_id][$group][] = array(
    'target' => $target,
    'source' => $source,
    'relationship' => $relationship,
    'action' => $action,
    'hidden' => $hidden,
    'limit' => $limit,
  );
  drupal_add_js($settings, 'setting');
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.