diff --git a/core/tests/Drupal/KernelTests/Core/Theme/ClaroTableTest.php b/core/tests/Drupal/KernelTests/Core/Theme/ClaroTableTest.php index 70dcd6606e2d41c9acdfa4b531d843c4bf61f17b..213f54b34e78c8776c6440d58475f63911ef6126 100644 --- a/core/tests/Drupal/KernelTests/Core/Theme/ClaroTableTest.php +++ b/core/tests/Drupal/KernelTests/Core/Theme/ClaroTableTest.php @@ -2,6 +2,7 @@ namespace Drupal\KernelTests\Core\Theme; +use Drupal\claro\ClaroPreRender; use Drupal\KernelTests\KernelTestBase; /** @@ -38,4 +39,35 @@ public function testThemeTableStickyHeaders() { $this->assertRaw('position-sticky'); } + /** + * Confirm Claro prerender callback is not executed for non-array class. + */ + public function testThemeTablePositionStickyPreRender(): void { + // Enable the Claro theme. + \Drupal::service('theme_installer')->install(['claro']); + $this->config('system.theme')->set('default', 'claro')->save(); + $header = ['one', 'two', 'three']; + $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; + $table = [ + '#type' => 'table', + '#header' => $header, + '#rows' => $rows, + '#sticky' => TRUE, + '#attributes' => [ + 'class' => 'class', + ], + '#pre_render' => [ + [ + ClaroPreRender::class, + 'tablePositionSticky', + ], + ], + ]; + + $renderedTable = \Drupal::service('renderer')->renderRoot($table); + + // Confirm that table is rendered. + $this->assertStringContainsString('class="class"', $renderedTable); + } + } diff --git a/core/themes/claro/src/ClaroPreRender.php b/core/themes/claro/src/ClaroPreRender.php index e18d0906c9849f52fccf56a08d938b45b860d976..a47f4ac26e71e4ff4aab3eac5adb98b6810f9141 100644 --- a/core/themes/claro/src/ClaroPreRender.php +++ b/core/themes/claro/src/ClaroPreRender.php @@ -193,7 +193,7 @@ public static function messagePlaceholder(array $element) { * Prerender callback for table elements. */ public static function tablePositionSticky(array $element) { - if (isset($element['#attributes']['class']) && in_array('sticky-enabled', $element['#attributes']['class'])) { + if (isset($element['#attributes']['class']) && is_array($element['#attributes']['class']) && in_array('sticky-enabled', $element['#attributes']['class'], TRUE)) { unset($element['#attributes']['class'][array_search('sticky-enabled', $element['#attributes']['class'])]); $element['#attributes']['class'][] = 'position-sticky'; }