[Drupal] Table render array example structure

Result

Rendered table from a render array
Rendered table from a render array

The code

  public function controllerCallbackExample() {
    return [
      '#type' => 'table',
      '#caption' => 'This is a caption. Use a translated string here.',
      '#header' => [
        'Header 1', 'Header 2', 'Header 3', 'Header 4'
      ],
      '#rows' => [
        [
          'Row 1 Cell 1', 'Row 1 Cell 2', 'Row 1 Cell 3', ['data' => 'Row 1 Cell 4']
        ],
        [
          'class' => ['second-row-class', 'may contain spaces but dont use them'],
          'data' => [
            'Row 2 Cell 1',
            [
              'data' => 'Row 2 Cell 2'
            ],
            [
              'colspan' => 2,
              'data' => 'Row 3 Cell 3 & 4',
              'style' => 'background-color: pink; text-align: center'
            ]
          ],
          'style' => 'font-weight: bold'
        ],
        [
          'data' => [
            'Row 3 Cell 1', 'Row 3 Cell 2', 'Row 3 Cell 3', 'Row 3 Cell 4'
          ],
          'no_striping' => FALSE // Not working in 8.2.x, may be fixed later
        ]
      ],
      '#footer' => [
        [
          'data' => [
            'Footer 1',
            [
              'colspan' => 2,
              'data' => 'Footer 2 with colspan = 2',
              'style' => 'text-align: inherit'
            ],
            [
              'data' => 'Footer 4',
              'style' => 'text-align: inherit'
            ]
          ],
          'style' => 'text-align: center'
        ],
        [
          [
            'colspan' => 4,
            'class' => 'second-footer-row',
            'data' => 'Another Footer Line',
            'style' => ['background-color: purple;', 'text-align: center;', 'color: white;']
          ]
        ]
      ]
    ];
  }

Additional Information

Published by Dakwamine

Dakwamine, alias Quang-Minh Dang, né en 1987 en région parisienne. Un type sympa, pas bavard et pragmatique.