trait ComponentRendererTrait

Same name and namespace in other branches
  1. 10 core/modules/sdc/tests/src/Traits/ComponentRendererTrait.php \Drupal\Tests\sdc\Traits\ComponentRendererTrait

Defines a trait for rendering components.

@internal

Hierarchy

1 file declares its use of ComponentRendererTrait
ComponentKernelTestBase.php in core/modules/sdc/tests/src/Kernel/ComponentKernelTestBase.php

File

core/modules/sdc/tests/src/Traits/ComponentRendererTrait.php, line 16

Namespace

Drupal\Tests\sdc\Traits
View source
trait ComponentRendererTrait {
    
    /**
     * Renders a component for testing sake.
     *
     * @param array $component
     *   Component render array.
     * @param \Drupal\Core\Render\BubbleableMetadata|null $metadata
     *   Bubble metadata.
     *
     * @return \Symfony\Component\DomCrawler\Crawler
     *   Crawler for introspecting the rendered component.
     */
    protected function renderComponentRenderArray(array $component, ?BubbleableMetadata $metadata = NULL) : Crawler {
        $component = [
            '#type' => 'container',
            '#attributes' => [
                'id' => 'sdc-wrapper',
            ],
            'component' => $component,
        ];
        $metadata = $metadata ?: new BubbleableMetadata();
        $context = new RenderContext();
        $renderer = \Drupal::service('renderer');
        $output = $renderer->executeInRenderContext($context, fn() => $renderer->render($component));
        if (!$context->isEmpty()) {
            $metadata->addCacheableDependency($context->pop());
        }
        return new Crawler((string) $output);
    }

}

Members

Title Sort descending Modifiers Object type Summary
ComponentRendererTrait::renderComponentRenderArray protected function Renders a component for testing sake.

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