You are here

class CustomValueVariable in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesVariable/CustomValueVariable.php \Drupal\business_rules\Plugin\BusinessRulesVariable\CustomValueVariable

Class ConstantVariable.

@package Drupal\business_rules\Plugin\BusinessRulesVariable

Plugin annotation


@BusinessRulesVariable(
  id = "custom_value_variable",
  label = @Translation("Custom value"),
  group = @Translation("Variable"),
  description = @Translation("Set an variable with a constant value."),
  reactsOnIds = {},
  isContextDependent = FALSE,
  hasTargetEntity = FALSE,
  hasTargetBundle = FALSE,
  hasTargetField = FALSE,
)

Hierarchy

Expanded class hierarchy of CustomValueVariable

File

src/Plugin/BusinessRulesVariable/CustomValueVariable.php, line 32

Namespace

Drupal\business_rules\Plugin\BusinessRulesVariable
View source
class CustomValueVariable extends BusinessRulesVariablePlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['value'] = [
      '#type' => 'textarea',
      '#title' => t('Custom value'),
      '#description' => t("The initial value for this variable. You can change this value through actions."),
      '#required' => FALSE,
      '#default_value' => $item
        ->getSettings('value'),
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate(Variable $variable, BusinessRulesEvent $event) {
    $custom_value = $variable
      ->getSettings('value');
    $variables = $event
      ->getArgument('variables');

    // Search for another's variables inside the original value.
    preg_match_all(BusinessRulesItemPluginInterface::VARIABLE_REGEX, $custom_value, $inside_variables);
    $varObjects = [];
    if (count($inside_variables)) {
      $inside_variables = $inside_variables[1];
      if (count($inside_variables)) {
        foreach ($inside_variables as $inside_variable) {
          $var = Variable::load($inside_variable);
          if ($var instanceof Variable) {
            $varObjects[$var
              ->id()] = $variables
              ->getVariables()[$var
              ->id()];
          }
          elseif (in_array($inside_variable, array_keys($variables
            ->getVariables()))) {
            $varObjects[$inside_variable] = new VariableObject($inside_variable, $variables
              ->getVariables()[$inside_variable]
              ->getValue(), $variables
              ->getVariables()[$inside_variable]
              ->getType());
          }
          elseif (stristr($inside_variable, '->')) {
            $arr_temp = explode('->', $inside_variable);
            $var_name = $arr_temp[0];
            $field_name = $arr_temp[1];
            $var = Variable::load($var_name);
            if ($var instanceof Variable) {
              $entity = $variables
                ->getVariables()[$var
                ->id()]
                ->getValue();
              if ($entity instanceof EntityInterface) {
                $field = $entity
                  ->get($field_name);
                $value = $field->value;
                $varObjects[$inside_variable] = new VariableObject($var_name, $value, $var
                  ->getType());
              }
            }
          }
        }
      }
    }

    // Replace the variables tokens for the variable value.
    if (count($varObjects)) {
      foreach ($varObjects as $key => $var) {
        if (is_string($var
          ->getValue()) || is_numeric($var
          ->getValue())) {
          $custom_value = str_replace('{{' . $key . '}}', $var
            ->getValue(), $custom_value);
        }
      }
    }
    $variableObject = new VariableObject($variable
      ->id(), $custom_value, 'custom_value_variable');
    return $variableObject;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
BusinessRulesItemPluginBase::buildForm public function Form constructor. Overrides BusinessRulesItemPluginInterface::buildForm 11
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::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginInterface::getVariables 9
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
BusinessRulesVariablePlugin::changeDetails public function Change the variable details box. Overrides BusinessRulesVariablePluginInterface::changeDetails 5
CustomValueVariable::evaluate public function Evaluate the variable. Overrides BusinessRulesVariablePlugin::evaluate
CustomValueVariable::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
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 2
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.