You are here

protected function WebformEntityConditionsManager::buildConditionItem in Webform 6.x

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

Convert a condition's select, trigger, and value into a human read-able format.

Parameters

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

string $selector: The condition's selector (i.e. :input[name="{element_key}").

array $condition: The condition's trigger and value.

array $options: An associative array of configuration options.

Return value

array A renderable array containing a condition's select, trigger, and value in a human read-able format.

1 call to WebformEntityConditionsManager::buildConditionItem()
WebformEntityConditionsManager::buildConditions in src/WebformEntityConditionsManager.php
Convert a webform's conditions into a human read-able format.

File

src/WebformEntityConditionsManager.php, line 185

Class

WebformEntityConditionsManager
Webform submission conditions (#states) validator.

Namespace

Drupal\webform

Code

protected function buildConditionItem(WebformInterface $webform, $selector, array $condition, array $options) {
  if (WebformArrayHelper::isSequential($condition)) {
    $sub_condition_items = [];
    foreach ($condition as $sub_condition) {
      $sub_condition_items[] = $this
        ->buildConditionItem($webform, $selector, $sub_condition, $options);
    }
    return $sub_condition_items;
  }

  // Ignore invalid selector and return an empty render array.
  $input_name = WebformSubmissionConditionsValidator::getSelectorInputName($selector);
  if (!$input_name) {
    return [];
  }
  $element_key = WebformSubmissionConditionsValidator::getInputNameAsArray($input_name, 0);
  $element_option_key = WebformSubmissionConditionsValidator::getInputNameAsArray($input_name, 1);
  $element = $webform
    ->getElement($element_key);

  // If no element is found try checking file uploads which use
  // :input[name="files[ELEMENT_KEY].
  // @see \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::getElementSelectorOptions
  if (!$element && strpos($selector, ':input[name="files[') === 0) {
    $element_key = WebformSubmissionConditionsValidator::getInputNameAsArray($input_name, 1);
    $element = $webform
      ->getWebform()
      ->getElement($element_key);
  }

  // Ignore missing dependee element and return an empty render array.
  if (!$element) {
    return [];
  }
  $trigger_state = key($condition);
  $trigger_value = $condition[$trigger_state];
  $element_plugin = $this->elementManager
    ->getElementInstance($element);

  // Ignored conditions for generic webform elements.
  if ($element_plugin instanceof WebformElement) {
    return [];
  }

  // Process trigger sub state used for custom #states API validation.
  // @see Drupal.behaviors.webformStatesComparisons
  // @see http://drupalsun.com/julia-evans/2012/03/09/extending-form-api-states-regular-expressions
  if ($trigger_state === 'value' && is_array($trigger_value)) {
    $trigger_substate = key($trigger_value);
    if (in_array($trigger_substate, [
      'pattern',
      '!pattern',
      'less',
      'less_equal',
      'greater',
      'greater_equal',
      'between',
      '!between',
    ])) {
      $trigger_state = $trigger_substate;
      $trigger_value = reset($trigger_value);
    }
  }

  // Get element options.
  $element_options = isset($element['#options']) ? OptGroup::flattenOptions($element['#options']) : [];

  // Set element title.
  $element_title = $element['#admin_title'];

  // Set trigger value and suffix element title with the trigger's option value.
  if ($element_option_key) {
    $element_title .= ': ' . WebformOptionsHelper::getOptionText($element_option_key, $element_options, TRUE);
  }

  // Checked 'checked: false' to 'unchecked: true' and vice-versa.
  if ($trigger_state === 'checked' && $trigger_value === FALSE) {
    $trigger_state = 'unchecked';
    $trigger_value = TRUE;
  }
  elseif ($trigger_state === 'unchecked' && $trigger_value === FALSE) {
    $trigger_state = 'checked';
    $trigger_value = TRUE;
  }

  // Build the condition.
  $t_args = [
    '@name' => $element_title,
    '@trigger' => $options['triggers'][$trigger_state],
  ];

  // Do not return the value boolean value for empty or checked states.
  switch ($trigger_state) {
    case 'empty':
    case 'filled':
    case 'checked':
    case 'unchecked':
      return [
        '#markup' => $this
          ->t('<strong>@name</strong> @trigger.', $t_args),
      ];
    case 'between':
      $range = explode(':', $trigger_value);
      $t_args['@min'] = $range[0];
      $t_args['@max'] = $range[1];
      return [
        '#markup' => $this
          ->t('<strong>@name</strong> @trigger <strong>@min</strong> and <strong>@max</strong>.', $t_args),
      ];
    default:
      $t_args['@value'] = isset($element_options[$trigger_value]) ? $element_options[$trigger_value] : $trigger_value;
      return [
        '#markup' => $this
          ->t('<strong>@name</strong> @trigger <strong>@value</strong>.', $t_args),
      ];
  }
}