You are here

public function ViewResultVariable::evaluate in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesVariable/ViewResultVariable.php \Drupal\business_rules\Plugin\BusinessRulesVariable\ViewResultVariable::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/ViewResultVariable.php, line 211

Class

ViewResultVariable
Class ViewResultVariable.

Namespace

Drupal\business_rules\Plugin\BusinessRulesVariable

Code

public function evaluate(Variable $variable, BusinessRulesEvent $event) {

  // Get settings.
  $defined_view = $variable
    ->getSettings('view');
  $view_fields = $variable
    ->getSettings('view_html');
  $args = $variable
    ->getSettings('arguments');
  $event_variables = $event
    ->getArgument('variables');

  // Process settings.
  $defined_view = explode(':', $defined_view);
  $view_id = $defined_view[0];
  $display = $defined_view[1];
  $args = explode(chr(10), $args);
  $args = array_map('trim', $args);
  $args = array_filter($args, 'strlen');

  // Process variables.
  foreach ($args as $key => $value) {
    $args[$key] = $this
      ->processVariables($value, $event_variables);
  }

  // Execute view.
  $view = Views::getView($view_id);
  $view
    ->setArguments($args);
  $view
    ->setDisplay($display);
  $view
    ->preExecute();
  $view
    ->build();
  $fields = $view->field;
  $variableObject = NULL;
  if ($view
    ->execute()) {

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    /** @var \Drupal\views\ResultRow $resultRow */
    $view_result = $view->result;
    $values = [];
    foreach ($view_result as $key => $resultRow) {
      if ($view_fields == TRUE) {

        // Inner field array
        $inner_values = [];

        // Building new context for cron
        $output = $renderer
          ->executeInRenderContext(new RenderContext(), function () use ($inner_values, $resultRow, $fields) {

          /** @var \Drupal\views\Plugin\views\field\Field $field */
          foreach ($fields as $field) {

            // Get field ID
            $field_id = $field->field;

            // Get HTML markup
            $html_rended = Markup::create($field
              ->advancedRender($resultRow));

            // Pass extra filter to be sure data is a string
            if (!is_string($html_rended)) {
              $html_rended = $html_rended
                ->__toString();
            }

            // Pass field data to array
            $inner_values[$field_id] = $html_rended;
          }
          return $inner_values;
        });
        $values[$key] = $output;
      }
      else {
        if ($view_fields == TRUE) {

          // Inner field array
          $inner_values = [];

          // Building new context for cron
          $output = $renderer
            ->executeInRenderContext(new RenderContext(), function () use ($inner_values, $resultRow, $fields) {

            /** @var \Drupal\views\Plugin\views\field\Field $field */
            foreach ($fields as $field) {

              // Get field ID
              $field_id = $field->field;

              // Get HTML markup
              $html_rended = Markup::create($field
                ->advancedRender($resultRow));

              // Pass extra filter to be sure data is a string
              if (!is_string($html_rended)) {
                $html_rended = $html_rended
                  ->__toString();
              }

              // Pass field data to array
              $inner_values[$field_id] = $html_rended;
            }
            return $inner_values;
          });
          $values[$key] = $output;
        }
        else {

          /** @var \Drupal\views\Plugin\views\field\Field $field */
          foreach ($fields as $field) {
            $field_id = $field->field;
            $values[$key][$field_id] = $field
              ->getValue($resultRow);
          }
        }
      }
    }
    $variableObject = new VariableObject($variable
      ->id(), $values, $variable
      ->getType());
  }
  else {
    $this->util->logger
      ->error('View %view could not be executed. Arguments: %args', [
      '%view' => $defined_view,
      '%args' => implode(', ', $args),
    ]);
  }
  return $variableObject;
}