Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
components
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
components
Merge requests
!31
Issue
#3216184
: Add HTML comments when Twig debugging is enabled
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3216184
: Add HTML comments when Twig debugging is enabled
issue/components-3216184:3216184-add-html-comments
into
3.x
Overview
0
Commits
1
Pipelines
0
Changes
3
Open
Dieter Holvoet
requested to merge
issue/components-3216184:3216184-add-html-comments
into
3.x
2 years ago
Overview
0
Commits
1
Pipelines
0
Changes
3
Expand
0
0
Merge request reports
Compare
3.x
3.x (HEAD)
and
latest version
latest version
5434edbc
1 commit,
2 years ago
3 files
+
165
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
src/Template/DebugNodeVisitor.php
0 → 100644
+
138
−
0
Options
<?php
namespace
Drupal\components\Template
;
use
Twig\Environment
;
use
Twig\Node\ModuleNode
;
use
Twig\Node\Node
;
use
Twig\Node\TextNode
;
use
Twig\NodeVisitor\NodeVisitorInterface
;
/**
* A Twig node visitor that adds debug information around components.
*/
class
DebugNodeVisitor
implements
NodeVisitorInterface
{
/**
* The components registry service.
*
* @var \Drupal\components\Template\ComponentsRegistry
*/
protected
$componentsRegistry
;
/**
* Constructs a new DebugNodeVisitor object.
*
* @param \Drupal\components\Template\ComponentsRegistry $componentsRegistry
* The components registry service.
*/
public
function
__construct
(
ComponentsRegistry
$componentsRegistry
)
{
$this
->
componentsRegistry
=
$componentsRegistry
;
}
/**
* {@inheritdoc}
*/
public
function
enterNode
(
Node
$node
,
Environment
$env
):
Node
{
if
(
!
$env
->
isDebug
())
{
return
$node
;
}
if
(
!
$node
instanceof
ModuleNode
)
{
return
$node
;
}
if
(
!
$source
=
$node
->
getSourceContext
())
{
return
$node
;
}
$name
=
$source
->
getName
();
$path
=
$this
->
getComponentPath
(
$name
);
if
(
$path
===
NULL
)
{
return
$node
;
}
if
(
$this
->
isNodeEmpty
(
$node
->
getNode
(
'body'
)))
{
// This node is empty.
return
$node
;
}
$startNodes
=
[
new
TextNode
(
'<!-- THEME DEBUG -->'
,
0
),
new
TextNode
(
sprintf
(
'<!-- COMPONENT: %s -->'
,
$name
),
0
),
];
if
(
$name
!==
$path
)
{
$startNodes
[]
=
new
TextNode
(
sprintf
(
"<!-- BEGIN OUTPUT from '%s' -->"
,
$path
),
0
);
}
$node
->
getNode
(
'display_start'
)
->
setNode
(
'_components_debug'
,
new
Node
(
$startNodes
));
$endNodes
=
[
new
TextNode
(
sprintf
(
"<!-- END OUTPUT from '%s' -->"
,
$path
),
0
),
];
$node
->
getNode
(
'display_end'
)
->
setNode
(
'_components_debug'
,
new
Node
(
$endNodes
));
return
$node
;
}
/**
* {@inheritdoc}
*/
public
function
leaveNode
(
Node
$node
,
Environment
$env
):
?Node
{
return
$node
;
}
/**
* {@inheritdoc}
*/
public
function
getPriority
():
int
{
return
0
;
}
/**
* Checks whether a template is a component and if so,
* returns the path to the component.
*/
protected
function
getComponentPath
(
string
$templateName
):
?string
{
if
(
$templateName
[
0
]
!==
'@'
)
{
return
NULL
;
}
if
(
strpos
(
substr
(
$templateName
,
2
),
'/'
)
===
FALSE
)
{
return
NULL
;
}
$extension
=
substr
(
$templateName
,
strrpos
(
$templateName
,
'.'
,
-
1
));
if
(
$extension
!==
'.twig'
&&
$extension
!==
'.html'
&&
$extension
!==
'.svg'
)
{
return
NULL
;
}
return
$this
->
componentsRegistry
->
getTemplate
(
$templateName
);
}
/**
* Checks whether a node - or one of its subnodes - actually contain something.
*/
protected
function
isNodeEmpty
(
Node
$node
):
bool
{
$subNodes
=
iterator_to_array
(
$node
);
if
(
count
(
$subNodes
)
>
1
)
{
return
FALSE
;
}
foreach
(
$subNodes
as
$subNode
)
{
if
(
!
$this
->
isNodeEmpty
(
$subNode
))
{
return
FALSE
;
}
}
return
TRUE
;
}
}
Loading