You are here

public function EntityFieldCondition::evaluate in Context entity field 8

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/EntityFieldCondition.php, line 125

Class

EntityFieldCondition
Provides an 'Entity Field' condition.

Namespace

Drupal\context_entity_field\Plugin\Condition

Code

public function evaluate() {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $this
    ->getContextValue($this->entityType
    ->id());
  if ($entity && $entity
    ->hasField($this->configuration['field_name'])) {
    $is_empty = $entity
      ->get($this->configuration['field_name'])
      ->isEmpty();

    // Field value is empty.
    if ($this->configuration['field_state'] == 'empty' && $is_empty) {
      return TRUE;
    }

    // Field value is not empty.
    if ($this->configuration['field_state'] == 'filled' && !$is_empty) {
      return TRUE;
    }

    // Field value matches given value.
    if ($this->configuration['field_state'] == 'value' && !$is_empty) {

      // Control value in available values.
      foreach ($entity
        ->get($this->configuration['field_name']) as $item) {
        if ($item
          ->getString() === $this->configuration['field_value']) {
          return TRUE;
        }
      }
    }
  }
  return FALSE;
}