function RulesComponentAction::execute

Overrides RulesActionBase::execute

File

src/Plugin/RulesAction/RulesComponentAction.php, line 76

Class

RulesComponentAction
Provides a generic 'Execute Rules component' action.

Namespace

Drupal\rules\Plugin\RulesAction

Code

public function execute() {
  $rules_config = $this->storage
    ->load($this->componentId);
  // Setup an isolated execution state for this expression and pass on the
  // necessary context.
  $rules_component = $rules_config->getComponent();
  foreach ($this->getContexts() as $context_name => $context) {
    // Pass through the already existing typed data objects to avoid creating
    // them from scratch.
    $rules_component->getState()
      ->setVariableData($context_name, $context->getContextData());
  }
  // We don't use RulesComponent::execute() here since we don't want to
  // auto-save immediately.
  $state = $rules_component->getState();
  $expression = $rules_component->getExpression();
  $expression->executeWithState($state);
  // Postpone auto-saving to the parent expression triggering this action.
  foreach ($state->getAutoSaveSelectors() as $selector) {
    $parts = explode('.', $selector);
    $context_name = reset($parts);
    if (array_key_exists($context_name, $this->context)) {
      $this->saveLater[] = $context_name;
    }
    else {
      // Otherwise we need to save here since it will not happen in the parent
      // execution.
      $typed_data = $state->fetchDataByPropertyPath($selector);
      // Things that can be saved must have a save() method, right?
      // Saving is always done at the root of the typed data tree, for example
      // on the entity level.
      $typed_data->getRoot()
        ->getValue()
        ->save();
    }
  }
  foreach ($this->getProvidedContextDefinitions() as $name => $definition) {
    $this->setProvidedValue($name, $state->getVariable($name));
  }
}