You are here

class SaveEntityVariableAction 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

Class SaveEntityVariableAction.

@package Drupal\business_rules\Plugin\BusinessRulesAction

Plugin annotation


@BusinessRulesAction(
  id = "save_entity_variable",
  label = @Translation("Save entity variable"),
  group = @Translation("Variable"),
  description = @Translation("Save a entity stored in a variable."),
  isContextDependent = FALSE,
  hasTargetEntity = TRUE,
  hasTargetBundle = TRUE,
  hasTargetField = FALSE,
)

Hierarchy

Expanded class hierarchy of SaveEntityVariableAction

File

src/Plugin/BusinessRulesAction/SaveEntityVariableAction.php, line 32

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction
View source
class SaveEntityVariableAction extends BusinessRulesActionPlugin {

  /**
   * Delete all expirable key value pairs.
   */
  public function __destruct() {
    $key_value = $this->util
      ->getKeyValueExpirable('save_entity_variable');
    $key_value
      ->deleteAll();
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings = [];
    if (!$item
      ->isNew()) {
      $settings['variable'] = [
        '#type' => 'select',
        '#title' => t('Entity variable'),
        '#required' => TRUE,
        '#description' => t('Entity variable to be saved. Remember to create actions to fill the entity variable fields and execute them before save the entity.'),
        '#options' => $this
          ->getAvailableEmptyVariables($item),
        '#default_value' => empty($item
          ->getSettings('variable')) ? '' : $item
          ->getSettings('variable'),
      ];
    }
    return $settings;
  }

  /**
   * Get the available empty variables for the context.
   *
   * @param \Drupal\business_rules\Entity\Action $item
   *   The action.
   *
   * @return array
   *   Array of available variables.
   */
  protected function getAvailableEmptyVariables(Action $item) {
    $variables = Variable::loadMultiple();
    $output = [];

    /** @var \Drupal\business_rules\Entity\Variable $variable */
    foreach ($variables as $variable) {
      if ($item
        ->getTargetEntityType() == $variable
        ->getTargetEntityType() && $item
        ->getTargetBundle() == $variable
        ->getTargetBundle() && $variable
        ->getType() == 'entity_empty_variable') {
        $output[$variable
          ->id()] = $variable
          ->label() . ' [' . $variable
          ->id() . ']';
      }
    }
    return $output;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array &$form, FormStateInterface $form_state) {
    unset($form['variables']);
  }

  /**
   * {@inheritdoc}
   */
  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;
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getVariables(ItemInterface $item) {
    $variableSet = parent::getVariables($item);
    $variable = new VariableObject($item
      ->getSettings('variable'));
    $variableSet
      ->append($variable);
    return $variableSet;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
BusinessRulesItemPluginBase::getDescription public function Provide a description of the item. Overrides BusinessRulesItemPluginInterface::getDescription
BusinessRulesItemPluginBase::getEditUrl public function Get the redirect url for the item edit-form route. Overrides BusinessRulesItemPluginInterface::getEditUrl
BusinessRulesItemPluginBase::getGroup public function Provide the group of the item. Overrides BusinessRulesItemPluginInterface::getGroup
BusinessRulesItemPluginBase::getRedirectUrl public function Get the redirect url for the item collection route. Overrides BusinessRulesItemPluginInterface::getRedirectUrl
BusinessRulesItemPluginBase::pregMatch public function Extract the variables from the plugin settings. Overrides BusinessRulesItemPluginInterface::pregMatch
BusinessRulesItemPluginBase::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginInterface::processSettings 19
BusinessRulesItemPluginBase::processTokenArraySetting private function Helper function to process tokens if the setting is an array.
BusinessRulesItemPluginBase::processTokens public function Process the tokens on the settings property for the item. Overrides BusinessRulesItemPluginInterface::processTokens
BusinessRulesItemPluginBase::processVariables public function Process the item replacing the variables by it's values. Overrides BusinessRulesItemPluginInterface::processVariables 1
BusinessRulesItemPluginBase::validateForm public function Plugin form validator. Overrides BusinessRulesItemPluginInterface::validateForm 11
BusinessRulesItemPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 11
BusinessRulesItemPluginInterface::VARIABLE_REGEX constant
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SaveEntityVariableAction::buildForm public function Form constructor. Overrides BusinessRulesItemPluginBase::buildForm
SaveEntityVariableAction::execute public function Execute the action. Overrides BusinessRulesActionPlugin::execute
SaveEntityVariableAction::getAvailableEmptyVariables protected function Get the available empty variables for the context.
SaveEntityVariableAction::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
SaveEntityVariableAction::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginBase::getVariables
SaveEntityVariableAction::__destruct public function Delete all expirable key value pairs.