function SvgIconBuilder::buildRenderArray

Same name in other branches
  1. 9 core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php \Drupal\Core\Layout\Icon\SvgIconBuilder::buildRenderArray()
  2. 10 core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php \Drupal\Core\Layout\Icon\SvgIconBuilder::buildRenderArray()
  3. 11.x core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php \Drupal\Core\Layout\Icon\SvgIconBuilder::buildRenderArray()

Builds a render array representation of an SVG.

Parameters

mixed[] $regions: An array keyed by region name, with each element containing the 'height', 'width', and 'x' and 'y' offsets of each region.

int $width: The width of the SVG.

int $height: The height of the SVG.

int|null $stroke_width: The width of region borders.

Return value

array A render array representing a SVG icon.

1 call to SvgIconBuilder::buildRenderArray()
SvgIconBuilder::build in core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php
Builds a render array representation of an SVG based on an icon map.

File

core/lib/Drupal/Core/Layout/Icon/SvgIconBuilder.php, line 78

Class

SvgIconBuilder
Builds SVG layout icons.

Namespace

Drupal\Core\Layout\Icon

Code

protected function buildRenderArray(array $regions, $width, $height, $stroke_width) {
    $build = [
        '#type' => 'html_tag',
        '#tag' => 'svg',
        '#attributes' => [
            'width' => $width,
            'height' => $height,
            'class' => [
                'layout-icon',
            ],
        ],
    ];
    if ($this->id) {
        $build['#attributes']['class'][] = Html::getClass('layout-icon--' . $this->id);
    }
    if ($this->label) {
        $build['title'] = [
            '#type' => 'html_tag',
            '#tag' => 'title',
            '#value' => $this->label,
        ];
    }
    // Append each polygon to the SVG.
    foreach ($regions as $region => $attributes) {
        // Wrapping with a <g> element allows for metadata to exist alongside the
        // rectangle.
        $build['region'][$region] = [
            '#type' => 'html_tag',
            '#tag' => 'g',
        ];
        $build['region'][$region]['title'] = [
            '#type' => 'html_tag',
            '#tag' => 'title',
            '#value' => $region,
        ];
        // Assemble the rectangle SVG element.
        $build['region'][$region]['rect'] = [
            '#type' => 'html_tag',
            '#tag' => 'rect',
            '#attributes' => [
                'x' => $attributes['x'],
                'y' => $attributes['y'],
                'width' => $attributes['width'],
                'height' => $attributes['height'],
                'stroke-width' => $stroke_width,
                'class' => [
                    'layout-icon__region',
                    Html::getClass('layout-icon__region--' . $region),
                ],
            ],
        ];
    }
    return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.