You are here

protected static function WebformElementStates::buildConditionRow in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformElementStates.php \Drupal\webform\Element\WebformElementStates::buildConditionRow()

Build condition row.

Parameters

array $element: The element.

array $condition: The condition.

string $table_id: The element's table id.

int $row_index: The row index.

array $ajax_settings: An array containing Ajax callback settings.

Return value

array A render array containing a condition table row.

1 call to WebformElementStates::buildConditionRow()
WebformElementStates::processWebformStates in src/Element/WebformElementStates.php
Expand an email confirm field into two HTML5 email elements.

File

src/Element/WebformElementStates.php, line 428

Class

WebformElementStates
Provides a webform element to edit an element's #states.

Namespace

Drupal\webform\Element

Code

protected static function buildConditionRow(array $element, array $condition, $table_id, $row_index, array $ajax_settings) {
  $condition += [
    'selector' => '',
    'trigger' => '',
    'value' => '',
  ];
  $element_name = $element['#name'];
  $trigger_selector = ":input[name=\"{$element_name}[states][{$row_index}][trigger]\"]";
  $row = [
    '#attributes' => [
      'class' => [
        'webform-states-table--condition',
      ],
    ],
  ];
  $row['state'] = [];
  $row['selector'] = [
    '#type' => 'select',
    '#title' => t('Selector'),
    '#title_display' => 'invisible',
    '#options' => $element['#selector_options'],
    '#wrapper_attributes' => [
      'class' => [
        'webform-states-table--selector',
      ],
    ],
    '#default_value' => $condition['selector'],
    '#empty_option' => t('- Select -'),
    '#error_no_message' => TRUE,
  ];
  if (!isset($element['#selector_options_flattened'][$condition['selector']])) {
    $row['selector']['#options'][$condition['selector']] = $condition['selector'];
  }
  $row['condition'] = [
    '#wrapper_attributes' => [
      'class' => [
        'webform-states-table--condition',
      ],
    ],
  ];
  $row['condition']['trigger'] = [
    '#type' => 'select',
    '#title' => t('Trigger'),
    '#title_display' => 'invisible',
    '#options' => $element['#trigger_options'],
    '#default_value' => $condition['trigger'],
    '#empty_option' => t('- Select -'),
    '#parents' => [
      $element_name,
      'states',
      $row_index,
      'trigger',
    ],
    '#wrapper_attributes' => [
      'class' => [
        'webform-states-table--trigger',
      ],
    ],
    '#error_no_message' => TRUE,
  ];
  $row['condition']['value'] = [
    '#type' => 'textfield',
    '#title' => t('Value'),
    '#title_display' => 'invisible',
    '#size' => 25,
    '#default_value' => $condition['value'],
    '#placeholder' => t('Enter value…'),
    '#states' => [
      'visible' => [
        [
          $trigger_selector => [
            'value' => 'value',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => '!value',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => 'pattern',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => '!pattern',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => 'greater',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => 'greater_equal',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => 'less',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => 'less_equal',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => 'between',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => '!between',
          ],
        ],
      ],
    ],
    '#wrapper_attributes' => [
      'class' => [
        'webform-states-table--value',
      ],
    ],
    '#parents' => [
      $element_name,
      'states',
      $row_index,
      'value',
    ],
    '#error_no_message' => TRUE,
  ];
  $row['condition']['pattern'] = [
    '#type' => 'container',
    'description' => [
      '#markup' => t('Enter a <a href=":href">regular expression</a>', [
        ':href' => 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions',
      ]),
    ],
    '#states' => [
      'visible' => [
        [
          $trigger_selector => [
            'value' => 'pattern',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => '!pattern',
          ],
        ],
      ],
    ],
  ];
  $row['condition']['pattern'] = [
    '#type' => 'container',
    'description' => [
      '#markup' => t('Enter a number range (1:100)'),
    ],
    '#states' => [
      'visible' => [
        [
          $trigger_selector => [
            'value' => 'between',
          ],
        ],
        'or',
        [
          $trigger_selector => [
            'value' => '!between',
          ],
        ],
      ],
    ],
  ];
  $row['operations'] = static::buildOperations($table_id, $row_index, $ajax_settings);
  return $row;
}