You are here

public function CustomValueVariable::evaluate in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/BusinessRulesVariable/CustomValueVariable.php \Drupal\business_rules\Plugin\BusinessRulesVariable\CustomValueVariable::evaluate()

Evaluate the variable.

Parameters

\Drupal\business_rules\Entity\Variable $variable: The variable to be evaluated.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The dispatched event.

Return value

\Drupal\business_rules\VariableObject|\Drupal\business_rules\VariablesSet The evaluated variables.

Overrides BusinessRulesVariablePlugin::evaluate

File

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

Class

CustomValueVariable
Class ConstantVariable.

Namespace

Drupal\business_rules\Plugin\BusinessRulesVariable

Code

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;
}