function TwigExtension::mergeAdditionalRenderContext

Same name in other branches
  1. 10 core/modules/sdc/src/Twig/TwigExtension.php \Drupal\sdc\Twig\TwigExtension::mergeAdditionalRenderContext()

Calculates additional context for this template.

Parameters

\Drupal\sdc\Plugin\Component $component: The component.

array $context: The context to update.

Return value

array The additional context to inject to component templates.

1 call to TwigExtension::mergeAdditionalRenderContext()
TwigExtension::addAdditionalContext in core/modules/sdc/src/Twig/TwigExtension.php
Appends additional context to the template based on the template id.

File

core/modules/sdc/src/Twig/TwigExtension.php, line 87

Class

TwigExtension
The twig extension so Drupal can recognize the new code.

Namespace

Drupal\sdc\Twig

Code

protected function mergeAdditionalRenderContext(Component $component, array $context) : array {
    $context['componentMetadata'] = $component->metadata
        ->normalize();
    $component_attributes = [
        'data-component-id' => $component->getPluginId(),
    ];
    if (!isset($context['attributes'])) {
        $context['attributes'] = new Attribute($component_attributes);
    }
    elseif ($context['attributes'] instanceof Attribute) {
        $context['attributes']->merge(new Attribute($component_attributes));
    }
    return $context;
}

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