function _webform_test_states in Webform 6.x
Same name and namespace in other branches
- 8.5 tests/modules/webform_test/includes/webform_test.test_states.inc \_webform_test_states()
Generates a render array of example elements.
Parameters
string $type: Type of state being generated.
array $default_properties: Associative array of default element properties.
Return value
array A render array of example elements
3 calls to _webform_test_states()
- webform_test_test_states_client_disabled in tests/
modules/ webform_test/ includes/ webform_test.test_states_client_disabled.inc - Generate test elements with #states.
- webform_test_test_states_client_invisible in tests/
modules/ webform_test/ includes/ webform_test.test_states_client_invisible.inc - Generate test elements with #states.
- webform_test_test_states_client_visible in tests/
modules/ webform_test/ includes/ webform_test.test_states_client_visible.inc - Generate test elements with #states.
File
- tests/
modules/ webform_test/ includes/ webform_test.test_states.inc, line 21 - Generate test elements with #states.
Code
function _webform_test_states($type, array $default_properties = []) {
$ignored_element_types = [
'captcha',
'machine_name',
'table',
'webform_card',
'webform_more',
'webform_table',
'webform_table_row',
];
$data = [
'containers' => [],
'elements' => [],
];
// Create a webform which will be used to generate test data.
$webform = Webform::create();
/** @var \Drupal\webform\WebformSubmissionGenerateInterface $generate */
$generate = \Drupal::service('webform_submission.generate');
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
$element_manager = \Drupal::service('plugin.manager.webform.element');
$elements = $element_manager
->getInstances();
foreach ($elements as $element_type => $webform_element) {
if (preg_match('/(webform_options_custom:|webform_options_custom_entity:)/', $element_type) || in_array($element_type, $ignored_element_types)) {
continue;
}
$element = _webform_test_get_element_preview($element_type);
if (!$element) {
continue;
}
// Set default container content.
if ($webform_element
->isContainer($element)) {
// Containers can't be disabled.
if ($type === 'disabled') {
continue;
}
}
else {
// Set default test data as default value.
// Don't generate a managed file and signature element.
if (!preg_match('/^(.*_file|webform_signature)$/', $element_type)) {
if ($value = $generate
->getTestValue($webform, $element_type, $element, [
'random' => FALSE,
])) {
$element['#default_value'] = $value;
}
}
}
// Set default properties.
$element += $default_properties;
$group = $webform_element
->isContainer($element) ? 'containers' : 'elements';
$element_key = str_replace(':', '_', $element_type);
$data[$group][$element_key] = $element;
}
return $data['containers'] + $data['elements'];
}