You are here

public function EntityEmptyVariable::evaluate in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/BusinessRulesVariable/EntityEmptyVariable.php \Drupal\business_rules\Plugin\BusinessRulesVariable\EntityEmptyVariable::evaluate()

Evaluate the variable.

Parameters

\Drupal\business_rules\Entity\Variable $variable: The variable to be evaluated.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The dispatched event.

Return value

\Drupal\business_rules\VariableObject|\Drupal\business_rules\VariablesSet The evaluated variables.

Overrides BusinessRulesVariablePlugin::evaluate

File

src/Plugin/BusinessRulesVariable/EntityEmptyVariable.php, line 103

Class

EntityEmptyVariable
Class EntityEmptyVariable.

Namespace

Drupal\business_rules\Plugin\BusinessRulesVariable

Code

public function evaluate(Variable $variable, BusinessRulesEvent $event) {

  /** @var \Drupal\Core\Entity\Entity $entity */
  $entity_type = $variable
    ->getTargetEntityType();
  $bundle = $variable
    ->getTargetBundle();

  // Node has entity key = 'type', comment has another entity key.
  // Needs to figure out the best way to get the entity key.
  // TODO review this logic in order to get entity in all situations.
  if ($entity_type == 'node') {
    $entity_key = 'type';
  }
  else {

    // Get entity bundle key.
    $manager = \Drupal::entityTypeManager();
    $entity_type1 = $manager
      ->getDefinition($entity_type);
    $entity_key = $entity_type1
      ->get('entity_keys')['bundle'];
  }
  $entity = \Drupal::entityTypeManager()
    ->getStorage($entity_type)
    ->create([
    $entity_key => $bundle,
  ]);

  // ->create(['type' => $bundle]);.
  $variableObject = new VariableObject($variable
    ->id(), $entity, $variable
    ->getType());
  $variableSet = new VariablesSet();
  $variableSet
    ->append($variableObject);
  $fields = $this->entityFieldManager
    ->getFieldDefinitions($variable
    ->getTargetEntityType(), $variable
    ->getTargetBundle());
  foreach ($fields as $field_name => $field_storage) {
    $variableObject = new VariableObject($variable
      ->id() . '->' . $field_name, $entity
      ->get($field_name)->value, $variable
      ->getType());
    $variableSet
      ->append($variableObject);
  }
  return $variableSet;
}