Issue #3419621 by dburiak, sijumpk, smustgrave, pameeela, alexpott: tablePositionSticky should not be called on a non-array variable

merge-requests/7363/head
nod_ 2024-04-08 10:50:38 +02:00
parent f1601a487e
commit 7e28ed514a
No known key found for this signature in database
GPG Key ID: 76624892606FA197
2 changed files with 33 additions and 1 deletions

View File

@ -2,6 +2,7 @@
namespace Drupal\KernelTests\Core\Theme;
use Drupal\claro\ClaroPreRender;
use Drupal\KernelTests\KernelTestBase;
/**
@ -38,4 +39,35 @@ class ClaroTableTest extends KernelTestBase {
$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);
}
}

View File

@ -193,7 +193,7 @@ class ClaroPreRender implements TrustedCallbackInterface {
* 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';
}