You are here

public function NodeField::evaluate in Entity Field Condition 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/NodeField.php, line 275

Class

NodeField
Provides a 'Node Field' condition.

Namespace

Drupal\entity_field_condition\Plugin\Condition

Code

public function evaluate() {
  if (empty($this->configuration['field']) && !$this
    ->isNegated()) {
    return TRUE;
  }
  $entity_type_id = $this->configuration['entity_type_id'];
  $entity_bundle = $this->configuration['entity_bundle'];
  $field = $this->configuration['field'];

  /** @var \Drupal\node\Entity\Node $entity */
  $entity = $this
    ->getContextValue($entity_type_id);
  if (is_subclass_of($entity, 'Drupal\\Core\\Entity\\ContentEntityBase') && $entity
    ->getEntityTypeId() === $entity_type_id && $entity
    ->getType() === $entity_bundle) {
    $value = $entity
      ->get($field)
      ->getValue();
    $value_to_compare = NULL;

    // Structured data.
    if (is_array($value)) {
      if (!empty($value)) {

        // Loop through each value and compare.
        foreach ($value as $value_item) {

          // Check for target_id to support references.
          if (isset($value_item['target_id'])) {
            $value_to_compare = $value_item['target_id'];
          }
          elseif (isset($value_item['uri'])) {
            $value_to_compare = $value_item['uri'];
          }
          else {
            $value_to_compare = $value_item['value'];
          }

          // Return comparison only if true.
          if ($value_to_compare === $this->configuration['value']) {
            return TRUE;
          }
        }
      }
    }
    else {
      $value_to_compare = $value;
    }

    // Compare if null.
    if ($this->configuration['value_source'] === 'null') {
      return is_null($value_to_compare);
    }

    // Regular comparison.
    return $value_to_compare === $this->configuration['value'];
  }
  return FALSE;
}