public function OptionsBase::prepare in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformElement/OptionsBase.php \Drupal\webform\Plugin\WebformElement\OptionsBase::prepare()
Prepare an element to be rendered within a webform.
Parameters
array $element: An element.
\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.
Overrides WebformElementBase::prepare
See also
\Drupal\webform\Element\WebformCompositeBase::processWebformComposite
2 calls to OptionsBase::prepare()
- Checkboxes::prepare in src/
Plugin/ WebformElement/ Checkboxes.php - Prepare an element to be rendered within a webform.
- Select::prepare in src/
Plugin/ WebformElement/ Select.php - Prepare an element to be rendered within a webform.
2 methods override OptionsBase::prepare()
- Checkboxes::prepare in src/
Plugin/ WebformElement/ Checkboxes.php - Prepare an element to be rendered within a webform.
- Select::prepare in src/
Plugin/ WebformElement/ Select.php - Prepare an element to be rendered within a webform.
File
- src/
Plugin/ WebformElement/ OptionsBase.php, line 157
Class
- OptionsBase
- Provides a base 'options' element.
Namespace
Drupal\webform\Plugin\WebformElementCode
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
$is_wrapper_fieldset = in_array($element['#type'], [
'checkboxes',
'radios',
'webform_entity_checkboxes',
'webform_entity_radios',
'webform_term_checkboxes',
'webform_toggles',
'webform_buttons',
]);
if ($is_wrapper_fieldset) {
// Issue #2396145: Option #description_display for webform element fieldset
// is not changing anything.
// @see core/modules/system/templates/fieldset.html.twig
$is_description_display = isset($element['#description_display']) ? TRUE : FALSE;
$has_description = !empty($element['#description']) ? TRUE : FALSE;
if ($is_description_display && $has_description) {
$description = WebformElementHelper::convertToString($element['#description']);
switch ($element['#description_display']) {
case 'before':
$element += [
'#field_prefix' => '',
];
$element['#field_prefix'] = '<div class="description">' . $description . '</div>' . $element['#field_prefix'];
unset($element['#description']);
unset($element['#description_display']);
break;
case 'tooltip':
$element += [
'#field_suffix' => '',
];
$element['#field_suffix'] .= '<div class="description visually-hidden">' . $description . '</div>';
// @see \Drupal\Core\Render\Element\CompositeFormElementTrait
// @see \Drupal\webform\Plugin\WebformElementBase::prepare
$element['#attributes']['class'][] = 'js-webform-tooltip-element';
$element['#attributes']['class'][] = 'webform-tooltip-element';
$element['#attached']['library'][] = 'webform/webform.tooltip';
unset($element['#description']);
unset($element['#description_display']);
break;
case 'invisible':
$element += [
'#field_suffix' => '',
];
$element['#field_suffix'] .= '<div class="description visually-hidden">' . $description . '</div>';
unset($element['#description']);
unset($element['#description_display']);
break;
}
}
}
parent::prepare($element, $webform_submission);
// Randomize options.
if (isset($element['#options']) && !empty($element['#options_randomize'])) {
$element['#options'] = WebformElementHelper::randomize($element['#options']);
}
// Options description display must be set to trigger the description display.
if ($this
->hasProperty('options_description_display') && empty($element['#options_description_display'])) {
$element['#options_description_display'] = $this
->getDefaultProperty('options_description_display');
}
// Options display must be set to trigger the options display.
if ($this
->hasProperty('options_display') && empty($element['#options_display'])) {
$element['#options_display'] = $this
->getDefaultProperty('options_display');
}
// Make sure submitted value is not lost if the element's #options were
// altered after the submission was completed.
// This only applies to the main webforom element with a #webform_key
// and not a webform composite's sub elements.
$is_completed = $webform_submission && $webform_submission
->isCompleted();
$has_default_value = isset($element['#default_value']) && $element['#default_value'] !== '' && $element['#default_value'] !== NULL;
if ($is_completed && $has_default_value && !$this
->isOptionsOther() && isset($element['#webform_key'])) {
if ($element['#default_value'] === $webform_submission
->getElementData($element['#webform_key'])) {
$options = OptGroup::flattenOptions($element['#options']);
$default_values = (array) $element['#default_value'];
foreach ($default_values as $default_value) {
if (!isset($options[$default_value])) {
$element['#options'][$default_value] = $default_value;
}
}
}
}
// If the element is #required and the #default_value is an empty string
// we need to unset the #default_value to prevent the below error.
// 'An illegal choice has been detected'.
if (!empty($element['#required']) && isset($element['#default_value']) && $element['#default_value'] === '') {
unset($element['#default_value']);
}
// Process custom options properties.
if ($this
->hasProperty('options__properties')) {
$this
->setElementDefaultCallback($element, 'process');
$element['#process'][] = [
get_class($this),
'processOptionsProperties',
];
}
}