You are here

public function SaveEntityVariableAction::execute in Business Rules 8

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

Class

SaveEntityVariableAction
Class SaveEntityVariableAction.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

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

  /** @var \Drupal\business_rules\VariablesSet $variables */

  /** @var \Drupal\Core\Entity\Entity $entity */
  $variables = $event
    ->getArgument('variables');
  $key_value = $this->util
    ->getKeyValueExpirable('save_entity_variable');
  if ($variables
    ->count()) {
    $variable = $variables
      ->getVariable($action
      ->getSettings('variable'));
    $entity = $variable ? $variable
      ->getValue() : FALSE;
    if ($entity instanceof EntityInterface) {

      // Prevent infinite calls regarding the dispatched entity events such as
      // save / presave, etc.
      $uuid = $entity->uuid->value;
      $saved_uuid = $key_value
        ->get($uuid);
      if ($entity->uuid
        ->getValue() !== $saved_uuid) {
        $key_value
          ->set($uuid, $uuid);
        $entity
          ->save();
        $result = [
          '#type' => 'markup',
          '#markup' => t('Entity: %entity on variable: %variable saved.', [
            '%entity' => $entity
              ->getEntityTypeId(),
            '%variable' => $action
              ->getSettings('variable'),
          ]),
        ];
        return $result;
      }
    }
  }
}