You are here

public function FetchEntityVariableAction::fetchEntityVariable in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/FetchEntityVariableAction.php \Drupal\business_rules\Plugin\BusinessRulesAction\FetchEntityVariableAction::fetchEntityVariable()
1 call to FetchEntityVariableAction::fetchEntityVariable()
FetchEntityVariableAction::execute in src/Plugin/BusinessRulesAction/FetchEntityVariableAction.php
Execute the action.

File

src/Plugin/BusinessRulesAction/FetchEntityVariableAction.php, line 128

Class

FetchEntityVariableAction
Class FetchEntityVariable.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

public function fetchEntityVariable($action, VariablesSet $event_variables) {

  /** @var \Drupal\business_rules\VariableObject $variable */

  /** @var \Drupal\business_rules\Entity\Action $action */
  $id_field = $action
    ->getSettings('field');
  $bundle = $action
    ->getTargetBundle();
  $id = $action
    ->getSettings('value');
  $id = parent::processVariables($id, $event_variables);
  $empty_variable = $action
    ->getSettings('empty_variable');
  $value = NULL;
  if ($event_variables
    ->count()) {
    foreach ($event_variables
      ->getVariables() as $variable) {
      if ($variable
        ->getType() == 'entity_empty_variable' && $empty_variable == substr($variable
        ->getId(), 0, strlen($empty_variable))) {
        $original_variable_value = $variable
          ->getValue();
        if (!stristr($variable
          ->getId(), '->')) {
          $entity = $this
            ->fetchEntity($id, $variable, $id_field, $action, $bundle, $original_variable_value);
          $event_variables
            ->replaceValue($variable
            ->getId(), $entity);
        }
        else {
          if (!$this->entityIsFetched) {
            $entity = $this
              ->fetchEntity($id, $variable, $id_field, $action, $bundle, $original_variable_value);
          }
          if ($entity instanceof EntityInterface) {
            $field = explode('->', $variable
              ->getId())[1];
            if ($entity->{$field}) {
              $already_replaced = [];
              $definition = $entity->{$field}
                ->getFieldDefinition();
              $field_type = $definition
                ->getType();
              $cardinality = $definition
                ->getFieldStorageDefinition()
                ->getCardinality();
              if ($field_type == 'entity_reference') {
                $property_name = 'target_id';
              }
              else {
                $property_name = 'value';
              }
              if ($cardinality === 1) {
                $value = $entity
                  ->get($field)->{$property_name};
              }
              else {
                $arr = $entity->{$field}
                  ->getValue();
                $field_definition = $entity
                  ->getFieldDefinition($field);

                // Check if value is entity reference.
                if ($field_definition
                  ->getType() == 'entity_reference') {
                  $entity_references = $entity
                    ->get($field)
                    ->referencedEntities();
                  foreach ($arr as $key => $item) {
                    $arr[$key] = $item['target_id'];
                    if (substr($variable
                      ->getId(), strlen($variable
                      ->getId()) - strlen('->label'), strlen('->label')) != '->label') {
                      $variable_key_id = substr($variable
                        ->getId(), strlen($variable
                        ->getId()) - strlen("[{$key}]"), strlen("[{$key}]")) == "[{$key}]" ? $variable
                        ->getId() : $variable
                        ->getId() . "[{$key}]";
                      $multi_val = new VariableObject($variable_key_id, $item['target_id'], $variable
                        ->getType());
                      $event_variables
                        ->append($multi_val);

                      // Prepare the variable field with the entity reference
                      // title / name / other property name.

                      /** @var \Drupal\Core\Entity\Entity $entity_reference */
                      $entity_reference = $entity_references[$key];
                      $entity_reference_value = $entity_reference
                        ->label();
                      $variable_key_label_id = substr($variable
                        ->getId(), strlen($variable
                        ->getId()) - strlen("[{$key}]->label"), strlen("[{$key}]->label")) == "[{$key}]->label" ? $variable
                        ->getId() : $variable
                        ->getId() . "[{$key}]->label";
                      $entity_reference_label = new VariableObject($variable_key_label_id, $entity_reference_value, $variable
                        ->getType());
                      $event_variables
                        ->append($entity_reference_label);
                    }

                    // Fetch array labels variable.
                    $arr_labels = [];
                    foreach ($entity_references as $reference) {
                      $arr_labels[] = $reference
                        ->label();
                    }
                    if (count($arr_labels)) {
                      $variable_label_id = substr($variable
                        ->getId(), strlen($variable
                        ->getId()) - strlen('->label'), strlen('->label')) == '->label' ? $variable
                        ->getId() : $variable
                        ->getId() . "->label";
                      $entity_reference_labels = new VariableObject($variable_label_id, $arr_labels, $variable
                        ->getType());
                      $event_variables
                        ->append($entity_reference_labels);
                      $event_variables
                        ->replaceValue($variable_label_id, $arr_labels);
                      $already_replaced[] = $variable_label_id;
                    }
                  }
                }
                else {
                  foreach ($arr as $key => $item) {
                    if (isset($item['value'])) {
                      $arr[$key] = $item['value'];
                      $multi_val = new VariableObject($variable
                        ->getId() . "[{$key}]", $item['value'], $variable
                        ->getType());
                      $event_variables
                        ->append($multi_val);
                    }
                  }
                }
                $value = $arr;
              }
              if (!in_array($variable
                ->getId(), $already_replaced)) {
                $event_variables
                  ->replaceValue($variable
                  ->getId(), $value);
              }
            }
          }
        }
      }
    }
  }
  return $event_variables;
}