You are here

public function WebformEntityConditionsManager::toText in Webform 6.x

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

Convert a webform's #states to a human read-able format.

Parameters

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

array $states: An element's #states array.

array $options: An associative array of configuration options.

Return value

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

Overrides WebformEntityConditionsManagerInterface::toText

File

src/WebformEntityConditionsManager.php, line 42

Class

WebformEntityConditionsManager
Webform submission conditions (#states) validator.

Namespace

Drupal\webform

Code

public function toText(WebformInterface $webform, array $states, array $options = []) {

  // Set default options.
  $options += [
    'name' => $this
      ->t('element'),
    'states' => [],
    'triggers' => [],
    'logic' => [],
  ];
  $options['states'] += [
    'visible' => $this
      ->t('visible'),
    'invisible' => $this
      ->t('hidden'),
    'visible-slide' => $this
      ->t('visible'),
    'invisible-slide' => $this
      ->t('hidden'),
    'enabled' => $this
      ->t('enabled'),
    'disabled' => $this
      ->t('disabled'),
    'readwrite' => $this
      ->t('read/write'),
    'readonly' => $this
      ->t('read-only'),
    'expanded' => $this
      ->t('expanded'),
    'collapsed' => $this
      ->t('collapsed'),
    'required' => $this
      ->t('required'),
    'optional' => $this
      ->t('optional'),
    'checked' => $this
      ->t('checked'),
    'unchecked' => $this
      ->t('unchecked'),
  ];
  $options['triggers'] += [
    'empty' => $this
      ->t('is empty'),
    'filled' => $this
      ->t('is filled'),
    'checked' => $this
      ->t('is checked'),
    'unchecked' => $this
      ->t('is not checked'),
    'value' => '=',
    '!value' => '!=',
    'pattern' => $this
      ->t('matches'),
    '!pattern' => $this
      ->t('does not match'),
    'less' => '<',
    'less_equal' => '<=',
    'greater' => '>',
    'greater_equal' => '>=',
    'between' => $this
      ->t('is between'),
    '!between' => $this
      ->t('is not between'),
  ];
  $options['logic'] += [
    'and' => $this
      ->t('all'),
    'or' => $this
      ->t('any'),
    'xor' => $this
      ->t('one'),
  ];
  $build = [];
  foreach ($states as $state => $conditions) {
    $t_args = [
      '@name' => $options['name'],
      '@state' => isset($options['states'][$state]) ? $options['states'][$state] : $state,
    ];
    $build[$state] = [
      'state' => [
        '#markup' => $this
          ->t('This @name is <strong>@state</strong>', $t_args),
        '#suffix' => ' ',
      ],
      'conditions' => $this
        ->buildConditions($webform, $conditions, $options),
    ];
  }
  return $build;
}