public function DateTime::prepare in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformElement/DateTime.php \Drupal\webform\Plugin\WebformElement\DateTime::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 DateBase::prepare
See also
\Drupal\webform\Element\WebformCompositeBase::processWebformComposite
File
- src/
Plugin/ WebformElement/ DateTime.php, line 79
Class
- DateTime
- Provides a 'datetime' element.
Namespace
Drupal\webform\Plugin\WebformElementCode
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
// Must define a '#default_value' for Datetime element to prevent the
// below error.
// Notice: Undefined index: #default_value in Drupal\Core\Datetime\Element\Datetime::valueCallback().
if (!isset($element['#default_value'])) {
$element['#default_value'] = NULL;
}
// Remove 'for' from the element's label.
$element['#label_attributes']['webform-remove-for-attribute'] = TRUE;
/* Date */
$date_element = isset($element['#date_date_element']) ? $element['#date_date_element'] : 'date';
// Unset unsupported date format for date elements that are not
// text or datepicker.
if (!in_array($date_element, [
'text',
'datepicker',
])) {
unset($element['date_date_format']);
}
// Set date year range.
$element += [
'#date_year_range' => '',
];
if (empty($element['#date_year_range'])) {
$date_min = $this
->getElementProperty($element, 'date_date_min') ?: $this
->getElementProperty($element, 'date_min');
$min_year = $date_min ? static::formatDate('Y', strtotime($date_min)) : '1900';
$date_max = $this
->getElementProperty($element, 'date_date_max') ?: $this
->getElementProperty($element, 'date_max');
$max_year = $date_max ? static::formatDate('Y', strtotime($date_max)) : '2050';
$element['#date_year_range'] = "{$min_year}:{$max_year}";
}
// Set date format.
if (!isset($element['#date_date_format'])) {
$element['#date_date_format'] = $this
->getDefaultProperty('date_date_format');
}
// Add date callback.
$element['#date_date_callbacks'][] = '_webform_datetime_date';
/* Time */
// Set time format.
if (!isset($element['#date_time_format'])) {
$element['#date_time_format'] = $this
->getDefaultProperty('date_time_format');
}
// Add time callback.
$element['#date_time_callbacks'][] = '_webform_datetime_time';
// Prepare element after date/time formats have been updated.
parent::prepare($element, $webform_submission);
}