public static function WebformElementComposite::processWebformElementComposite in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Element/WebformElementComposite.php \Drupal\webform\Element\WebformElementComposite::processWebformElementComposite()
Processes a webform element composite (builder) element.
File
- src/
Element/ WebformElementComposite.php, line 88
Class
- WebformElementComposite
- Provides a element for the composite elements.
Namespace
Drupal\webform\ElementCode
public static function processWebformElementComposite(&$element, FormStateInterface $form_state, &$complete_form) {
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
$element_manager = \Drupal::service('plugin.manager.webform.element');
$placeholder_elements = [];
$options_elements = [];
$type_options = [];
$elements = $element_manager
->getInstances();
$definitions = $element_manager
->getDefinitions();
$definitions = $element_manager
->getSortedDefinitions($definitions, 'category');
$definitions = $element_manager
->removeExcludeDefinitions($definitions);
$grouped_definitions = $element_manager
->getGroupedDefinitions($definitions);
foreach ($grouped_definitions as $group => $definitions) {
foreach ($definitions as $element_type => $definition) {
if (!WebformCompositeBaseElement::isSupportedElementType($element_type)) {
continue;
}
$element_plugin = $elements[$element_type];
$type_options[$group][$element_type] = $definition['label'];
if ($element_plugin
->hasProperty('options')) {
$options_elements[$element_type] = $element_type;
}
if ($element_plugin
->hasProperty('placeholder')) {
$placeholder_elements[$element_type] = $element_type;
}
}
}
$edit_source = \Drupal::currentUser()
->hasPermission('edit webform source');
$element['#tree'] = TRUE;
$element['elements'] = [
'#type' => 'webform_multiple',
'#title' => t('Elements'),
'#title_display' => 'invisible',
'#label' => t('element'),
'#labels' => t('elements'),
'#empty_items' => 0,
'#min_items' => 1,
'#header' => TRUE,
'#add' => FALSE,
'#default_value' => isset($element['#default_value']) ? $element['#default_value'] : NULL,
'#error_no_message' => TRUE,
'#element' => [
'settings' => [
'#type' => 'container',
'#title' => t('Settings'),
'#help' => '<b>' . t('Key') . ':</b> ' . t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.') . '<hr/>' . '<b>' . t('Type') . ':</b> ' . t('The type of element to be displayed.') . '<hr/>' . '<b>' . t('Options') . ':</b> ' . t('Please select predefined options or enter custom options.') . ' ' . t('Key-value pairs MUST be specified as "safe_key: \'Some readable options\'". Use of only alphanumeric characters and underscores is recommended in keys. One option per line.') . ($edit_source ? '<hr/>' . '<b>' . t('Custom Properties') . ':</b> ' . t('Properties do not have to be prepended with a hash (#) character, the hash character will be automatically added to the custom properties.') : '') . '<hr/>' . '<b>' . t('Required') . ':</b> ' . t('Check this option if the user must enter a value.'),
'key' => [
'#type' => 'textfield',
'#title' => t('Key'),
'#title_display' => 'invisible',
'#placeholder' => t('Enter key…'),
'#pattern' => '^[a-z0-9_]+$',
'#attributes' => [
'title' => t('Enter a unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
],
'#required' => TRUE,
'#error_no_message' => TRUE,
],
'type' => [
'#type' => 'select',
'#title' => t('Type'),
'#title_display' => 'invisible',
'#description' => t('The type of element to be displayed.'),
'#description_display' => 'invisible',
'#options' => $type_options,
'#empty_option' => t('- Select type -'),
'#required' => TRUE,
'#attributes' => [
'class' => [
'js-webform-composite-type',
],
],
'#error_no_message' => TRUE,
],
'options' => [
'#type' => 'webform_element_options',
'#yaml' => TRUE,
'#title' => t('Options'),
'#title_display' => 'invisible',
'#description' => t('Please select predefined options or enter custom options.') . ' ' . t('Key-value pairs MUST be specified as "safe_key: \'Some readable options\'". Use of only alphanumeric characters and underscores is recommended in keys. One option per line.'),
'#description_display' => 'invisible',
'#wrapper_attributes' => [
'data-composite-types' => implode(',', $options_elements),
'data-composite-required' => 'data-composite-required',
],
'#error_no_message' => TRUE,
],
// ISSUE:
// Set #access: FALSE is losing the custom properties.
//
// WORKAROUND:
// Use 'hidden' element.
// @see \Drupal\webform\Element\WebformMultiple::buildElementRow
'custom' => $edit_source ? [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => t('Custom properties'),
'#title_display' => 'invisible',
'#description' => t('Properties do not have to be prepended with a hash (#) character, the hash character will be automatically added to the custom properties.'),
'#description_display' => 'invisible',
'#placeholder' => t('Enter custom properties…'),
'#error_no_message' => TRUE,
] : [
'#type' => 'hidden',
],
// Note: Setting #return_value: TRUE is not returning any value.
'required' => [
'#type' => 'checkbox',
'#title' => t('Required'),
'#description' => t('Check this option if the user must enter a value.'),
'#description_display' => 'invisible',
'#error_no_message' => TRUE,
],
],
'labels' => [
'#type' => 'container',
'#title' => t('Labels'),
'#help' => '<b>' . t('Title') . ':</b> ' . t('This is used as a descriptive label when displaying this webform element.') . '<hr/><b>' . t('Placeholder') . ':</b> ' . t('The placeholder will be shown in the element until the user starts entering a value.') . '<hr/><b>' . t('Description') . ':</b> ' . t('A short description of the element used as help for the user when they use the webform.') . '<hr/><b>' . t('Help text') . ':</b> ' . t('A tooltip displayed after the title.'),
'title' => [
'#type' => 'textfield',
'#title' => t('Title'),
'#title_display' => 'invisible',
'#description' => t('This is used as a descriptive label when displaying this webform element.'),
'#description_display' => 'invisible',
'#placeholder' => t('Enter title…'),
'#required' => TRUE,
'#error_no_message' => TRUE,
],
'placeholder' => [
'#type' => 'textfield',
'#title' => t('Placeholder'),
'#title_display' => 'invisible',
'#description' => t('The placeholder will be shown in the element until the user starts entering a value.'),
'#description_display' => 'invisible',
'#placeholder' => t('Enter placeholder…'),
'#attributes' => [
'data-composite-types' => implode(',', $placeholder_elements),
],
'#error_no_message' => TRUE,
],
'description' => [
'#type' => 'textarea',
'#title' => t('Description'),
'#description' => t('A short description of the element used as help for the user when they use the webform.'),
'#description_display' => 'invisible',
'#title_display' => 'invisible',
'#placeholder' => t('Enter description…'),
'#rows' => 2,
'#error_no_message' => TRUE,
],
'help' => [
'#type' => 'textarea',
'#title' => t('Help text'),
'#title_display' => 'invisible',
'#description' => t('A tooltip displayed after the title.'),
'#description_display' => 'invisible',
'#placeholder' => t('Enter help text…'),
'#rows' => 2,
'#error_no_message' => TRUE,
],
],
],
];
$element['#attached']['library'][] = 'webform/webform.element.composite';
// Add validate callback.
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformElementComposite',
]);
return $element;
}