public function Term::evaluate in Term condition 8
Same name and namespace in other branches
- 2.0.x src/Plugin/Condition/Term.php \Drupal\term_condition\Plugin\Condition\Term::evaluate()
Evaluates the condition and returns TRUE or FALSE accordingly.
Return value
bool TRUE if the condition has been met, FALSE otherwise.
Overrides ConditionInterface::evaluate
File
- src/
Plugin/ Condition/ Term.php, line 128 - Contains \Drupal\term_condition\Plugin\Condition\Term.
Class
- Term
- Provides a 'Term' condition to enable a condition based in module selected status.
Namespace
Drupal\term_condition\Plugin\ConditionCode
public function evaluate() {
if (empty($this->configuration['tid']) && !$this
->isNegated()) {
return TRUE;
}
// If configuration['tid'] is an array, there is multiple terms set.
if (is_array($this->configuration['tid'])) {
$tids = $this->configuration['tid'];
unset($this->configuration['tid']);
foreach ($tids as $tid) {
$this->configuration['tid'][] = array_pop($tid);
}
}
$entity = $this
->getContextValue('node');
// Not in a node context. Try a few other options.
if (!$entity) {
// Potential other ways to try fetch the entity. Assoc array to try get revisions.
// I wonder if there is a cleaner way to do this?
// TODO - Provide hook to add extras.
$potentialRouteMatches = [
'taxonomy_term' => 'taxonomy_term',
'node' => 'node_revision',
];
foreach ($potentialRouteMatches as $key => $potentialRouteMatch) {
$entity = \Drupal::routeMatch()
->getParameter($potentialRouteMatch);
// If the entity extends EntityInterface, we have the entity we want.
if ($entity instanceof EntityInterface) {
break;
}
elseif (is_string($entity)) {
// If the entity is a string, its likely the revision ID,
// try load that.
$entity = $this->entityTypeManager
->getStorage($key)
->loadRevision($entity);
break;
}
}
// All checks failed. Stop.
if (!$entity) {
return FALSE;
}
}
foreach ($entity
->referencedEntities() as $referenced_entity) {
// If configuration['tid'] is an array with multiple terms, check all
// tids in the array against the term.
if (is_array($this->configuration['tid'])) {
if ($referenced_entity
->getEntityTypeId() == 'taxonomy_term' && in_array($referenced_entity
->id(), $this->configuration['tid'])) {
return TRUE;
}
}
else {
if ($referenced_entity
->getEntityTypeId() == 'taxonomy_term' && $referenced_entity
->id() == $this->configuration['tid']) {
return TRUE;
}
}
}
return FALSE;
}