You are here

class FetchEntityVariableAction in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/BusinessRulesAction/FetchEntityVariableAction.php \Drupal\business_rules\Plugin\BusinessRulesAction\FetchEntityVariableAction

Class FetchEntityVariable.

@package Drupal\business_rules\Plugin\BusinessRulesAction

Plugin annotation


@BusinessRulesAction(
  id = "fetch_entity_variable",
  label = @Translation("Fetch entity variable by id"),
  group = @Translation("Variable"),
  description = @Translation("Fetch an entity variable by id provided by
  another variable or a constant value."), isContextDependent = FALSE,
  hasTargetEntity = TRUE, hasTargetBundle = TRUE, hasTargetField = TRUE,
)

Hierarchy

Expanded class hierarchy of FetchEntityVariableAction

File

src/Plugin/BusinessRulesAction/FetchEntityVariableAction.php, line 31

Namespace

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

  /**
   * If the entity is fetched.
   *
   * @var bool
   */
  private $entityIsFetched = FALSE;

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings = [];
    if (!$item
      ->isNew()) {
      $settings['empty_variable'] = [
        '#type' => 'select',
        '#title' => t('Empty variable'),
        '#required' => TRUE,
        '#description' => t('Empty variable to be filled in.'),
        '#options' => $this
          ->getAvailableEmptyVariables($item),
        '#default_value' => empty($item
          ->getSettings('empty_variable')) ? '' : $item
          ->getSettings('empty_variable'),
      ];
      $settings['value'] = [
        '#type' => 'textfield',
        '#title' => t('Value'),
        '#default_value' => $item
          ->getSettings('value'),
        '#description' => t('The entity ID value to fill the 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 entities variables.
   */
  public 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() . ']';
      }
    }
    asort($output);
    return $output;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array &$form, FormStateInterface $form_state) {
    $form['settings']['field']['#description'] = t('Select the entity id field.');
    $form['settings']['field']['#title'] = t('Entity id field.');
  }

  /**
   * {@inheritdoc}
   */
  public function execute(ActionInterface $action, BusinessRulesEvent $event) {

    /** @var \Drupal\business_rules\VariablesSet $variables */
    $id_variable = $action
      ->getSettings('empty_variable');
    $variables = $event
      ->getArgument('variables');
    $processed_variables = $this
      ->fetchEntityVariable($action, $variables);
    $event
      ->setArgument('variables', $processed_variables);
    $result = [
      '#type' => 'markup',
      '#markup' => t('Entity variable %variable fetched.', [
        '%variable' => $id_variable,
      ]),
    ];
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function fetchEntityVariable($action, VariablesSet $event_variables) {

    /** @var \Drupal\business_rules\VariableObject $variable */

    /** @var \Drupal\business_rules\Entity\Action $action */
    $id_field = $action
      ->getSettings('field');
    $bundle = $action
      ->getTargetBundle();
    $id = $action
      ->getSettings('value');
    $id = parent::processVariables($id, $event_variables);
    $empty_variable = $action
      ->getSettings('empty_variable');
    $value = NULL;
    if ($event_variables
      ->count()) {
      foreach ($event_variables
        ->getVariables() as $variable) {
        if ($variable
          ->getType() == 'entity_empty_variable' && $empty_variable == substr($variable
          ->getId(), 0, strlen($empty_variable))) {
          $original_variable_value = $variable
            ->getValue();
          if (!stristr($variable
            ->getId(), '->')) {
            $entity = $this
              ->fetchEntity($id, $variable, $id_field, $action, $bundle, $original_variable_value);
            $event_variables
              ->replaceValue($variable
              ->getId(), $entity);
          }
          else {
            if (!$this->entityIsFetched) {
              $entity = $this
                ->fetchEntity($id, $variable, $id_field, $action, $bundle, $original_variable_value);
            }
            if ($entity instanceof EntityInterface) {
              $field = explode('->', $variable
                ->getId())[1];
              if ($entity->{$field}) {
                $already_replaced = [];
                $definition = $entity->{$field}
                  ->getFieldDefinition();
                $field_type = $definition
                  ->getType();
                $cardinality = $definition
                  ->getFieldStorageDefinition()
                  ->getCardinality();
                if ($field_type == 'entity_reference') {
                  $property_name = 'target_id';
                }
                else {
                  $property_name = 'value';
                }
                if ($cardinality === 1) {
                  $value = $entity
                    ->get($field)->{$property_name};
                }
                else {
                  $arr = $entity->{$field}
                    ->getValue();
                  $field_definition = $entity
                    ->getFieldDefinition($field);

                  // Check if value is entity reference.
                  if ($field_definition
                    ->getType() == 'entity_reference') {
                    $entity_references = $entity
                      ->get($field)
                      ->referencedEntities();
                    foreach ($arr as $key => $item) {
                      $arr[$key] = $item['target_id'];
                      if (substr($variable
                        ->getId(), strlen($variable
                        ->getId()) - strlen('->label'), strlen('->label')) != '->label') {
                        $variable_key_id = substr($variable
                          ->getId(), strlen($variable
                          ->getId()) - strlen("[{$key}]"), strlen("[{$key}]")) == "[{$key}]" ? $variable
                          ->getId() : $variable
                          ->getId() . "[{$key}]";
                        $multi_val = new VariableObject($variable_key_id, $item['target_id'], $variable
                          ->getType());
                        $event_variables
                          ->append($multi_val);

                        // Prepare the variable field with the entity reference
                        // title / name / other property name.

                        /** @var \Drupal\Core\Entity\Entity $entity_reference */
                        $entity_reference = $entity_references[$key];
                        $entity_reference_value = $entity_reference
                          ->label();
                        $variable_key_label_id = substr($variable
                          ->getId(), strlen($variable
                          ->getId()) - strlen("[{$key}]->label"), strlen("[{$key}]->label")) == "[{$key}]->label" ? $variable
                          ->getId() : $variable
                          ->getId() . "[{$key}]->label";
                        $entity_reference_label = new VariableObject($variable_key_label_id, $entity_reference_value, $variable
                          ->getType());
                        $event_variables
                          ->append($entity_reference_label);
                      }

                      // Fetch array labels variable.
                      $arr_labels = [];
                      foreach ($entity_references as $reference) {
                        $arr_labels[] = $reference
                          ->label();
                      }
                      if (count($arr_labels)) {
                        $variable_label_id = substr($variable
                          ->getId(), strlen($variable
                          ->getId()) - strlen('->label'), strlen('->label')) == '->label' ? $variable
                          ->getId() : $variable
                          ->getId() . "->label";
                        $entity_reference_labels = new VariableObject($variable_label_id, $arr_labels, $variable
                          ->getType());
                        $event_variables
                          ->append($entity_reference_labels);
                        $event_variables
                          ->replaceValue($variable_label_id, $arr_labels);
                        $already_replaced[] = $variable_label_id;
                      }
                    }
                  }
                  else {
                    foreach ($arr as $key => $item) {
                      if (isset($item['value'])) {
                        $arr[$key] = $item['value'];
                        $multi_val = new VariableObject($variable
                          ->getId() . "[{$key}]", $item['value'], $variable
                          ->getType());
                        $event_variables
                          ->append($multi_val);
                      }
                    }
                  }
                  $value = $arr;
                }
                if (!in_array($variable
                  ->getId(), $already_replaced)) {
                  $event_variables
                    ->replaceValue($variable
                    ->getId(), $value);
                }
              }
            }
          }
        }
      }
    }
    return $event_variables;
  }

  /**
   * Fetch the entity.
   *
   * @param string $id
   *   The entity id.
   * @param \Drupal\business_rules\VariableObject $variable
   *   The VariableObject.
   * @param string $id_field
   *   The field id.
   * @param \Drupal\business_rules\Entity\Action $action
   *   The Business rule Action action.
   * @param string $bundle
   *   The bundle.
   * @param mixed $original_variable_value
   *   The original variable value.
   *
   * @return \Drupal\Core\Entity\EntityInterface|null
   *   The entity.
   */
  private function fetchEntity($id, VariableObject $variable, $id_field, Action $action, $bundle, $original_variable_value) {
    try {
      $var = Variable::load($variable
        ->getId());
      if ($var) {
        $entity_type = $var
          ->getTargetEntityType();
        $entity = \Drupal::entityTypeManager()
          ->getStorage($entity_type)
          ->load($id);
        if (is_object($entity)) {
          $new_entity = clone $entity;
          $this->entityIsFetched = TRUE;
          return $new_entity;
        }
        else {

          // drupal_set_message(t("Action: %action fail. It's not possible to fetch entity %entity, bundle %bundle, with id=%id", [
          //   '%action' => $action->label() . ' [' . $action->id() . ']',
          //   '%entity' => $entity_type,
          //   '%bundle' => $bundle,
          //   '%id'     => $id,
          // ]), 'error');
          return $original_variable_value;
        }
      }
      else {

        // drupal_set_message(t("Action: %action fail. Variable: %variable could not be loaded.", [
        //   '%action'   => $action->label() . ' [' . $action->id() . ']',
        //   '%variable' => $variable->getId(),
        // ]), 'error');
        return $original_variable_value;
      }
    } catch (\Exception $e) {
      drupal_set_message($e, 'error');
      return $original_variable_value;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getVariables(ItemInterface $item) {
    $variableSet = parent::getVariables($item);
    $empty_variable = new VariableObject($item
      ->getSettings('empty_variable'));
    $variableSet
      ->append($empty_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
FetchEntityVariableAction::$entityIsFetched private property If the entity is fetched.
FetchEntityVariableAction::buildForm public function Form constructor. Overrides BusinessRulesItemPluginBase::buildForm
FetchEntityVariableAction::execute public function Execute the action. Overrides BusinessRulesActionPlugin::execute
FetchEntityVariableAction::fetchEntity private function Fetch the entity.
FetchEntityVariableAction::fetchEntityVariable public function
FetchEntityVariableAction::getAvailableEmptyVariables public function Get the available empty variables for the context.
FetchEntityVariableAction::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
FetchEntityVariableAction::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginBase::getVariables
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.