You are here

protected function BusinessRulesProcessor::getDebugItems in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Util/BusinessRulesProcessor.php \Drupal\business_rules\Util\BusinessRulesProcessor::getDebugItems()

Helper function to prepare the render array for the Business Rules Items.

Parameters

array $items: Array of items.

string $parent_id: The parent item id.

Return value

array The render array.

1 call to BusinessRulesProcessor::getDebugItems()
BusinessRulesProcessor::getDebugRenderArray in src/Util/BusinessRulesProcessor.php
Generates the render array for business_rules debug.

File

src/Util/BusinessRulesProcessor.php, line 553

Class

BusinessRulesProcessor
Class BusinessRulesProcessor.

Namespace

Drupal\business_rules\Util

Code

protected function getDebugItems(array $items, $parent_id) {

  /** @var \Drupal\business_rules\BusinessRulesItemObject $item */

  /** @var \Drupal\business_rules\Entity\Action $executed_action */

  /** @var \Drupal\business_rules\Entity\Condition $executed_condition */
  $actions_executed = isset($this->debugArray['actions'][$this->ruleBeingExecuted
    ->id()]) ? $this->debugArray['actions'][$this->ruleBeingExecuted
    ->id()] : [];
  $conditions_success = isset($this->debugArray['conditions'][$this->ruleBeingExecuted
    ->id()]['success']) ? $this->debugArray['conditions'][$this->ruleBeingExecuted
    ->id()]['success'] : [];
  $output = [];
  foreach ($items as $item) {
    if ($item
      ->getType() == BusinessRulesItemObject::ACTION) {
      $action = Action::load($item
        ->getId());
      if (!empty($action)) {
        $action_link = Link::createFromRoute($action
          ->id(), 'entity.business_rules_action.edit_form', [
          'business_rules_action' => $action
            ->id(),
        ]);
        $style = 'fail';
        foreach ($actions_executed as $executed) {
          $action_parent = $executed['parent'];
          $executed_action = $executed['item'];
          if ($action_parent == $parent_id) {
            $style = $executed_action
              ->id() == $action
              ->id() ? 'success' : 'fail';
            if ($style == 'success') {
              break;
            }
          }
        }
        $action_label = $this
          ->t('Action');
        $output[$item
          ->getId()] = [
          '#type' => 'details',
          '#title' => $action_label . ': ' . $action
            ->label(),
          '#description' => $action_link
            ->toString() . '<br>' . $action
            ->getDescription(),
          '#attributes' => [
            'class' => [
              $style,
            ],
          ],
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
        ];
        if (isset($this->debugArray['action_result'][$this->ruleBeingExecuted
          ->id()][$item
          ->getId()])) {
          $output[$item
            ->getId()]['action_result'][$this->ruleBeingExecuted
            ->id()] = $this->debugArray['action_result'][$this->ruleBeingExecuted
            ->id()][$item
            ->getId()];
        }
      }
    }
    elseif ($item
      ->getType() == BusinessRulesItemObject::CONDITION) {
      $condition = Condition::load($item
        ->getId());
      if (!empty($condition)) {
        $condition_link = Link::createFromRoute($condition
          ->id(), 'entity.business_rules_condition.edit_form', [
          'business_rules_condition' => $condition
            ->id(),
        ]);
        $style = 'fail';
        foreach ($conditions_success as $success) {
          $condition_parent = $success['parent'];
          $executed_condition = $success['item'];
          if ($condition_parent == $parent_id) {
            $style = $executed_condition
              ->id() == $condition
              ->id() ? 'success' : 'fail';
            if ($style == 'success') {
              break;
            }
          }
        }
        $title = $condition
          ->isReverse() ? $this
          ->t('(Not)') . ' ' . $condition
          ->label() : $condition
          ->label();
        $condition_label = $this
          ->t('Condition');
        $output[$item
          ->getId()] = [
          '#type' => 'details',
          '#title' => $condition_label . ': ' . $title,
          '#description' => $condition_link
            ->toString() . '<br>' . $condition
            ->getDescription(),
          '#attributes' => [
            'class' => [
              $style,
            ],
          ],
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
        ];
        $success_items = $condition
          ->getSuccessItems();
        if (is_array($success_items) && count($success_items)) {
          $output[$item
            ->getId()]['success'] = [
            '#type' => 'details',
            '#title' => $this
              ->t('Success items'),
            '#attributes' => [
              'class' => [
                $style,
              ],
            ],
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
          ];
          $output[$item
            ->getId()]['success'][] = $this
            ->getDebugItems($success_items, $condition
            ->id());
        }
        $fail_items = $condition
          ->getFailItems();
        if (is_array($fail_items) && count($fail_items)) {
          $output[$item
            ->getId()]['fail'] = [
            '#type' => 'details',
            '#title' => $this
              ->t('Fail items'),
            '#attributes' => [
              'class' => [
                $style == 'success' ? 'fail' : 'success',
              ],
            ],
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
          ];
          $output[$item
            ->getId()]['fail'][] = $this
            ->getDebugItems($fail_items, $condition
            ->id());
        }
      }
    }
  }
  return $output;
}