Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Table.php

function Table::render

Renders table to output.

Example:

+---------------+-----------------------+------------------+ | ISBN | Title | Author | +---------------+-----------------------+------------------+ | 99921-58-10-7 | Divine Comedy | Dante Alighieri | | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | +---------------+-----------------------+------------------+

1 call to Table::render()
Table::appendRow in vendor/symfony/console/Helper/Table.php
Adds a row to the table, and re-renders the table.

File

vendor/symfony/console/Helper/Table.php, line 312

Class

Table
Provides helpers to display a table.

Namespace

Symfony\Component\Console\Helper

Code

public function render() : void {
    $divider = new TableSeparator();
    $isCellWithColspan = static fn($cell) => $cell instanceof TableCell && $cell->getColspan() >= 2;
    $horizontal = self::DISPLAY_ORIENTATION_HORIZONTAL === $this->displayOrientation;
    $vertical = self::DISPLAY_ORIENTATION_VERTICAL === $this->displayOrientation;
    $rows = [];
    if ($horizontal) {
        foreach ($this->headers[0] ?? [] as $i => $header) {
            $rows[$i] = [
                $header,
            ];
            foreach ($this->rows as $row) {
                if ($row instanceof TableSeparator) {
                    continue;
                }
                if (isset($row[$i])) {
                    $rows[$i][] = $row[$i];
                }
                elseif ($isCellWithColspan($rows[$i][0])) {
                    // Noop, there is a "title"
                }
                else {
                    $rows[$i][] = null;
                }
            }
        }
    }
    elseif ($vertical) {
        $formatter = $this->output
            ->getFormatter();
        $maxHeaderLength = array_reduce($this->headers[0] ?? [], static fn($max, $header) => max($max, Helper::width(Helper::removeDecoration($formatter, $header))), 0);
        foreach ($this->rows as $row) {
            if ($row instanceof TableSeparator) {
                continue;
            }
            if ($rows) {
                $rows[] = [
                    $divider,
                ];
            }
            $containsColspan = false;
            foreach ($row as $cell) {
                if ($containsColspan = $isCellWithColspan($cell)) {
                    break;
                }
            }
            $headers = $this->headers[0] ?? [];
            $maxRows = max(\count($headers), \count($row));
            for ($i = 0; $i < $maxRows; ++$i) {
                $cell = (string) ($row[$i] ?? '');
                $eol = str_contains($cell, "\r\n") ? "\r\n" : "\n";
                $parts = explode($eol, $cell);
                foreach ($parts as $idx => $part) {
                    if ($headers && !$containsColspan) {
                        if (0 === $idx) {
                            $rows[] = [
                                \sprintf('<comment>%s%s</>: %s', str_repeat(' ', $maxHeaderLength - Helper::width(Helper::removeDecoration($formatter, $headers[$i] ?? ''))), $headers[$i] ?? '', $part),
                            ];
                        }
                        else {
                            $rows[] = [
                                \sprintf('%s  %s', str_pad('', $maxHeaderLength, ' ', \STR_PAD_LEFT), $part),
                            ];
                        }
                    }
                    elseif ('' !== $cell) {
                        $rows[] = [
                            $part,
                        ];
                    }
                }
            }
        }
    }
    else {
        $rows = array_merge($this->headers, [
            $divider,
        ], $this->rows);
    }
    $this->calculateNumberOfColumns($rows);
    $rowGroups = $this->buildTableRows($rows);
    $this->calculateColumnsWidth($rowGroups);
    $isHeader = !$horizontal;
    $isFirstRow = $horizontal;
    $hasTitle = (bool) $this->headerTitle;
    foreach ($rowGroups as $rowGroup) {
        $isHeaderSeparatorRendered = false;
        foreach ($rowGroup as $row) {
            if ($divider === $row) {
                $isHeader = false;
                $isFirstRow = true;
                continue;
            }
            if ($row instanceof TableSeparator) {
                $this->renderRowSeparator();
                continue;
            }
            if (!$row) {
                continue;
            }
            if ($isHeader && !$isHeaderSeparatorRendered) {
                $this->renderRowSeparator(self::SEPARATOR_TOP, $hasTitle ? $this->headerTitle : null, $hasTitle ? $this->style
                    ->getHeaderTitleFormat() : null);
                $hasTitle = false;
                $isHeaderSeparatorRendered = true;
            }
            if ($isFirstRow) {
                $this->renderRowSeparator($horizontal ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM, $hasTitle ? $this->headerTitle : null, $hasTitle ? $this->style
                    ->getHeaderTitleFormat() : null);
                $isFirstRow = false;
                $hasTitle = false;
            }
            if ($vertical) {
                $isHeader = false;
                $isFirstRow = false;
            }
            if ($horizontal) {
                $this->renderRow($row, $this->style
                    ->getCellRowFormat(), $this->style
                    ->getCellHeaderFormat());
            }
            else {
                $this->renderRow($row, $isHeader ? $this->style
                    ->getCellHeaderFormat() : $this->style
                    ->getCellRowFormat());
            }
        }
    }
    $this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style
        ->getFooterTitleFormat());
    $this->cleanup();
    $this->rendered = true;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal