You are here

public function BusinessRulesUtil::getVariablesDetailsBox in Business Rules 2.x

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

Return a details box which the available variables for use on this context.

Parameters

\Drupal\business_rules\Entity\BusinessRulesItemBase $item: The business Rule Item.

string $plugin_type: The variable plugin type id.

Return value

array The render array.

File

src/Util/BusinessRulesUtil.php, line 605

Class

BusinessRulesUtil
Class BusinessRulesUtil.

Namespace

Drupal\business_rules\Util

Code

public function getVariablesDetailsBox(BusinessRulesItemBase $item, $plugin_type = '') {
  $target_entity_type = $item
    ->getTargetEntityType();
  $target_bundle = $item
    ->getTargetBundle();
  $variables = Variable::loadMultiple();
  $available_variables = [];
  $details = [];
  if (is_array($variables)) {

    /** @var \Drupal\business_rules\Entity\Variable $variable */
    foreach ($variables as $variable) {

      // Check targetBundle.
      if ((($variable
        ->getTargetBundle() == $target_bundle || empty($target_bundle) || empty($variable
        ->getTargetBundle())) && ($variable
        ->getTargetEntityType() == $target_entity_type || empty($target_entity_type) || empty($variable
        ->getTargetEntityType())) || !$variable
        ->isContextDependent()) && ($plugin_type == '' || $plugin_type == $variable
        ->getType()) && ($item instanceof Variable && $item
        ->id() != $variable
        ->id() || !$item instanceof Variable)) {
        $available_variables[] = $variable;
      }
    }
  }
  if (is_array($available_variables) && count($available_variables)) {
    $storage = $this->entityTypeManager
      ->getStorage('business_rules_variable');
    $list = new VariableListBuilder($variable
      ->getEntityType(), $storage);
    $details = [
      '#type' => 'details',
      '#title' => $this
        ->t('Available Variables for this context'),
      '#collapsed' => TRUE,
      '#collapsable' => TRUE,
    ];
    $header = $list
      ->buildHeader();
    $new_header['id'] = $this
      ->t('Variable');
    unset($header['id']);
    foreach ($header as $key => $item) {
      $new_header[$key] = $item;
    }
    $header = $new_header;
    $rows = [];
    foreach ($available_variables as $variable) {
      $row = $list
        ->buildRow($variable);
      $new_row['id'] = '{{' . $row['id']['data']['#markup'] . '}}';
      unset($row['id']);
      foreach ($row as $key => $item) {
        $new_row[$key] = $item;
      }

      // Give a chance to the variable plugin change the details about this
      // availability.
      $type = $variable
        ->getType();
      $variable_type = $this->variableManager
        ->getDefinition($type);
      $reflection = new \ReflectionClass($variable_type['class']);
      $defined_variable = $reflection
        ->newInstance($variable_type, $variable_type['id'], $variable_type);
      $defined_variable
        ->changeDetails($variable, $new_row);
      $rows[] = $new_row;
    }
    $details['variables'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    ];
  }
  return $details;
}