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
Merge requests
!25
Resolve
#3311480
"add set_attribute and add_class filters"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve
#3311480
"add set_attribute and add_class filters"
issue/ui_patterns-3311480:3311480-remove-the-need
into
8.x-1.x
Overview
16
Commits
5
Pipelines
0
Changes
3
All threads resolved!
Show all comments
Merged
Pierre Dureau
requested to merge
issue/ui_patterns-3311480:3311480-remove-the-need
into
8.x-1.x
2 years ago
Overview
16
Commits
5
Pipelines
0
Changes
3
All threads resolved!
Show all comments
Expand
Closes
#3311480
0
0
Merge request reports
Compare
8.x-1.x
version 4
9d7344b3
1 year ago
version 3
da28ab0d
1 year ago
version 2
8bfab0e0
1 year ago
version 1
7874e0c5
2 years ago
8.x-1.x (base)
and
latest version
latest version
181d6add
5 commits,
1 year ago
version 4
9d7344b3
4 commits,
1 year ago
version 3
da28ab0d
4 commits,
1 year ago
version 2
8bfab0e0
3 commits,
1 year ago
version 1
7874e0c5
2 commits,
2 years ago
3 files
+
336
−
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/AttributesFilterTrait.php
0 → 100644
+
117
−
0
Options
<?php
namespace
Drupal\ui_patterns\Template
;
use
Drupal\Core\Template\Attribute
;
use
Drupal\Core\Template\AttributeHelper
;
/**
* Methods for the set_attribute and add_class filters.
*
* In a trait, to be usable in other places.
*/
trait
AttributesFilterTrait
{
/**
* Add a value into the class attributes of a given element.
*
* @param mixed $element
* A render array.
* @param string[]|string ...$classes
* The classes to add on element. Arguments can include string keys directly
* or arrays of string keys.
*
* @return mixed
* The element with the given class(es) in attributes or the unchanged
* element if passed value is not an array.
*
* @see Drupal\Core\Template\TwigExtension::addClass()
*/
public
function
addClass
(
mixed
$element
,
...
$classes
):
mixed
{
if
(
!
\is_array
(
$element
))
{
return
$element
;
}
if
(
$this
->
arrayIsList
(
$element
))
{
foreach
(
$element
as
$index
=>
$item
)
{
if
(
!
\is_array
(
$item
))
{
continue
;
}
$element
[
$index
]
=
$this
->
addClass
(
$item
,
...
$classes
);
}
return
$element
;
}
$attributes
=
new
Attribute
(
$element
[
'#attributes'
]
??
[]);
$attributes
->
addClass
(
...
$classes
);
$element
[
'#attributes'
]
=
$attributes
->
toArray
();
// Make sure element gets rendered again.
unset
(
$element
[
'#printed'
]);
return
$element
;
}
/**
* Set attribute on a given element.
*
* @param mixed $element
* A render array.
* @param string $name
* The attribute name.
* @param mixed $value
* (optional) The attribute value.
*
* @return mixed
* The element with the given sanitized attribute's value or the unchanged
* element if passed value is not an array.
*
* @see Drupal\Core\Template\TwigExtension::setAttribute()
*/
public
function
setAttribute
(
mixed
$element
,
string
$name
,
mixed
$value
=
NULL
):
mixed
{
if
(
!
\is_array
(
$element
))
{
return
$element
;
}
if
(
$this
->
arrayIsList
(
$element
))
{
foreach
(
$element
as
$index
=>
$item
)
{
if
(
!
\is_array
(
$item
))
{
continue
;
}
$element
[
$index
]
=
$this
->
setAttribute
(
$item
,
$name
,
$value
);
}
return
$element
;
}
$element
[
'#attributes'
]
=
AttributeHelper
::
mergeCollections
(
$element
[
'#attributes'
]
??
[],
new
Attribute
([
$name
=>
$value
])
);
// Make sure element gets rendered again.
unset
(
$element
[
'#printed'
]);
return
$element
;
}
/**
* Checks whether a given array is a list.
*
* Same as array_is_list() but compatible with PHP8.
*
* @param array $array
* The array being evaluated.
*
* @return bool
* Returns true if array is a list, false otherwise.
*
* @see https://www.php.net/manual/en/function.array-is-list.php#126794
*/
private
function
arrayIsList
(
array
$array
):
bool
{
$i
=
-
1
;
foreach
(
$array
as
$k
=>
$v
)
{
++
$i
;
if
(
$k
!==
$i
)
{
return
FALSE
;
}
}
return
TRUE
;
}
}
Loading