You are here

public function LoopThroughFieldVariable::execute in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/LoopThroughFieldVariable.php \Drupal\business_rules\Plugin\BusinessRulesAction\LoopThroughFieldVariable::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/LoopThroughFieldVariable.php, line 322

Class

LoopThroughFieldVariable
Class LoopThroughFieldVariable.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

public function execute(ActionInterface $action, BusinessRulesEvent $event) {

  /** @var \Drupal\business_rules\VariablesSet $event_variables */
  $event_variables = $event
    ->getArgument('variables');
  $field_variable = $event_variables
    ->getVariable($action
    ->getSettings('variable'));
  $action_items = $action
    ->getSettings('items');

  // Execute action items.
  foreach ($field_variable
    ->getValue() as $key => $value) {

    // Add current value variable.
    $varObj = new VariableObject($field_variable
      ->getId() . '->current', $value, $field_variable
      ->getType());
    $event_variables
      ->append($varObj);

    // Add the current id for entity reference fields.
    if ($event_variables
      ->getVariable($field_variable
      ->getId() . "[{$key}]")) {
      $current_id = $event_variables
        ->getVariable($field_variable
        ->getId() . "[{$key}]")
        ->getValue();
      $varObj = new VariableObject($field_variable
        ->getId() . '->current->id', $current_id, $field_variable
        ->getType());
      $event_variables
        ->append($varObj);
    }

    // Add the current label for entity reference fields.
    if ($event_variables
      ->getVariable($field_variable
      ->getId() . "[{$key}]->label")) {
      $current_label = $event_variables
        ->getVariable($field_variable
        ->getId() . "[{$key}]->label")
        ->getValue();
      $varObj = new VariableObject($field_variable
        ->getId() . '->current->label', $current_label, $field_variable
        ->getType());
      $event_variables
        ->append($varObj);
    }

    // Process items.
    $items = BusinessRulesItemObject::itemsArrayToItemsObject($action_items);
    $this->processor
      ->processItems($items, $event, $action
      ->id());
  }
}