function ClientSideFilterTable::preRenderTable

Same name in other branches
  1. 4.x src/Element/ClientSideFilterTable.php \Drupal\devel\Element\ClientSideFilterTable::preRenderTable()

Pre-render callback: Assemble render array for the filterable table.

Parameters

array $element: An associative array containing the properties of the element.

Return value

array The $element with prepared render array ready for rendering.

1 call to ClientSideFilterTable::preRenderTable()
DevelClientSideFilterTableTest::testPreRenderTable in tests/src/Unit/DevelClientSideFilterTableTest.php
@covers ::preRenderTable @dataProvider providerPreRenderTable

File

src/Element/ClientSideFilterTable.php, line 76

Class

ClientSideFilterTable
Provides a render element for filterable table data.

Namespace

Drupal\devel\Element

Code

public static function preRenderTable(array $element) : array {
    $build['#attached']['library'][] = 'devel/devel-table-filter';
    $identifier = Html::getUniqueId('js-devel-table-filter');
    $build['filters'] = [
        '#type' => 'container',
        '#weight' => -1,
        '#attributes' => [
            'class' => [
                'table-filter',
                'js-show',
            ],
        ],
    ];
    $build['filters']['name'] = [
        '#type' => 'search',
        '#size' => 30,
        '#title' => $element['#filter_label'],
        '#placeholder' => $element['#filter_placeholder'],
        '#attributes' => [
            'class' => [
                'table-filter-text',
            ],
            'data-table' => '.' . $identifier,
            'autocomplete' => 'off',
            'title' => $element['#filter_description'],
        ],
    ];
    foreach ($element['#rows'] as &$row) {
        foreach ($row as &$cell) {
            if (!isset($cell['data'])) {
                continue;
            }
            if (empty($cell['filter'])) {
                continue;
            }
            $cell['class'][] = 'table-filter-text-source';
        }
    }
    $build['table'] = [
        '#type' => 'table',
        '#header' => $element['#header'],
        '#rows' => $element['#rows'],
        '#empty' => $element['#empty'],
        '#sticky' => $element['#sticky'],
        '#responsive' => $element['#responsive'],
        '#attributes' => $element['#attributes'],
    ];
    $build['table']['#attributes']['class'][] = $identifier;
    $build['table']['#attributes']['class'][] = 'devel-table-filter';
    return $build;
}