Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ui_patterns
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
ui_patterns
Commits
398d67c9
Commit
398d67c9
authored
7 months ago
by
Pierre Dureau
Browse files
Options
Downloads
Patches
Plain Diff
Non working proposal for include tag
parent
9e254d2d
Branches
Branches containing commit
Tags
8.x-1.0-beta1
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/Template/ComponentNodeVisitor.php
+0
-5
0 additions, 5 deletions
src/Template/ComponentNodeVisitor.php
src/Template/IncludeNodeVisitor.php
+90
-0
90 additions, 0 deletions
src/Template/IncludeNodeVisitor.php
src/Template/TwigExtension.php
+1
-1
1 addition, 1 deletion
src/Template/TwigExtension.php
with
91 additions
and
6 deletions
src/Template/ComponentNodeVisitor.php
+
0
−
5
View file @
398d67c9
...
@@ -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.
*/
*/
...
...
This diff is collapsed.
Click to expand it.
src/Template/IncludeNodeVisitor.php
0 → 100755
+
90
−
0
View file @
398d67c9
<?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
;
}
}
This diff is collapsed.
Click to expand it.
src/Template/TwigExtension.php
+
1
−
1
View file @
398d67c9
...
@@ -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
))
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment