View source
<?php
namespace Drupal\tabledrag_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TableDragTestForm extends FormBase {
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('state'));
}
public function getFormId() {
return 'tabledrag_test_form';
}
protected function buildTestTable(array $rows = [], $table_id = 'tabledrag-test-table', $group_prefix = 'tabledrag-test', $indentation = TRUE) {
$tabledrag = [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => "{$group_prefix}-weight",
],
];
if ($indentation) {
$tabledrag[] = [
'action' => 'match',
'relationship' => 'parent',
'group' => "{$group_prefix}-parent",
'subgroup' => "{$group_prefix}-parent",
'source' => "{$group_prefix}-id",
'hidden' => TRUE,
'limit' => 2,
];
$tabledrag[] = [
'action' => 'depth',
'relationship' => 'group',
'group' => "{$group_prefix}-depth",
'hidden' => TRUE,
];
}
$table = [
'#type' => 'table',
'#header' => [
[
'data' => $this
->t('Text'),
'colspan' => $indentation ? 4 : 2,
],
$this
->t('Weight'),
],
'#tabledrag' => $tabledrag,
'#attributes' => [
'id' => $table_id,
],
'#attached' => [
'library' => [
'tabledrag_test/tabledrag',
],
],
];
$rows = !empty($rows) ? $rows : $this->state
->get('tabledrag_test_table', array_flip(range(1, 5)));
foreach ($rows as $id => $row) {
if (!is_array($row)) {
$row = [];
}
$row += [
'parent' => '',
'weight' => 0,
'depth' => 0,
'classes' => [],
'draggable' => TRUE,
];
if (!empty($row['draggable'])) {
$row['classes'][] = 'draggable';
}
$table[$id] = [
'title' => [
'indentation' => [
'#theme' => 'indentation',
'#size' => $indentation ? $row['depth'] : 0,
],
'#plain_text' => "Row with id {$id}",
],
'id' => [
'#type' => 'hidden',
'#value' => $id,
'#parents' => [
'table',
$id,
'id',
],
'#attributes' => [
'class' => [
"{$group_prefix}-id",
],
],
],
'#attributes' => [
'class' => $row['classes'],
],
];
if ($indentation) {
$table[$id]['parent'] = [
'#type' => 'hidden',
'#default_value' => $row['parent'],
'#parents' => [
'table',
$id,
'parent',
],
'#attributes' => [
'class' => [
"{$group_prefix}-parent",
],
],
];
$table[$id]['depth'] = [
'#type' => 'hidden',
'#default_value' => $row['depth'],
'#parents' => [
'table',
$id,
'depth',
],
'#attributes' => [
'class' => [
"{$group_prefix}-depth",
],
],
];
}
$table[$id]['weight'] = [
'#type' => 'weight',
'#default_value' => $row['weight'],
'#parents' => [
'table',
$id,
'weight',
],
'#attributes' => [
'class' => [
"{$group_prefix}-weight",
],
],
];
}
return $table;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['table'] = $this
->buildTestTable();
$form['actions'] = $this
->buildFormActions();
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$operation = isset($form_state
->getTriggeringElement()['#op']) ? $form_state
->getTriggeringElement()['#op'] : 'save';
switch ($operation) {
case 'reset':
$this->state
->set('tabledrag_test_table', array_flip(range(1, 5)));
break;
default:
$test_table = [];
foreach ($form_state
->getValue('table') as $row) {
$test_table[$row['id']] = $row;
}
$this->state
->set('tabledrag_test_table', $test_table);
break;
}
}
protected function buildFormActions() {
return [
'#type' => 'actions',
'save' => [
'#type' => 'submit',
'#value' => $this
->t('Save'),
],
'reset' => [
'#type' => 'submit',
'#op' => 'reset',
'#value' => $this
->t('Reset'),
],
];
}
}