function ThemeTest::testAttributeMerging

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Kernel/Theme/ThemeTest.php \Drupal\Tests\system\Kernel\Theme\ThemeTest::testAttributeMerging()
  2. 10 core/modules/system/tests/src/Kernel/Theme/ThemeTest.php \Drupal\Tests\system\Kernel\Theme\ThemeTest::testAttributeMerging()
  3. 11.x core/modules/system/tests/src/Kernel/Theme/ThemeTest.php \Drupal\Tests\system\Kernel\Theme\ThemeTest::testAttributeMerging()

Tests attribute merging.

Render arrays that use a render element and templates (and hence call template_preprocess()) must ensure the attributes at different occasions are all merged correctly:

  • $variables['attributes'] as passed in to the theme hook implementation.
  • the render element's #attributes
  • any attributes set in the template's preprocessing function

File

core/modules/system/tests/src/Kernel/Theme/ThemeTest.php, line 39

Class

ThemeTest
Tests low-level theme functions.

Namespace

Drupal\Tests\system\Kernel\Theme

Code

public function testAttributeMerging() {
    $theme_test_render_element = [
        'elements' => [
            '#attributes' => [
                'data-foo' => 'bar',
            ],
        ],
        'attributes' => [
            'id' => 'bazinga',
        ],
    ];
    $this->assertThemeOutput('theme_test_render_element', $theme_test_render_element, '<div id="bazinga" data-foo="bar" data-variables-are-preprocessed></div>' . "\n");
}

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