class TwigExtension

Same name in this branch
  1. 11.x core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension
Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension
  2. 8.9.x core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension
  3. 10 core/modules/sdc/src/Twig/TwigExtension.php \Drupal\sdc\Twig\TwigExtension
  4. 10 core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension

The twig extension so Drupal can recognize the new code.

@internal

Hierarchy

  • class \Drupal\sdc\Twig\TwigExtension extends \Twig\Extension\AbstractExtension

Expanded class hierarchy of TwigExtension

File

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

Namespace

Drupal\sdc\Twig
View source
final class TwigExtension extends AbstractExtension {
    
    /**
     * Creates TwigExtension.
     *
     * @param \Drupal\sdc\ComponentPluginManager $pluginManager
     *   The component plugin manager.
     * @param \Drupal\sdc\Component\ComponentValidator $componentValidator
     *   The component validator.
     */
    public function __construct(ComponentPluginManager $pluginManager, ComponentValidator $componentValidator) {
    }
    
    /**
     * {@inheritdoc}
     */
    public function getNodeVisitors() : array {
        return [
            new ComponentNodeVisitor($this->pluginManager),
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFunctions() : array {
        return [
            new TwigFunction('sdc_additional_context', [
                $this,
                'addAdditionalContext',
            ], [
                'needs_context' => TRUE,
            ]),
            new TwigFunction('sdc_validate_props', [
                $this,
                'validateProps',
            ], [
                'needs_context' => TRUE,
            ]),
        ];
    }
    
    /**
     * Appends additional context to the template based on the template id.
     *
     * @param array &$context
     *   The context.
     * @param string $component_id
     *   The component ID.
     *
     * @throws \Drupal\sdc\Exception\ComponentNotFoundException
     */
    public function addAdditionalContext(array &$context, string $component_id) : void {
        $context = $this->mergeAdditionalRenderContext($this->pluginManager
            ->find($component_id), $context);
    }
    
    /**
     * Calculates additional context for this template.
     *
     * @param \Drupal\sdc\Plugin\Component $component
     *   The component.
     * @param array $context
     *   The context to update.
     *
     * @return array
     *   The additional context to inject to component templates.
     */
    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;
    }
    
    /**
     * Validates the props in development environments.
     *
     * @param array $context
     *   The context provided to the component.
     * @param string $component_id
     *   The component ID.
     *
     * @throws \Drupal\sdc\Exception\InvalidComponentException
     */
    public function validateProps(array &$context, string $component_id) : void {
        assert($this->doValidateProps($context, $component_id));
    }
    
    /**
     * Performs the actual validation of the schema for the props.
     *
     * @param array $context
     *   The context provided to the component.
     * @param string $component_id
     *   The component ID.
     *
     * @return bool
     *   TRUE if it's valid.
     *
     * @throws \Drupal\sdc\Exception\InvalidComponentException
     */
    protected function doValidateProps(array $context, string $component_id) : bool {
        try {
            return $this->componentValidator
                ->validateProps($context, $this->pluginManager
                ->find($component_id));
        } catch (ComponentNotFoundException $e) {
            throw new InvalidComponentException($e->getMessage(), $e->getCode(), $e);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
TwigExtension::addAdditionalContext public function Appends additional context to the template based on the template id.
TwigExtension::doValidateProps protected function Performs the actual validation of the schema for the props.
TwigExtension::getFunctions public function
TwigExtension::getNodeVisitors public function
TwigExtension::mergeAdditionalRenderContext protected function Calculates additional context for this template.
TwigExtension::validateProps public function Validates the props in development environments.
TwigExtension::__construct public function Creates TwigExtension.

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