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
24b163c2
Commit
24b163c2
authored
5 months ago
by
christian.wiedemann
Committed by
Pierre Dureau
5 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3492962
by christian.wiedemann, pdureau: Empty slot values when not renderable
parent
5cab2636
No related branches found
No related tags found
1 merge request
!297
Optimize is empty rendering
Pipeline
#365774
passed
5 months ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Element/ComponentElementAlter.php
+25
-3
25 additions, 3 deletions
src/Element/ComponentElementAlter.php
tests/src/Unit/Element/ComponentElementAlterTest.php
+91
-0
91 additions, 0 deletions
tests/src/Unit/Element/ComponentElementAlterTest.php
with
116 additions
and
3 deletions
src/Element/ComponentElementAlter.php
+
25
−
3
View file @
24b163c2
...
...
@@ -124,12 +124,12 @@ class ComponentElementAlter implements TrustedCallbackInterface {
* @return bool
* Returns true for empty.
*/
p
rotected
static
function
isSlotEmpty
(
array
$slot
,
int
$max_level
=
1
,
int
$level
=
0
):
bool
{
p
ublic
static
function
isSlotEmpty
(
array
$slot
,
int
$max_level
=
2
,
int
$level
=
0
):
bool
{
if
(
is_array
(
$slot
)
&&
empty
(
$slot
))
{
return
TRUE
;
}
if
(
$level
<
$max_level
)
{
foreach
(
Element
::
getVisibleC
hildren
(
$slot
)
as
$child
)
{
foreach
(
Element
::
c
hildren
(
$slot
)
as
$child
)
{
if
(
self
::
isSlotEmpty
(
$slot
[
$child
],
$max_level
,
$level
+
1
)
===
FALSE
)
{
return
FALSE
;
}
...
...
@@ -138,7 +138,29 @@ class ComponentElementAlter implements TrustedCallbackInterface {
}
}
}
if
(
Element
::
isEmpty
(
$slot
))
{
return
self
::
checkSlotEmpty
(
$slot
);
}
/**
* Advanced indicates whether the given element is empty.
*
* Before using Element::isEmpty($slot) the slot values are trimmed
* to catch more empty cases.
*
* @param array $slot
* The slot.
*
* @return bool
* Whether the given element is empty.
*/
private
static
function
checkSlotEmpty
(
array
$slot
):
bool
{
foreach
([
'#markup'
,
'#plain_text'
]
as
$key
)
{
if
(
array_key_exists
(
$key
,
$slot
)
&&
empty
(
$slot
[
$key
]))
{
unset
(
$slot
[
$key
]);
}
}
if
(
Element
::
isEmpty
(
$slot
)
||
Element
::
isVisibleElement
(
$slot
)
===
FALSE
)
{
return
TRUE
;
}
return
FALSE
;
...
...
This diff is collapsed.
Click to expand it.
tests/src/Unit/Element/ComponentElementAlterTest.php
0 → 100644
+
91
−
0
View file @
24b163c2
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\Tests\ui_patterns\Unit
;
use
Drupal\Core\Render\Markup
;
use
Drupal\Tests\UnitTestCase
;
use
Drupal\ui_patterns
\Element\ComponentElementAlter
;
/**
* @coversDefaultClass \Drupal\ui_patterns\Element\ComponentElementAlter
*
* @group ui_patterns
*/
final
class
ComponentElementAlterTest
extends
UnitTestCase
{
/**
* Test the method ::canonicalize().
*
* @dataProvider provideSlots
*/
public
function
testIsSlotEmpty
(
array
$slot
,
bool
$isEmpty
):
void
{
$this
->
assertEquals
(
$isEmpty
,
ComponentElementAlter
::
isSlotEmpty
(
$slot
));
}
/**
* Provide data for testCanonicalize.
*/
public
static
function
provideSlots
():
array
{
return
[
[
[
'#markup'
=>
''
],
TRUE
,
],
[
[
'#markup'
=>
Markup
::
create
(
''
)],
TRUE
,
],
[
[
'#markup'
=>
Markup
::
create
(
'test'
)],
FALSE
,
],
[
[
'#cache'
=>
[
'tags'
=>
[
'tag1'
]]],
TRUE
,
],
[
[
'#cache'
=>
[
'tags'
=>
[
'tag2'
]],
'#markup'
=>
NULL
],
TRUE
,
],
[
[
'#cache'
=>
[
'tags'
=>
[
'tag2'
]],
'children'
=>
[[
'#markup'
=>
''
]]],
TRUE
,
],
[
[
'#plain_text'
=>
''
],
TRUE
,
],
[
[
'#plain_text'
=>
''
,
'#preprocess'
=>
'dummy'
],
FALSE
,
],
[
[
'children'
=>
[
[
'#markup'
=>
''
],
[
'#markup'
=>
''
],
],
],
TRUE
,
],
[
[
'children'
=>
[
[
'#markup'
=>
''
],
[
'#markup'
=>
'TEST'
],
],
],
FALSE
,
],
[
[
'children'
=>
[
'#markup'
=>
'some'
]],
FALSE
,
],
[
[
'#markup'
=>
'some data'
],
FALSE
,
],
[
[
'#theme'
=>
'my_theme'
],
FALSE
,
],
[
[
'#weight'
=>
0
],
TRUE
,
],
[
[
'#theme'
=>
'my_theme'
,
'#access'
=>
FALSE
],
TRUE
,
],
];
}
}
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