diff --git a/core/lib/Drupal/Core/Layout/IconGenerator.php b/core/lib/Drupal/Core/Layout/IconGenerator.php index 39de9a6..eb1f86b 100644 --- a/core/lib/Drupal/Core/Layout/IconGenerator.php +++ b/core/lib/Drupal/Core/Layout/IconGenerator.php @@ -10,9 +10,9 @@ class IconGenerator implements IconGeneratorInterface { /** * {@inheritdoc} */ - public function generateSvgFromIconMap(array $icon_map, $width, $height, $stroke_width, $padding, $fill, $stroke) { + public function generateSvgFromIconMap(array $icon_map, $label, $width, $height, $stroke_width, $padding, $fill, $stroke) { $regions = $this->calculateSvgValues($icon_map, $width, $height, $stroke_width, $padding); - return $this->generateSvg($regions, $width, $height, $stroke_width, $fill, $stroke); + return $this->generateSvg($regions, $label, $width, $height, $stroke_width, $fill, $stroke); } /** @@ -21,6 +21,8 @@ public function generateSvgFromIconMap(array $icon_map, $width, $height, $stroke * @param mixed[] $regions * An array keyed by region name, with each element containing the 'height', * 'width', and 'x' and 'y' offsets of each region. + * @param string $label + * The label of the layout. * @param int $width * The width of the generated SVG. * @param int $height @@ -35,7 +37,7 @@ public function generateSvgFromIconMap(array $icon_map, $width, $height, $stroke * @return array * A render array representing a SVG icon. */ - protected function generateSvg(array $regions, $width, $height, $stroke_width, $fill, $stroke) { + protected function generateSvg(array $regions, $label, $width, $height, $stroke_width, $fill, $stroke) { $build = [ '#type' => 'html_tag', '#tag' => 'svg', @@ -43,24 +45,29 @@ protected function generateSvg(array $regions, $width, $height, $stroke_width, $ 'width' => $width, 'height' => $height, ], + 'title' => [ + '#type' => 'html_tag', + '#tag' => 'title', + '#value' => $label, + ], ]; // Append each polygon to the SVG. foreach ($regions as $region => $attributes) { // Group our regions allows for metadata, nested elements, and tooltips. - $build[$region] = [ + $build['region'][$region] = [ '#type' => 'html_tag', '#tag' => 'g', ]; - $build[$region]['title'] = [ + $build['region'][$region]['title'] = [ '#type' => 'html_tag', '#tag' => 'title', '#value' => $region, ]; // Assemble the rectangle SVG element. - $build[$region]['rect'] = [ + $build['region'][$region]['rect'] = [ '#type' => 'html_tag', '#tag' => 'rect', '#attributes' => [ @@ -167,6 +174,10 @@ protected function getOffset($delta, $length, $stroke_width, $padding) { * The height or width of a region. */ protected function getLength($number_of_regions, $length, $stroke_width, $padding) { + if ($number_of_regions === 0) { + return 0; + } + $total_stroke = $number_of_regions * $stroke_width; $total_padding = ($number_of_regions - 1) * $padding; return ($length - $total_padding - $total_stroke) / $number_of_regions; diff --git a/core/lib/Drupal/Core/Layout/IconGeneratorInterface.php b/core/lib/Drupal/Core/Layout/IconGeneratorInterface.php index 38cd949..a738ed7 100644 --- a/core/lib/Drupal/Core/Layout/IconGeneratorInterface.php +++ b/core/lib/Drupal/Core/Layout/IconGeneratorInterface.php @@ -30,6 +30,8 @@ * - [top] * - [first, second, second, third] * - [first, bottom, bottom, bottom]. + * @param string $label + * The label of the layout. * @param int $width * The width of the generated SVG. * @param int $height @@ -46,6 +48,6 @@ * @return array * A render array representing a SVG icon. */ - public function generateSvgFromIconMap(array $icon_map, $width, $height, $stroke_width, $padding, $fill, $stroke); + public function generateSvgFromIconMap(array $icon_map, $label, $width, $height, $stroke_width, $padding, $fill, $stroke); } diff --git a/core/lib/Drupal/Core/Layout/LayoutDefinition.php b/core/lib/Drupal/Core/Layout/LayoutDefinition.php index 58d3c33..4bbc24b 100644 --- a/core/lib/Drupal/Core/Layout/LayoutDefinition.php +++ b/core/lib/Drupal/Core/Layout/LayoutDefinition.php @@ -436,10 +436,11 @@ public function getIcon($width = 125, $height = 150, $stroke_width = 1, $padding '#uri' => $icon_path, '#width' => $width, '#height' => $height, + '#alt' => $this->getLabel(), ]; } elseif ($icon_map = $this->getIconMap()) { - $icon = $this->getIconGenerator()->generateSvgFromIconMap($icon_map, $width, $height, $stroke_width, $padding, $fill, $stroke); + $icon = $this->getIconGenerator()->generateSvgFromIconMap($icon_map, $this->getLabel(), $width, $height, $stroke_width, $padding, $fill, $stroke); } return $icon; } diff --git a/core/tests/Drupal/KernelTests/Core/Layout/IconGeneratorTest.php b/core/tests/Drupal/KernelTests/Core/Layout/IconGeneratorTest.php index c08475a..ca2e1d9 100644 --- a/core/tests/Drupal/KernelTests/Core/Layout/IconGeneratorTest.php +++ b/core/tests/Drupal/KernelTests/Core/Layout/IconGeneratorTest.php @@ -21,10 +21,10 @@ class IconGeneratorTest extends KernelTestBase { * * @dataProvider providerTestGenerateSvgFromIconMap */ - public function testGenerateSvgFromIconMap($icon_map, $expected, $stroke_width = 2) { + public function testGenerateSvgFromIconMap($icon_map, $label, $expected, $stroke_width = 2) { $renderer = $this->container->get('renderer'); $icon_generator = new IconGenerator(); - $build = $icon_generator->generateSvgFromIconMap($icon_map, 250, 300, $stroke_width, 4, 'lightgray', 'black'); + $build = $icon_generator->generateSvgFromIconMap($icon_map, $label, 250, 300, $stroke_width, 4, 'lightgray', 'black'); $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($build, $renderer) { return $renderer->render($build); }); @@ -34,14 +34,18 @@ public function testGenerateSvgFromIconMap($icon_map, $expected, $stroke_width = public function providerTestGenerateSvgFromIconMap() { $data = []; $data['empty'][] = []; + $data['empty'][] = 'Empty'; $data['empty'][] = <<<'EOD' - +Empty + EOD; $data['two_column'][] = [['left', 'right']]; + $data['two_column'][] = 'Two Column'; $data['two_column'][] = <<<'EOD' -left +Two Column +left right @@ -52,8 +56,10 @@ public function providerTestGenerateSvgFromIconMap() { EOD; $data['two_column_no_stroke'][] = [['left', 'right']]; + $data['two_column_no_stroke'][] = 'Two Column (no stroke)'; $data['two_column_no_stroke'][] = <<<'EOD' -left +Two Column (no stroke) +left right @@ -71,8 +77,10 @@ public function providerTestGenerateSvgFromIconMap() { ['footer_left', 'footer_right'], ['footer_full'], ]; + $data['stacked'][] = 'Stacked'; $data['stacked'][] = <<<'EOD' -sidebar +Stacked +sidebar top