Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
eca
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
eca
Merge requests
!349
Issue
#3352393
: New condition: Contains for lists
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Issue
#3352393
: New condition: Contains for lists
issue/eca-3352393:3352393-list-contains
into
1.2.x
Overview
5
Commits
66
Pipelines
1
Changes
3
Merged
mxh
requested to merge
issue/eca-3352393:3352393-list-contains
into
1.2.x
2 years ago
Overview
5
Commits
66
Pipelines
1
Changes
3
Expand
0
0
Merge request reports
Compare
1.2.x
version 18
b1553918
2 years ago
version 17
b1553918
2 years ago
version 16
9028d01d
2 years ago
version 15
9ecb0ffa
2 years ago
version 14
dd142b55
2 years ago
version 13
e012145f
2 years ago
version 12
591e3066
2 years ago
version 11
22c032a3
2 years ago
version 10
3d72a6c1
2 years ago
version 9
3f008ef6
2 years ago
version 8
69692330
2 years ago
version 7
abb84420
2 years ago
version 6
17641e08
2 years ago
version 5
6121ec5f
2 years ago
version 4
7cc884b9
2 years ago
version 3
8c16f832
2 years ago
version 2
b28038cd
2 years ago
version 1
e7d82422
2 years ago
1.2.x (base)
and
latest version
latest version
b1553918
66 commits,
2 years ago
version 18
b1553918
66 commits,
2 years ago
version 17
b1553918
66 commits,
2 years ago
version 16
9028d01d
65 commits,
2 years ago
version 15
9ecb0ffa
64 commits,
2 years ago
version 14
dd142b55
63 commits,
2 years ago
version 13
e012145f
62 commits,
2 years ago
version 12
591e3066
61 commits,
2 years ago
version 11
22c032a3
60 commits,
2 years ago
version 10
3d72a6c1
59 commits,
2 years ago
version 9
3f008ef6
58 commits,
2 years ago
version 8
69692330
57 commits,
2 years ago
version 7
abb84420
56 commits,
2 years ago
version 6
17641e08
55 commits,
2 years ago
version 5
6121ec5f
5 commits,
2 years ago
version 4
7cc884b9
4 commits,
2 years ago
version 3
8c16f832
3 commits,
2 years ago
version 2
b28038cd
2 commits,
2 years ago
version 1
e7d82422
1 commit,
2 years ago
3 files
+
394
−
1
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
modules/base/src/Plugin/ECA/Condition/ListContains.php
0 → 100644
+
180
−
0
Options
<?php
namespace
Drupal\eca_base\Plugin\ECA\Condition
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Field\EntityReferenceFieldItemListInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\TypedData\ComplexDataInterface
;
use
Drupal\Core\TypedData\Exception\MissingDataException
;
use
Drupal\Core\TypedData\ListInterface
;
use
Drupal\Core\TypedData\TraversableTypedDataInterface
;
use
Drupal\Core\TypedData\TypedDataInterface
;
use
Drupal\eca\Plugin\DataType\DataTransferObject
;
use
Drupal\eca\Plugin\ECA\Condition\ConditionBase
;
/**
* ECA condition plugin for evaluating whether a certain item is in a list.
*
* @EcaCondition(
* id = "eca_list_contains",
* label = @Translation("List: contains item"),
* description = @Translation("Evaluate whether a certain item is in a list.")
* )
*/
class
ListContains
extends
ConditionBase
{
/**
* {@inheritdoc}
*/
public
function
defaultConfiguration
():
array
{
return
[
'list_token'
=>
''
,
'method'
=>
'value'
,
'value'
=>
''
,
]
+
parent
::
defaultConfiguration
();
}
/**
* {@inheritdoc}
*/
public
function
buildConfigurationForm
(
array
$form
,
FormStateInterface
$form_state
):
array
{
$form
=
parent
::
buildConfigurationForm
(
$form
,
$form_state
);
$form
[
'list_token'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Name of token containing the list'
),
'#description'
=>
$this
->
t
(
'Provide the name of the token that contains a list of items.'
),
'#default_value'
=>
$this
->
configuration
[
'list_token'
],
'#weight'
=>
-
200
,
];
$form
[
'method'
]
=
[
'#type'
=>
'select'
,
'#title'
=>
$this
->
t
(
'Method'
),
'#default_value'
=>
$this
->
configuration
[
'method'
],
'#weight'
=>
-
190
,
'#options'
=>
[
'index'
=>
$this
->
t
(
'Lookup index key'
),
'value'
=>
$this
->
t
(
'Lookup list value'
),
],
'#required'
=>
TRUE
,
];
$form
[
'value'
]
=
[
'#type'
=>
'textarea'
,
'#title'
=>
$this
->
t
(
'Value to lookup'
),
'#default_value'
=>
$this
->
configuration
[
'value'
],
'#weight'
=>
-
180
,
];
return
$form
;
}
/**
* {@inheritdoc}
*/
public
function
submitConfigurationForm
(
array
&
$form
,
FormStateInterface
$form_state
):
void
{
parent
::
submitConfigurationForm
(
$form
,
$form_state
);
$this
->
configuration
[
'list_token'
]
=
$form_state
->
getValue
(
'list_token'
);
$this
->
configuration
[
'method'
]
=
$form_state
->
getValue
(
'method'
);
$this
->
configuration
[
'value'
]
=
$form_state
->
getValue
(
'value'
);
}
/**
* {@inheritdoc}
*/
public
function
evaluate
():
bool
{
$list
=
$this
->
getItemList
();
$result
=
FALSE
;
switch
(
$this
->
configuration
[
'method'
])
{
case
'index'
:
$index
=
trim
((
string
)
$this
->
tokenServices
->
replaceClear
(
$this
->
configuration
[
'value'
]));
if
(
!
$index
||
!
(
$list
instanceof
ComplexDataInterface
)
||
ctype_digit
(
$index
))
{
$index
=
(
int
)
$index
;
}
if
(
$list
instanceof
DataTransferObject
)
{
try
{
$properties
=
$list
->
getProperties
(
TRUE
)
??
[];
$result
=
isset
(
$properties
[
$index
]);
}
catch
(
MissingDataException
)
{
// Can be ignored.
}
}
elseif
(
$values
=
(
$list
->
getValue
()
??
[]))
{
$result
=
isset
(
$values
[
$index
]);
}
break
;
case
'value'
:
$value
=
$this
->
tokenServices
->
getOrReplace
(
$this
->
configuration
[
'value'
]);
if
(
$value
instanceof
TypedDataInterface
)
{
$value
=
$value
->
getValue
();
}
if
(
$list
instanceof
DataTransferObject
)
{
try
{
$result
=
(
NULL
!==
(
clone
$list
)
->
remove
(
$value
))
||
$this
->
containsTrimmedValue
(
$value
,
$list
->
toArray
());
}
catch
(
MissingDataException
)
{
// Can be ignored.
}
}
elseif
((
$value
instanceof
EntityInterface
)
&&
(
$list
instanceof
EntityReferenceFieldItemListInterface
))
{
if
(
$entities
=
$list
->
referencedEntities
())
{
$result
=
(
NULL
!==
DataTransferObject
::
create
(
$entities
)
->
remove
(
$value
));
}
}
elseif
(
$values
=
$list
->
getValue
())
{
$result
=
(
in_array
(
$value
,
$values
,
TRUE
))
||
$this
->
containsTrimmedValue
(
$value
,
$values
);
}
break
;
}
return
$this
->
negationCheck
(
$result
);
}
/**
* Checks whether the trimmed form of the value is contained in the values.
*
* @param mixed $value
* The value to lookup.
* @param mixed $values
* The values to check.
*
* @return bool
* Returns TRUE if it contains the trimmed value, FALSE otherwise.
*/
protected
function
containsTrimmedValue
(
mixed
$value
,
mixed
$values
):
bool
{
if
(
!
is_scalar
(
$value
)
||
!
is_array
(
$values
)
||
empty
(
$values
))
{
return
FALSE
;
}
$value
=
trim
((
string
)
$value
);
$result
=
FALSE
;
array_walk_recursive
(
$values
,
static
function
(
$list_value
)
use
(
&
$value
,
&
$result
)
{
if
(
is_scalar
(
$list_value
)
&&
(
trim
((
string
)
$list_value
)
===
$value
))
{
$result
=
TRUE
;
}
});
return
$result
;
}
/**
* Get the item list.
*
* @return \Drupal\Core\TypedData\TraversableTypedDataInterface|null
* The item list, or NULL if the targeted token is not a list.
*/
protected
function
getItemList
():
?TraversableTypedDataInterface
{
$list_token
=
trim
((
string
)
$this
->
configuration
[
'list_token'
]);
$token
=
$this
->
tokenServices
;
if
(
!
$token
->
hasTokenData
(
$list_token
))
{
return
NULL
;
}
$list
=
$token
->
getTokenData
(
$list_token
);
return
(
$list
instanceof
ListInterface
||
$list
instanceof
DataTransferObject
)
?
$list
:
NULL
;
}
}
Loading