You are here

public function TableDragTestForm::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/tabledrag_test/src/Form/TableDragTestForm.php \Drupal\tabledrag_test\Form\TableDragTestForm::buildForm()
  2. 10 core/modules/system/tests/modules/tabledrag_test/src/Form/TableDragTestForm.php \Drupal\tabledrag_test\Form\TableDragTestForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/system/tests/modules/tabledrag_test/src/Form/TableDragTestForm.php, line 49

Class

TableDragTestForm
Provides a form for draggable table testing.

Namespace

Drupal\tabledrag_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['table'] = [
    '#type' => 'table',
    '#header' => [
      [
        'data' => $this
          ->t('Text'),
        'colspan' => 4,
      ],
      $this
        ->t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'tabledrag-test-weight',
      ],
      [
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'tabledrag-test-parent',
        'subgroup' => 'tabledrag-test-parent',
        'source' => 'tabledrag-test-id',
        'hidden' => TRUE,
        'limit' => 2,
      ],
      [
        'action' => 'depth',
        'relationship' => 'group',
        'group' => 'tabledrag-test-depth',
        'hidden' => TRUE,
      ],
    ],
    '#attributes' => [
      'id' => 'tabledrag-test-table',
    ],
    '#attached' => [
      'library' => [
        'tabledrag_test/tabledrag',
      ],
    ],
  ];

  // Provide a default set of five 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';
    }
    $form['table'][$id] = [
      'title' => [
        'indentation' => [
          '#theme' => 'indentation',
          '#size' => $row['depth'],
        ],
        '#plain_text' => "Row with id {$id}",
      ],
      'id' => [
        '#type' => 'hidden',
        '#value' => $id,
        '#attributes' => [
          'class' => [
            'tabledrag-test-id',
          ],
        ],
      ],
      'parent' => [
        '#type' => 'hidden',
        '#default_value' => $row['parent'],
        '#parents' => [
          'table',
          $id,
          'parent',
        ],
        '#attributes' => [
          'class' => [
            'tabledrag-test-parent',
          ],
        ],
      ],
      'depth' => [
        '#type' => 'hidden',
        '#default_value' => $row['depth'],
        '#attributes' => [
          'class' => [
            'tabledrag-test-depth',
          ],
        ],
      ],
      'weight' => [
        '#type' => 'weight',
        '#default_value' => $row['weight'],
        '#attributes' => [
          'class' => [
            'tabledrag-test-weight',
          ],
        ],
      ],
      '#attributes' => [
        'class' => $row['classes'],
      ],
    ];
  }
  $form['save'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
  ];
  return $form;
}