You are here

protected function WebformEntityConditionsManager::buildConditions in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformEntityConditionsManager.php \Drupal\webform\WebformEntityConditionsManager::buildConditions()

Convert a webform's conditions into a human read-able format.

Parameters

\Drupal\webform\WebformInterface $webform: A webform.

array $conditions: A webform's state conditions.

array $options: An associative array of configuration options.

Return value

array A renderable array containing the webform's conditions in a human read-able format.

1 call to WebformEntityConditionsManager::buildConditions()
WebformEntityConditionsManager::toText in src/WebformEntityConditionsManager.php
Convert a webform's #states to a human read-able format.

File

src/WebformEntityConditionsManager.php, line 119

Class

WebformEntityConditionsManager
Webform submission conditions (#states) validator.

Namespace

Drupal\webform

Code

protected function buildConditions(WebformInterface $webform, array $conditions, array $options) {

  // Determine condition logic.
  // @see Drupal.states.Dependent.verifyConstraints
  if (WebformArrayHelper::isSequential($conditions)) {
    $logic = in_array('xor', $conditions) ? 'xor' : 'or';
  }
  else {
    $logic = 'and';
  }
  $condition_items = [];
  foreach ($conditions as $index => $value) {

    // Skip and, or, and xor.
    if (is_string($value) && in_array($value, [
      'and',
      'or',
      'xor',
    ])) {
      continue;
    }
    if (is_int($index) && is_array($value) && (WebformArrayHelper::isSequential($value) || count($value) > 1)) {
      $condition_items[] = $this
        ->buildConditions($webform, $value, $options + [
        'nested' => TRUE,
      ]);
    }
    else {
      if (is_int($index)) {
        $selector = key($value);
        $condition = $value[$selector];
      }
      else {
        $selector = $index;
        $condition = $value;
      }
      $condition_items[] = $this
        ->buildConditionItem($webform, $selector, $condition, $options + [
        'nested' => TRUE,
      ]);
    }
  }
  $t_args = [
    '@logic' => $options['logic'][$logic],
  ];
  $build = [];
  $build['logic'] = [
    '#markup' => empty($options['nested']) ? $this
      ->t('when <strong>@logic</strong> of the following conditions are met:', $t_args) : $this
      ->t('When <strong>@logic</strong> of the following (nested) conditions are met:', $t_args),
  ];
  $build['condition'] = [
    '#theme' => 'item_list',
    '#items' => $condition_items,
  ];
  return $build;
}