Skip to content

Instantly share code, notes, and snippets.

@lhuria94
Created November 22, 2017 10:59
Show Gist options
  • Save lhuria94/da78e800d6d7f7167b30c89b9f87d7a6 to your computer and use it in GitHub Desktop.
Save lhuria94/da78e800d6d7f7167b30c89b9f87d7a6 to your computer and use it in GitHub Desktop.
Drupal table drag example in form An example of using a Drupal tabledrag table without the need of creating a theme function for the complete form. This makes it a lot easier to build a form which contains multiple form elements/fieldsets.
<?php
/**
* @file
* A tabledrag example - without theming the whole form.
*/
/**
* Implements hook_menu().
*/
function better_tabledrag_example_menu() {
$items = array();
$items['tabledrag'] = array(
'title' => 'A tabledrag example',
'page callback' => 'drupal_get_form',
'page arguments' => array('better_tabledrag_example_form'),
'access callback' => TRUE,
);
return $items;
}
/**
* Form callback: A tabledrag example.
*/
function better_tabledrag_example_form($form, &$form_state) {
$form = array();
$rows = array();
$row_elements = array();
// Put it into a fieldset for no reason.
$form['data_table'] = array(
'#type' => 'fieldset',
'#title' => t('Data table'),
);
// Collect your data.
$data = array(
'some-id-1' => array(
'enable' => TRUE,
'default' => TRUE,
'weight' => 1,
'name' => 'some text from config',
'description' => 'some description text',
),
'some-id-2' => array(
'enable' => TRUE,
'default' => FALSE,
'weight' => 3,
'name' => 'some more text from config',
'description' => 'more description text',
),
'some-id-3' => array(
'enable' => FALSE,
'default' => TRUE,
'weight' => 2,
'name' => 'and even more text from config',
'description' => 'mooore description text',
),
);
// Sort the rows.
uasort($data, '_better_tabledrag_example_form_weight_arraysort');
// Build the rows.
foreach ($data as $id => $entry) {
// Build the table rows.
$rows[$id] = array(
'data' => array(
// Cell for the cross drag&drop element.
array('class' => array('entry-cross')),
// Weight item for the tabledrag.
array('data' => array(
'#type' => 'weight',
'#title' => t('Weight'),
'#title_display' => 'invisible',
'#default_value' => $entry['weight'],
'#parents' => array('data_table', $id, 'weight'),
'#attributes' => array(
'class' => array('entry-order-weight'),
),
)),
// Enabled checkbox.
array('data' => array(
'#type' => 'checkbox',
'#title' => t('Enable'),
'#title_display' => 'invisible',
'#default_value' => $entry['enable'],
'#parents' => array('data_table', $id, 'enabled'),
)),
// Default checkbox.
array('data' => array(
'#type' => 'checkbox',
'#title' => t('Default'),
'#title_display' => 'invisible',
'#default_value' => $entry['default'],
'#parents' => array('data_table', $id, 'default'),
)),
// Name textfield.
array('data' => array(
'#type' => 'textfield',
'#size' => 10,
'#title' => t('Name'),
'#title_display' => 'invisible',
'#default_value' => $entry['name'],
'#parents' => array('data_table', $id, 'name'),
)),
// Entry description.
check_plain($entry['description']),
// Operations.
array('data' => array(
'#theme' => 'link',
'#text' => t('Edit settings'),
'#path' => 'tabledrag/' . $id . '/edit',
'#options' => array('attributes' => array(), 'html' => FALSE),
)),
array('data' => array(
'#theme' => 'link',
'#text' => t('Delete entry'),
'#path' => 'tabledrag/' . $id . '/delete',
'#options' => array('attributes' => array(), 'html' => FALSE),
)),
),
'class' => array('draggable'),
);
// Build rows of the form elements in the table.
$row_elements[$id] = array(
'weight' => &$rows[$id]['data'][1]['data'],
'enabled' => &$rows[$id]['data'][2]['data'],
'default' => &$rows[$id]['data'][3]['data'],
'name' => &$rows[$id]['data'][4]['data'],
);
}
// Add the table to the form.
$form['data_table']['table'] = array(
'#theme' => 'table',
// The row form elements need to be processed and build,
// therefore pass them as element children.
'elements' => $row_elements,
'#header' => array(
// We need two empty columns for the weigth field and the cross.
array('data' => NULL, 'colspan' => 2),
t('Enabled'),
t('Default'),
t('Name'),
t('Description'),
array('data' => t('Operations'), 'colspan' => 2),
),
'#rows' => $rows,
'#empty' => t('There are no entries available.'),
'#attributes' => array('id' => 'entry-order'),
);
drupal_add_tabledrag('entry-order', 'order', 'sibling', 'entry-order-weight');
return $form;
}
/**
* Helper function for sorting entry weights.
*/
function _better_tabledrag_example_form_weight_arraysort($a, $b) {
if (isset($a['weight']) && isset($b['weight'])) {
return $a['weight'] < $b['weight'] ? -1 : 1;
}
return 0;
}
//A few aesthetic improvements.
$rows[$id] = array(
'data' => array(
// Name textfield.
array('data' => array(
'#type' => 'textfield',
'#size' => 10,
'#title' => t('Name'),
'#title_display' => 'invisible',
'#default_value' => $entry['name'],
'#parents' => array('data_table', $id, 'name'),
)
'class' => array('entry-cross')),
// Enabled checkbox.
array('data' => array(
'#type' => 'checkbox',
'#title' => t('Enable'),
'#title_display' => 'invisible',
'#default_value' => $entry['enable'],
'#parents' => array('data_table', $id, 'enabled'),
)),
// Default checkbox.
array('data' => array(
'#type' => 'checkbox',
'#title' => t('Default'),
'#title_display' => 'invisible',
'#default_value' => $entry['default'],
'#parents' => array('data_table', $id, 'default'),
)),
// Entry description.
check_plain($entry['description']),
// Operations.
array('data' => array(
'#theme' => 'link',
'#text' => t('Edit settings'),
'#path' => 'tabledrag/' . $id . '/edit',
'#options' => array('attributes' => array(), 'html' => FALSE),
)),
array('data' => array(
'#theme' => 'link',
'#text' => t('Delete entry'),
'#path' => 'tabledrag/' . $id . '/delete',
'#options' => array('attributes' => array(), 'html' => FALSE),
)),
// Weight item for the tabledrag.
array('data' => array(
'#type' => 'weight',
'#title' => t('Weight'),
'#title_display' => 'invisible',
'#default_value' => $entry['weight'],
'#parents' => array('data_table', $id, 'weight'),
'#attributes' => array(
'class' => array('entry-order-weight'),
),
)
'class' => array('tabledrag-hide')),
),
'class' => array('draggable'),
);
// Build rows of the form elements in the table.
$row_elements[$id] = array(
'weight' => &$rows[$id]['data'][1]['data'],
'enabled' => &$rows[$id]['data'][2]['data'],
'default' => &$rows[$id]['data'][3]['data'],
'name' => &$rows[$id]['data'][4]['data'],
);
}
// Add the table to the form.
$form['data_table']['table'] = array(
'#theme' => 'table',
// The row form elements need to be processed and build,
// therefore pass them as element children.
'elements' => $row_elements,
'#header' => array(
// We need two empty columns for the weigth field and the cross.
t('Name'),
t('Enabled'),
t('Default'),
t('Description'),
array('data' => t('Operations'), 'colspan' => 2),
array('data' => t('Weight'), 'class' => array('tabledrag-hide')),
),
'#rows' => $rows,
'#empty' => t('There are no entries available.'),
'#attributes' => array('id' => 'entry-order'),
);
What that will do is move "Name" to the first position in the table and add the "move" cross bar in front of it in the same column. It will hide the weight column by default and then when you click on the "Show row weights" link that appears above the table it will hide the cross bar and show the weight column like other draggable tables. It looks cleaner and functions naturally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment