Skip to content
Snippets Groups Projects
Commit 398d67c9 authored by Pierre Dureau's avatar Pierre Dureau
Browse files

Non working proposal for include tag

parent 9e254d2d
Branches
Tags 8.x-1.0-beta1
No related merge requests found
...@@ -22,11 +22,6 @@ use Twig\TwigFunction; ...@@ -22,11 +22,6 @@ use Twig\TwigFunction;
*/ */
class ComponentNodeVisitor implements NodeVisitorInterface { class ComponentNodeVisitor implements NodeVisitorInterface {
/**
* Node name: expr.
*/
const NODE_NAME_EXPR = 'expr';
/** /**
* The component plugin manager. * The component plugin manager.
*/ */
......
<?php
namespace Drupal\ui_patterns\Template;
use Drupal\Core\Template\ComponentNodeVisitor as CoreComponentNodeVisitor;
use Drupal\Core\Theme\ComponentPluginManager;
use Twig\Environment;
use Twig\Node\EmbedNode;
use Twig\Node\Expression\FunctionExpression;
use Twig\Node\IncludeNode;
use Twig\Node\Node;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\TwigFunction;
/**
* Provides a IncludeNodeVisitor to change the generated parse-tree.
*
* @internal
*/
class IncludeNodeVisitor implements NodeVisitorInterface {
/**
* The component plugin manager.
*/
protected ComponentPluginManager $componentManager;
/**
* Constructs a new ComponentNodeVisitor object.
*
* @param \Drupal\Core\Theme\ComponentPluginManager $component_plugin_manager
* The component plugin manager.
*/
public function __construct(ComponentPluginManager $component_plugin_manager) {
$this->componentManager = $component_plugin_manager;
}
/**
* {@inheritdoc}
*/
public function enterNode(Node $node, Environment $env): Node {
return $node;
}
/**
* {@inheritdoc}
*/
public function leaveNode(Node $node, Environment $env): ?Node {
if (!$node instanceof IncludeNode) {
return $node;
}
if ($node instanceof EmbedNode) {
// @todo Embed is a different beast.
return $node;
}
$template = $node->getNode("expr")->getAttribute("value");
if (!preg_match('/^[a-z]([a-zA-Z0-9_-]*[a-zA-Z0-9])*:[a-z]([a-zA-Z0-9_-]*[a-zA-Z0-9])*$/', $template)) {
return $node;
}
$line = $node->getTemplateLine();
$arguments = new Node([
$node->getNode("expr"),
$node->getNode('variables'),
]);
$function = new FunctionExpression(
new TwigFunction(
"include",
["Twig\Extension\CoreExtension", "include"],
),
$arguments,
$line,
);
return $function;
}
/**
* {@inheritdoc}
*/
public function getPriority(): int {
$priority = &drupal_static(__METHOD__);
if (!isset($priority)) {
$original_node_visitor = new CoreComponentNodeVisitor($this->componentManager);
// Ensure that this component node visitor's priority is higher than
// core's node visitor class for components, because this class has to run
// core's class.
$priority = $original_node_visitor->getPriority() + 1;
}
return is_numeric($priority) ? (int) $priority : 0;
}
}
...@@ -43,6 +43,7 @@ class TwigExtension extends AbstractExtension { ...@@ -43,6 +43,7 @@ class TwigExtension extends AbstractExtension {
public function getNodeVisitors(): array { public function getNodeVisitors(): array {
return [ return [
new ComponentNodeVisitor($this->componentManager), new ComponentNodeVisitor($this->componentManager),
new IncludeNodeVisitor($this->componentManager),
]; ];
} }
...@@ -73,7 +74,6 @@ class TwigExtension extends AbstractExtension { ...@@ -73,7 +74,6 @@ class TwigExtension extends AbstractExtension {
* A hack of Twig\Extension\CoreExtension::include(). * A hack of Twig\Extension\CoreExtension::include().
* If the template filepath is a component ID, call the renderer service * If the template filepath is a component ID, call the renderer service
* instead of the normal include function. * instead of the normal include function.
* Works also for the include tag.
*/ */
public function include(Environment $env, $context, $template, $variables = [], $withContext = TRUE, $ignoreMissing = FALSE, $sandboxed = FALSE): array|string { public function include(Environment $env, $context, $template, $variables = [], $withContext = TRUE, $ignoreMissing = FALSE, $sandboxed = FALSE): array|string {
if (!preg_match('/^[a-z]([a-zA-Z0-9_-]*[a-zA-Z0-9])*:[a-z]([a-zA-Z0-9_-]*[a-zA-Z0-9])*$/', $template)) { if (!preg_match('/^[a-z]([a-zA-Z0-9_-]*[a-zA-Z0-9])*:[a-z]([a-zA-Z0-9_-]*[a-zA-Z0-9])*$/', $template)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment