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

Non working proposal for include tag

parent 9e254d2d
Branches
Tags
1 merge request!262First proposal for include
......@@ -60,6 +60,7 @@ class ComponentElementAlter implements TrustedCallbackInterface {
continue;
}
$element["#slots"][$slot_id] = SlotPropType::normalize($slot);
$element["#slots"][$slot_id] = "Yes, render eleemnt is executed";
}
return $element;
}
......
......@@ -22,11 +22,6 @@ use Twig\TwigFunction;
*/
class ComponentNodeVisitor implements NodeVisitorInterface {
/**
* Node name: expr.
*/
const NODE_NAME_EXPR = 'expr';
/**
* 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 {
public function getNodeVisitors(): array {
return [
new ComponentNodeVisitor($this->componentManager),
new IncludeNodeVisitor($this->componentManager),
];
}
......@@ -51,7 +52,11 @@ class TwigExtension extends AbstractExtension {
*/
public function getFunctions() {
return [
new TwigFunction('include', [$this, 'include'], ['needs_environment' => TRUE, 'needs_context' => TRUE, 'is_safe' => ['all']]),
new TwigFunction('include', [$this, 'include'], [
'needs_environment' => TRUE,
'needs_context' => TRUE,
'is_safe' => ['all'],
]),
new TwigFunction('component', [$this, 'renderComponent']),
new TwigFunction('_ui_patterns_preprocess_props', [$this, 'preprocessProps'], ['needs_context' => TRUE]),
];
......@@ -73,7 +78,6 @@ class TwigExtension extends AbstractExtension {
* A hack of Twig\Extension\CoreExtension::include().
* If the template filepath is a component ID, call the renderer service
* 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 {
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