Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
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
drupal
Merge requests
!10179
Draft: Resolve
#3443882
"Plugin option attribute"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Closed
Draft: Resolve
#3443882
"Plugin option attribute"
issue/drupal-3443882:3443882-plugin-option-attribute
into
11.x
Overview
0
Commits
15
Pipelines
13
Changes
16
Closed
godotislate
requested to merge
issue/drupal-3443882:3443882-plugin-option-attribute
into
11.x
8 months ago
Overview
0
Commits
15
Pipelines
13
Changes
16
Expand
Closes
#3443882
0
0
Merge request reports
Compare
11.x
version 12
1a36b933
7 months ago
version 11
c0a2ae9c
7 months ago
version 10
61235cd2
7 months ago
version 9
f769e915
7 months ago
version 8
f9438935
7 months ago
version 7
dc0b9860
8 months ago
version 6
868f1735
8 months ago
version 5
65aa9a09
8 months ago
version 4
4525cc6a
8 months ago
version 3
5e5002d5
8 months ago
version 2
d990272f
8 months ago
version 1
a5bbb107
8 months ago
11.x (base)
and
latest version
latest version
c40a7ca6
15 commits,
7 months ago
version 12
1a36b933
14 commits,
7 months ago
version 11
c0a2ae9c
10 commits,
7 months ago
version 10
61235cd2
9 commits,
7 months ago
version 9
f769e915
9 commits,
7 months ago
version 8
f9438935
8 commits,
7 months ago
version 7
dc0b9860
6 commits,
8 months ago
version 6
868f1735
6 commits,
8 months ago
version 5
65aa9a09
5 commits,
8 months ago
version 4
4525cc6a
5 commits,
8 months ago
version 3
5e5002d5
3 commits,
8 months ago
version 2
d990272f
2 commits,
8 months ago
version 1
a5bbb107
2 commits,
8 months ago
16 files
+
605
−
34
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
16
Search (e.g. *.vue) (Ctrl+P)
core/lib/Drupal/Component/Plugin/Attribute/PluginProperty.php
0 → 100644
+
120
−
0
Options
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\Component\Plugin\Attribute
;
use
Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
;
use
Drupal\Component\Utility\NestedArray
;
/**
* Attribute class for adding optional property values to plugin definitions.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class
PluginProperty
extends
AttributeBase
implements
PluginPropertyInterface
{
/**
* Constructs a plugin property object.
*
* @param int|string|array{int,string} $key
* The property key to set in the plugin in the definition. If the key is an
* array, it will be treated as a nested key, with the outermost keys coming
* first.
* @param mixed $value
* The value to set the property to.
* @param class-string[] $allowedPluginClasses
* List of plugin attribute class names that this property attribute can set
* properties on. If list is empty, property can be applied to any plugin.
* @param bool $allowOverride
* Whether the plugin can override property value already defined and set.
* @param string|null $provider
* The provider of the attribute class.
* @param mixed $addToDefinitionCallback
* Callable that adds the property to the plugin definition. If NULL and the
* plugin definition is an array, the property will be set by nested key on
* the array. Callback function parameters are this plugin property
* attribute object and the plugin definition:
* @code
* function exampleAddToDefinitionCallback(PluginProperty $attribute, array|object $definition): array|object {}
* @endcode
*/
public
function
__construct
(
public
readonly
int
|
string
|
array
$key
,
public
readonly
mixed
$value
,
public
readonly
array
$allowedPluginClasses
=
[],
public
readonly
bool
$allowOverride
=
FALSE
,
public
?string
$provider
=
'core'
,
public
mixed
$addToDefinitionCallback
=
NULL
,
)
{
if
(
$key
===
[])
{
throw
new
\InvalidArgumentException
(
'Key can not be an empty array.'
);
}
// Can not serialize closures, so prevent their use.
if
(
$this
->
addToDefinitionCallback
instanceof
\Closure
)
{
throw
new
\InvalidArgumentException
(
'Add to definition callback can not be a closure.'
);
}
}
/**
* {@inheritdoc}
*/
public
function
getKey
():
int
|
string
|
array
{
return
$this
->
key
;
}
/**
* {@inheritdoc}
*/
public
function
getValue
():
mixed
{
return
$this
->
value
;
}
/**
* {@inheritdoc}
*/
public
function
isValidPluginClass
(
string
$plugin_class
):
bool
{
if
(
empty
(
$this
->
allowedPluginClasses
))
{
return
TRUE
;
}
foreach
(
$this
->
allowedPluginClasses
as
$allowed_class
)
{
if
(
is_a
(
$plugin_class
,
$allowed_class
,
TRUE
))
{
return
TRUE
;
}
}
return
FALSE
;
}
/**
* {@inheritdoc}
*/
public
function
allowsExistingPropertyOverride
():
bool
{
return
$this
->
allowOverride
;
}
/**
* {@inheritdoc}
*/
public
function
addToDefinition
(
array
|
object
$definition
):
array
|
object
{
if
(
isset
(
$this
->
addToDefinitionCallback
))
{
if
(
is_callable
(
$this
->
addToDefinitionCallback
))
{
return
(
$this
->
addToDefinitionCallback
)(
$this
,
$definition
);
}
throw
new
InvalidPluginDefinitionException
(
$definition
[
'id'
],
'Can not add property to plugin definition because specified callback is invalid.'
);
}
// Plugin property classes that work with plugin attributes that return an
// object from Plugin::get() will need to override this method.
if
(
is_array
(
$definition
))
{
$key
=
is_array
(
$this
->
key
)
?
$this
->
key
:
[
$this
->
key
];
if
(
!
$this
->
allowOverride
&&
NestedArray
::
keyExists
(
$definition
,
$key
))
{
throw
new
InvalidPluginDefinitionException
(
$definition
[
'id'
],
sprintf
(
'Can not override %s plugin definition property at key: [%s].'
,
$definition
[
'class'
],
implode
(
', '
,
$key
)));
}
NestedArray
::
setValue
(
$definition
,
$key
,
$this
->
value
);
}
return
$definition
;
}
}
Loading