You are here

protected function TableDragTestForm::buildTestTable in Drupal 9

Builds the draggable test table.

Parameters

array $rows: (optional) Rows that should be shown on the table. Default value is an empty array.

string $table_id: (optional) An HTML ID for the table, defaults to 'tabledrag-test-table'.

string $group_prefix: (optional) A prefix for HTML classes generated in the method, defaults to 'tabledrag-test'.

bool $indentation: (optional) A boolean indicating whether the rows can be indented, defaults to TRUE.

Return value

array The renderable array of the draggable table used for testing.

2 calls to TableDragTestForm::buildTestTable()
NestedTableDragTestForm::buildForm in core/modules/system/tests/modules/tabledrag_test/src/Form/NestedTableDragTestForm.php
Form constructor.
TableDragTestForm::buildForm in core/modules/system/tests/modules/tabledrag_test/src/Form/TableDragTestForm.php
Form constructor.

File

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

Class

TableDragTestForm
Provides a form for draggable table testing.

Namespace

Drupal\tabledrag_test\Form

Code

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',
      ],
    ],
  ];

  // Provide a default set of five rows.
  $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;
}