Skip to content
Snippets Groups Projects
Commit 82f2e1cc authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3271771: Condition plugins should not rely on the existance of the token named "entity"

parent 39428409
No related merge requests found
......@@ -229,7 +229,7 @@ class EntityFieldValue extends StringComparisonBase {
* The entity, or NULL if not found.
*/
public function getEntity(): ?EntityInterface {
return $this->getContextValue('entity');
return $this->getValueFromContext('entity');
}
}
......@@ -23,7 +23,7 @@ class EntityFieldValueChanged extends ConditionBase {
* {@inheritdoc}
*/
public function evaluate(): bool {
$entity = $this->getContextValue('entity');
$entity = $this->getValueFromContext('entity');
$field_name = $this->tokenServices->replaceClear($this->configuration['field_name']);
if ($entity instanceof FieldableEntityInterface && isset($entity->original) && $entity->hasField($field_name)) {
return $this->negationCheck($entity->get($field_name)->getValue() !== $entity->original->get($field_name)->getValue());
......
......@@ -23,7 +23,7 @@ class EntityFieldValueEmpty extends ConditionBase {
* {@inheritdoc}
*/
public function evaluate(): bool {
$entity = $this->getContextValue('entity');
$entity = $this->getValueFromContext('entity');
$field_name = $this->tokenServices->replaceClear($this->configuration['field_name']);
if ($entity instanceof FieldableEntityInterface && $entity->hasField($field_name)) {
return $this->negationCheck($entity->get($field_name)->isEmpty());
......
......@@ -22,7 +22,7 @@ class EntityIsNew extends ConditionBase {
* {@inheritdoc}
*/
public function evaluate(): bool {
$entity = $this->getContextValue('entity');
$entity = $this->getValueFromContext('entity');
if ($entity instanceof EntityInterface) {
$result = $entity->isNew();
return $this->negationCheck($result);
......
......@@ -27,7 +27,7 @@ class EntityTypeAndBundle extends ConditionBase implements OptionsInterface {
* {@inheritdoc}
*/
public function evaluate(): bool {
$entity = $this->getContextValue('entity');
$entity = $this->getValueFromContext('entity');
if ($entity instanceof EntityInterface) {
$result = $this->bundleFieldApplies($entity, $this->configuration['type']);
return $this->negationCheck($result);
......
......@@ -4,6 +4,7 @@ namespace Drupal\eca\Plugin\ECA\Condition;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\EventDispatcher\Event;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -105,6 +106,24 @@ abstract class ConditionBase extends PluginBase implements ConditionInterface, C
);
}
/**
* Returns the named value from context, if available.
*
* @param $name
* The name of the value that should be returned.
*
* @return mixed|null
* The named value, if available. NULL otherwise.
*/
public function getValueFromContext($name) {
try {
return $this->getContextValue($name);
}
catch (ContextException $e) {
return NULL;
}
}
/**
* {@inheritdoc}
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment