function EntityViewBuilderTest::testEntityViewBuilderWeight

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest::testEntityViewBuilderWeight()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest::testEntityViewBuilderWeight()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest::testEntityViewBuilderWeight()

Tests weighting of display components.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php, line 186

Class

EntityViewBuilderTest
Tests the entity view builder.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityViewBuilderWeight() {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    // Set a weight for the label component.
    \Drupal::service('entity_display.repository')->getViewDisplay('entity_test', 'entity_test', 'full')
        ->setComponent('label', [
        'weight' => 20,
    ])
        ->save();
    // Create and build a test entity.
    $entity_test = $this->createTestEntity('entity_test');
    $view = $this->container
        ->get('entity_type.manager')
        ->getViewBuilder('entity_test')
        ->view($entity_test, 'full');
    $renderer->renderRoot($view);
    // Check that the weight is respected.
    $this->assertEqual($view['label']['#weight'], 20, 'The weight of a display component is respected.');
}

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