You are here

public function FillEntityVariableFields::execute in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/BusinessRulesAction/FillEntityVariableFields.php \Drupal\business_rules\Plugin\BusinessRulesAction\FillEntityVariableFields::execute()

Execute the action.

Parameters

\Drupal\business_rules\ActionInterface $action: The configured action.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event that has triggered the action.

Return value

array The render array to be showed on debug block.

Overrides BusinessRulesActionPlugin::execute

File

src/Plugin/BusinessRulesAction/FillEntityVariableFields.php, line 286

Class

FillEntityVariableFields
Class FillEntityVariableFields.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

public function execute(ActionInterface $action, BusinessRulesEvent $event) {
  $entity_variable_id = $action
    ->getSettings('variable');
  $event_variables = $event
    ->getArgument('variables');
  $entity_variable = $event_variables
    ->getVariable($entity_variable_id);
  $result = [];
  if ($entity_variable) {

    /** @var \Drupal\Core\Entity\Entity $entity */
    $entity = $entity_variable
      ->getValue();
    $fields_values = $action
      ->getSettings('fields_values');
    if (is_array($fields_values)) {
      foreach ($fields_values as $field_value) {
        $cardinality = $entity
          ->get($field_value['entity_field'])
          ->getFieldDefinition()
          ->getFieldStorageDefinition()
          ->getCardinality();
        if ($cardinality === 1) {

          // Single value field.
          $value = $this
            ->processVariables($field_value['field_value'], $event_variables);
        }
        else {

          // Multiple value field.
          $arr = explode(chr(10) . '|', $field_value['field_value']);
          if (substr($arr[0], 0, 1) == '|') {
            $arr[0] = substr($arr[0], 1, strlen($arr[0]) - 1);
          }
          foreach ($arr as $key => $value) {
            if (substr($value, strlen($value) - 1, 1) == "\r") {
              $arr[$key] = substr($value, 0, strlen($value) - 1);
            }
            $arr[$key . '000000'] = $this
              ->processVariables($arr[$key], $event_variables);
            unset($arr[$key]);
          }

          // Put all values at the array root.
          foreach ($arr as $key => $item) {
            if (is_array($item)) {
              unset($arr[$key]);
              foreach ($item as $new_key => $new_item) {
                $arr[$key + $new_key] = $new_item;
              }
            }
          }
          ksort($arr);

          // Remove empty values.
          if (is_array($arr) && count($arr)) {
            foreach ($arr as $key => $item) {
              if (empty($item) || is_null($item) || is_string($item) && strlen(trim($item)) == 0) {
                $arr[$key] = NULL;
              }
            }
          }
          $value = $arr;
        }
        if (empty($value) || is_null($value) || is_string($value) && strlen(trim($value)) === 0) {
          $value = NULL;
        }
        $entity
          ->set($field_value['entity_field'], $value);
        $result[$field_value['entity_field']] = [
          '#type' => 'markup',
          '#markup' => t('Entity variable: %variable field: %field filled with value: %value.', [
            '%variable' => $entity_variable_id,
            '%field' => $field_value['entity_field'],
            '%value' => is_array($value) ? implode(',', $value) : $value,
          ]),
        ];
      }
    }
  }
  return $result;
}