You are here

public function VariableForm::getVariableUsedByDetailsBox in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Form/VariableForm.php \Drupal\business_rules\Form\VariableForm::getVariableUsedByDetailsBox()

Show details box of items using this variable.

Parameters

string $item_type: The item type: action|condition.

Return value

array The render array.

1 call to VariableForm::getVariableUsedByDetailsBox()
VariableForm::form in src/Form/VariableForm.php
Gets the actual form array to be built.

File

src/Form/VariableForm.php, line 51

Class

VariableForm
Class VariableForm.

Namespace

Drupal\business_rules\Form

Code

public function getVariableUsedByDetailsBox($item_type) {

  /** @var \Drupal\business_rules\Entity\BusinessRulesItemBase $item */
  if ($item_type == 'condition') {
    $items = Condition::loadMultiple();
    $entity_type = 'business_rules_condition';
    $box_title = $this
      ->t('Conditions using this variable');
    $storage = $this->entityTypeManager
      ->getStorage($entity_type);
  }
  elseif ($item_type == 'action') {
    $items = Action::loadMultiple();
    $entity_type = 'business_rules_action';
    $box_title = $this
      ->t('Actions using this variable');
    $storage = $this->entityTypeManager
      ->getStorage($entity_type);
  }
  $used_by = [];
  $details = [];
  foreach ($items as $key => $item) {
    $variables = $item
      ->getVariables();

    /** @var \Drupal\business_rules\VariableObject $variable */
    foreach ($variables
      ->getVariables() as $variable) {
      if ($this->entity
        ->id() == $variable
        ->getId()) {
        $used_by[$key] = $item;
      }
    }
  }
  if (count($used_by)) {
    if ($item_type == 'condition') {
      $list = new ConditionListBuilder($item
        ->getEntityType(), $storage);
    }
    elseif ($item_type == 'action') {
      $list = new ActionListBuilder($item
        ->getEntityType(), $storage);
    }
    $details = [
      '#type' => 'details',
      '#title' => $box_title,
      '#collapsed' => TRUE,
      '#collapsable' => TRUE,
    ];
    $header = $list
      ->buildHeader();
    $rows = [];
    foreach ($used_by as $item) {
      $rows[] = $list
        ->buildRow($item);
    }
    $details['used_by'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    ];
  }
  return $details;
}