abstract class DateBase in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformElement/DateBase.php \Drupal\webform\Plugin\WebformElement\DateBase
Provides a base 'date' class.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\webform\Plugin\WebformElementBase implements TrustedCallbackInterface, WebformElementInterface uses MessengerTrait, StringTranslationTrait, WebformCompositeFormElementTrait, WebformEntityStorageTrait, WebformEntityInjectionTrait
- class \Drupal\webform\Plugin\WebformElement\DateBase
- class \Drupal\webform\Plugin\WebformElementBase implements TrustedCallbackInterface, WebformElementInterface uses MessengerTrait, StringTranslationTrait, WebformCompositeFormElementTrait, WebformEntityStorageTrait, WebformEntityInjectionTrait
Expanded class hierarchy of DateBase
1 file declares its use of DateBase
- WebformDevelSchema.php in modules/
webform_devel/ src/ WebformDevelSchema.php
File
- src/
Plugin/ WebformElement/ DateBase.php, line 24
Namespace
Drupal\webform\Plugin\WebformElementView source
abstract class DateBase extends WebformElementBase {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->dateFormatter = $container
->get('date.formatter');
return $instance;
}
/**
* {@inheritdoc}
*/
protected function defineDefaultProperties() {
return [
// Form validation.
'date_date_min' => '',
'date_date_max' => '',
'date_days' => [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
],
] + parent::defineDefaultProperties() + $this
->defineDefaultMultipleProperties();
}
/****************************************************************************/
// Element rendering methods.
/****************************************************************************/
/**
* {@inheritdoc}
*/
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
// Don't used 'datetime_wrapper', instead use 'form_element' wrapper.
// Note: Below code must be executed before parent::prepare().
// @see \Drupal\Core\Datetime\Element\Datelist
// @see \Drupal\webform\Plugin\WebformElement\DateTime
$element['#theme_wrappers'] = [
'form_element',
];
// Must manually process #states.
// @see \Drupal\Core\Form\FormHelper::processStates
if (!empty($element['#states'])) {
$element['#attached']['library'][] = 'core/drupal.states';
$element['#wrapper_attributes']['data-drupal-states'] = Json::encode($element['#states']);
}
parent::prepare($element, $webform_submission);
// Parse #default_value date input format.
$this
->parseInputFormat($element, '#default_value');
// If date picker does not exist, remove date picker related properties.
if (!$this
->datePickerExists()) {
unset($element['#datepicker'], $element['#datepicker_button'], $element['#date_date_format'], $element['#date_date_datepicker_button']);
}
// Set date min/max attributes.
// This overrides extra attributes set via Datetime::processDatetime.
// @see \Drupal\Core\Datetime\Element\Datetime::processDatetime
if (isset($element['#date_date_format'])) {
$date_min = $this
->getElementProperty($element, 'date_date_min') ?: $this
->getElementProperty($element, 'date_min');
if ($date_min) {
$element['#attributes']['min'] = static::formatDate($element['#date_date_format'], strtotime($date_min));
$element['#attributes']['data-min-year'] = static::formatDate('Y', strtotime($date_min));
}
$date_max = $this
->getElementProperty($element, 'date_date_max') ?: $this
->getElementProperty($element, 'date_max');
if (!empty($date_max)) {
$element['#attributes']['max'] = static::formatDate($element['#date_date_format'], strtotime($date_max));
$element['#attributes']['data-max-year'] = static::formatDate('Y', strtotime($date_max));
}
}
// Set date days (of week) attributes.
if (!empty($element['#date_days'])) {
$element['#attributes']['data-days'] = implode(',', $element['#date_days']);
}
// Display datepicker button.
if (!empty($element['#datepicker_button']) || !empty($element['#date_date_datepicker_button'])) {
$element['#attributes']['data-datepicker-button'] = TRUE;
$element['#attached']['drupalSettings']['webform']['datePicker']['buttonImage'] = base_path() . drupal_get_path('module', 'webform') . '/images/elements/date-calendar.png';
}
// Set first day according to admin/config/regional/settings.
$config = $this->configFactory
->get('system.date');
$element['#attached']['drupalSettings']['webform']['dateFirstDay'] = $config
->get('first_day');
$cacheability = CacheableMetadata::createFromObject($config);
$cacheability
->applyTo($element);
if ($this
->datePickerExists()) {
$element['#attached']['library'][] = 'webform/webform.element.date';
}
$element['#after_build'][] = [
get_class($this),
'afterBuild',
];
}
/**
* {@inheritdoc}
*/
protected function prepareElementValidateCallbacks(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
$element['#element_validate'] = array_merge([
[
get_class($this),
'preValidateDate',
],
], $element['#element_validate']);
$element['#element_validate'][] = [
get_class($this),
'validateDate',
];
parent::prepareElementValidateCallbacks($element, $webform_submission);
}
/**
* {@inheritdoc}
*/
public function setDefaultValue(array &$element) {
if ($this
->hasMultipleValues($element)) {
$element['#default_value'] = isset($element['#default_value']) ? (array) $element['#default_value'] : NULL;
return;
}
// Datelist and Datetime require #default_value to be DrupalDateTime.
if (in_array($element['#type'], [
'datelist',
'datetime',
])) {
if (!empty($element['#default_value']) && is_string($element['#default_value'])) {
$element['#default_value'] = $element['#default_value'] ? DrupalDateTime::createFromTimestamp(strtotime($element['#default_value'])) : NULL;
}
}
}
/**
* After build handler for date elements.
*/
public static function afterBuild(array $element, FormStateInterface $form_state) {
// Add parent title to sub-elements to child elements which applies to
// datetime and datelist elements.
$child_keys = Element::children($element);
foreach ($child_keys as $child_key) {
if (isset($element[$child_key]['#title'])) {
$t_args = [
'@parent' => $element['#title'],
'@child' => $element[$child_key]['#title'],
];
$element[$child_key]['#title'] = t('@parent: @child', $t_args);
}
}
// Remove orphaned form label.
if ($child_keys) {
$element['#label_attributes']['webform-remove-for-attribute'] = TRUE;
}
return $element;
}
/****************************************************************************/
// Display submission value methods.
/****************************************************************************/
/**
* {@inheritdoc}
*/
protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
$timestamp = strtotime($value);
if (empty($timestamp)) {
return $value;
}
$format = $this
->getItemFormat($element);
if ($format === 'raw') {
return $value;
}
elseif (DateFormat::load($format)) {
return $this->dateFormatter
->format($timestamp, $format);
}
else {
return static::formatDate($format, $timestamp);
}
}
/**
* {@inheritdoc}
*/
public function getItemDefaultFormat() {
return 'fallback';
}
/**
* {@inheritdoc}
*/
public function getItemFormats() {
$formats = parent::getItemFormats();
$date_formats = DateFormat::loadMultiple();
foreach ($date_formats as $date_format) {
$formats[$date_format
->id()] = $date_format
->label();
}
// If a default format is defined update the fallback date formats label.
// @see \Drupal\webform\Plugin\WebformElementBase::getItemFormat
$default_format = $this->configFactory
->get('webform.settings')
->get('format.' . $this
->getPluginId() . '.item');
if ($default_format && isset($date_formats[$default_format])) {
$formats['fallback'] = $this
->t('Default date format (@label)', [
'@label' => $date_formats[$default_format]
->label(),
]);
}
return $formats;
}
/****************************************************************************/
// Export methods.
/****************************************************************************/
/**
* {@inheritdoc}
*/
public function buildExportRecord(array $element, WebformSubmissionInterface $webform_submission, array $export_options) {
$element['#format'] = $this
->getDateType($element) === 'datetime' ? 'Y-m-d H:i:s' : 'Y-m-d';
return [
$this
->formatText($element, $webform_submission, $export_options),
];
}
/****************************************************************************/
// Element configuration methods.
/****************************************************************************/
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
// Append supported date input format to #default_value description.
$form['default']['default_value']['#description'] .= '<br /><br />' . $this
->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 are all valid.');
// Append token date format to #default_value description.
$form['default']['default_value']['#description'] .= '<br /><br />' . $this
->t("You may use tokens. Tokens should use the 'html_date' or 'html_datetime' date format. (i.e. @date_format)", [
'@date_format' => '[current-user:field_date_of_birth:date:html_date]',
]);
// Allow custom date formats to be entered.
$form['display']['format']['#type'] = 'webform_select_other';
$form['display']['format']['#other__option_label'] = $this
->t('Custom date format…');
$form['display']['format']['#other__description'] = $this
->t('A user-defined date format. See the <a href="http://php.net/manual/function.date.php">PHP manual</a> for available options.');
$form['date'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Date settings'),
];
// Date min/max validation.
$form['date']['date_container'] = $this
->getFormInlineContainer() + [
'#weight' => 10,
];
$form['date']['date_container']['date_date_min'] = [
'#type' => 'textfield',
'#title' => $this
->t('Date minimum'),
'#description' => $this
->t('Specifies the minimum date.') . ' ' . $this
->t('To limit the minimum date to the submission date use the <code>[webform_submission:created:html_date]</code> token.') . '<br /><br />' . $this
->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 are all valid.'),
];
$form['date']['date_container']['date_date_max'] = [
'#type' => 'textfield',
'#title' => $this
->t('Date maximum'),
'#description' => $this
->t('Specifies the maximum date.') . ' ' . $this
->t('To limit the maximum date to the submission date use the <code>[webform_submission:created:html_date]</code> token.') . '<br /><br />' . $this
->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 are all valid.'),
];
// Date days of the week validation.
$form['date']['date_days'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Date days of the week'),
'#options' => DateHelper::weekDaysAbbr(TRUE),
'#element_validate' => [
[
'\\Drupal\\webform\\Utility\\WebformElementHelper',
'filterValues',
],
],
'#description' => $this
->t('Specifies the day(s) of the week.'),
'#options_display' => 'side_by_side',
'#required' => TRUE,
'#weight' => 20,
];
if ($this
->datePickerExists()) {
$form['date']['date_days']['#description'] .= ' ' . $this
->t('Please note, the date picker will disable unchecked days of the week.');
}
// Date/time min/max validation.
if ($this
->hasProperty('date_date_min') && $this
->hasProperty('date_time_min') && $this
->hasProperty('date_min')) {
$form['validation']['date_min_max_message'] = [
'#type' => 'webform_message',
'#message_type' => 'warning',
'#access' => TRUE,
'#message_message' => $this
->t("'Date/time' minimum or maximum should not be used with 'Date' or 'Time' specific minimum or maximum.") . '<br/>' . '<strong>' . $this
->t('This can cause unexpected validation errors.') . '</strong>',
'#message_close' => TRUE,
'#message_storage' => WebformMessageElement::STORAGE_SESSION,
'#states' => [
'visible' => [
[
':input[name="properties[date_date_min]"]' => [
'filled' => TRUE,
],
],
[
':input[name="properties[date_date_max]"]' => [
'filled' => TRUE,
],
],
[
':input[name="properties[date_time_min]"]' => [
'filled' => TRUE,
],
],
[
':input[name="properties[date_time_max]"]' => [
'filled' => TRUE,
],
],
],
],
];
}
$form['validation']['date_container'] = $this
->getFormInlineContainer();
$form['validation']['date_container']['date_min'] = [
'#type' => 'textfield',
'#title' => $this
->t('Date/time minimum'),
'#description' => $this
->t('Specifies the minimum date/time.') . ' ' . $this
->t('To limit the minimum date/time to the submission date/time use the <code>[webform_submission:created:html_datetime]</code> token.') . '<br /><br />' . $this
->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date/Time Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 10:00 PM are all valid.'),
];
$form['validation']['date_container']['date_max'] = [
'#type' => 'textfield',
'#title' => $this
->t('Date/time maximum'),
'#description' => $this
->t('Specifies the maximum date/time.') . ' ' . $this
->t('To limit the maximum date/time to the submission date/time use the <code>[webform_submission:created:html_datetime]</code> token.') . '<br /><br />' . $this
->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date/Time Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 10:00 PM are all valid.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$properties = $this
->getConfigurationFormProperties($form, $form_state);
// Validate #default_value GNU Date Input Format.
if (!$this
->validateGnuDateInputFormat($properties, '#default_value')) {
$this
->setGnuDateInputFormatError($form['properties']['default']['default_value'], $form_state);
}
// Validate #*_min and #*_max GNU Date Input Format.
if (!$this
->validateGnuDateInputFormat($properties, '#date_min')) {
$this
->setGnuDateInputFormatError($form['properties']['validation']['date_min'], $form_state);
}
if (!$this
->validateGnuDateInputFormat($properties, '#date_max')) {
$this
->setGnuDateInputFormatError($form['properties']['validation']['date_max'], $form_state);
}
if (!$this
->validateGnuDateInputFormat($properties, '#date_date_min')) {
$this
->setGnuDateInputFormatError($form['properties']['date']['date_date_min'], $form_state);
}
if (!$this
->validateGnuDateInputFormat($properties, '#date_date_max')) {
$this
->setGnuDateInputFormatError($form['properties']['date']['date_date_max'], $form_state);
}
parent::validateConfigurationForm($form, $form_state);
}
/**
* Get the type of date/time element.
*
* @param array $element
* An element.
*
* @return string
* The type of date/time element which be either a 'date' or 'datetime'.
*/
protected function getDateType(array $element) {
switch ($element['#type']) {
case 'datelist':
return isset($element['#date_part_order']) && !in_array('hour', $element['#date_part_order']) ? 'date' : 'datetime';
case 'datetime':
return 'datetime';
case 'date':
default:
return 'date';
}
}
/**
* Parse GNU Date Input Format.
*
* @param array $element
* An element.
* @param string $property
* The element's date property.
*/
protected function parseInputFormat(array &$element, $property) {
if (!isset($element[$property])) {
return;
}
elseif (is_array($element[$property])) {
foreach ($element[$property] as $key => $value) {
$timestamp = strtotime($value);
$element[$property][$key] = $timestamp ? $this->dateFormatter
->format($timestamp, 'html_' . $this
->getDateType($element)) : NULL;
}
}
else {
$timestamp = strtotime($element[$property]);
$element[$property] = $timestamp ? $this->dateFormatter
->format($timestamp, 'html_' . $this
->getDateType($element)) : NULL;
}
}
/****************************************************************************/
// Validation methods.
/****************************************************************************/
/**
* Validate GNU date input format.
*
* @param array $properties
* An array of element properties.
* @param string $key
* The property name containing the GNU date input format.
*
* @return bool
* TRUE if property's value is a valid GNU date input format or contains
* a token.
*/
protected function validateGnuDateInputFormat(array $properties, $key) {
if (empty($properties[$key])) {
return TRUE;
}
$values = (array) $properties[$key];
foreach ($values as $value) {
if (!preg_match('/^\\[[^]]+\\]$/', $value)) {
if (strtotime($value) === FALSE) {
return FALSE;
}
}
}
return TRUE;
}
/**
* Set GNU date input format error.
*
* @param array $element
* The property element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function setGnuDateInputFormatError(array $element, FormStateInterface $form_state) {
$t_args = [
'@title' => $element['#title'] ?: $element['#key'],
];
$form_state
->setError($element, $this
->t('The @title could not be interpreted in <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>.', $t_args));
}
/**
* Webform element pre validation handler for Date elements.
*/
public static function preValidateDate(&$element, FormStateInterface $form_state, &$complete_form) {
// ISSUE #2723159:
// Datetime form element cannot validate when using a
// format without seconds.
// WORKAROUND:
// Append the second format before the time element is validated.
//
// @see \Drupal\Core\Datetime\Element\Datetime::valueCallback
// @see https://www.drupal.org/node/2723159
if ($element['#type'] === 'datetime' && $element['#date_time_format'] === 'H:i' && strlen($element['#value']['time']) === 8) {
$element['#date_time_format'] = 'H:i:s';
}
// ISSUE:
// Date list in composite element is missing the date object.
//
// WORKAROUND:
// Manually set the date object.
$date_element_types = [
'datelist' => '\\Drupal\\Core\\Datetime\\Element\\Datelist',
'datetime' => '\\Drupal\\Core\\Datetime\\Element\\Datetime',
];
if (isset($date_element_types[$element['#type']])) {
$date_class = $date_element_types[$element['#type']];
$input_exists = FALSE;
$input = NestedArray::getValue($form_state
->getValues(), $element['#parents'], $input_exists);
if (!isset($input['object'])) {
// Time picker converts all submitted time values to H:i:s format.
// @see \Drupal\webform\Element\WebformTime::validateWebformTime
if (isset($element['#date_time_element']) && $element['#date_time_element'] === 'timepicker') {
$element['#date_time_format'] = 'H:i:s';
}
$input = $date_class::valueCallback($element, $input, $form_state);
$form_state
->setValueForElement($element, $input);
$element['#value'] = $input;
}
}
}
/**
* Webform element validation handler for date elements.
*
* Note that #required is validated by _form_validate() already.
*
* @see \Drupal\Core\Render\Element\Number::validateNumber
*/
public static function validateDate(&$element, FormStateInterface $form_state, &$complete_form) {
$value = $element['#value'];
$name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
$date_date_format = !empty($element['#date_date_format']) ? $element['#date_date_format'] : DateFormat::load('html_date')
->getPattern();
$date_time_format = !empty($element['#date_time_format']) ? $element['#date_time_format'] : DateFormat::load('html_time')
->getPattern();
// Convert DrupalDateTime array and object to ISO datetime.
if (is_array($value)) {
$value = $value['object'] ? $value['object']
->format(DateFormat::load('html_datetime')
->getPattern()) : '';
}
elseif ($value) {
$datetime = WebformDateHelper::createFromFormat($date_date_format, $value);
if ($datetime === FALSE || static::formatDate($date_date_format, $datetime
->getTimestamp()) !== $value) {
$form_state
->setError($element, t('%name must be a valid date.', [
'%name' => $name,
]));
$value = '';
}
else {
// Clear timestamp to date elements.
if ($element['#type'] === 'date') {
$datetime
->setTime(0, 0, 0);
$value = $datetime
->format(DateFormat::load('html_date')
->getPattern());
}
else {
$value = $datetime
->format(DateFormat::load('html_datetime')
->getPattern());
}
}
}
$form_state
->setValueForElement($element, $value);
if ($value === '') {
return;
}
$time = strtotime($value);
// Ensure that the input is greater than the #date_date_min property, if set.
if (isset($element['#date_date_min'])) {
$min = strtotime(static::formatDate('Y-m-d', strtotime($element['#date_date_min'])));
if ($time < $min) {
$form_state
->setError($element, t('%name must be on or after %min.', [
'%name' => $name,
'%min' => static::formatDate($date_date_format, $min),
]));
}
}
// Ensure that the input is less than the #date_date_max property, if set.
if (isset($element['#date_date_max'])) {
$max = strtotime(static::formatDate('Y-m-d 23:59:59', strtotime($element['#date_date_max'])));
if ($time > $max) {
$form_state
->setError($element, t('%name must be on or before %max.', [
'%name' => $name,
'%max' => static::formatDate($date_date_format, $max),
]));
}
}
// Ensure that the input is greater than the #date_min property, if set.
if (isset($element['#date_min'])) {
$min = strtotime($element['#date_min']);
if ($time < $min) {
$form_state
->setError($element, t('%name must be on or after %min.', [
'%name' => $name,
'%min' => static::formatDate($date_date_format, $min) . ' ' . static::formatDate($date_time_format, $min),
]));
}
}
// Ensure that the input is less than the #date_max property, if set.
if (isset($element['#date_max'])) {
$max = strtotime($element['#date_max']);
if ($time > $max) {
$form_state
->setError($element, t('%name must be on or before %max.', [
'%name' => $name,
'%max' => static::formatDate($date_date_format, $max) . ' ' . static::formatDate($date_time_format, $max),
]));
}
}
// Ensure that the input is a day of week.
if (!empty($element['#date_days'])) {
$days = $element['#date_days'];
$day = date('w', $time);
if (!in_array($day, $days)) {
$form_state
->setError($element, t('%name must be a %days.', [
'%name' => $name,
'%days' => WebformArrayHelper::toString(array_intersect_key(DateHelper::weekDays(TRUE), array_combine($days, $days)), t('or')),
]));
}
}
}
/**
* {@inheritdoc}
*/
public function getTestValues(array $element, WebformInterface $webform, array $options = []) {
$format = DateFormat::load('html_datetime')
->getPattern();
if (!empty($element['#date_year_range'])) {
list($min, $max) = static::datetimeRangeYears($element['#date_year_range']);
}
else {
$min = !empty($element['#date_date_min']) ? strtotime($element['#date_date_min']) : strtotime('-10 years');
$max = !empty($element['#date_date_max']) ? strtotime($element['#date_date_max']) : max($min, strtotime('+20 years') ?: PHP_INT_MAX);
}
return static::formatDate($format, rand($min, $max));
}
/**
* Specifies the start and end year to use as a date range.
*
* Copied from: DateElementBase::datetimeRangeYears.
*
* @param string $string
* A min and max year string like '-3:+1' or '2000:2010' or '2000:+3'.
* @param object $date
* (optional) A date object to test as a default value. Defaults to NULL.
*
* @return array
* A numerically indexed array, containing the minimum and maximum year
* described by this pattern.
*
* @see \Drupal\Core\Datetime\Element\DateElementBase::datetimeRangeYears
*/
protected static function datetimeRangeYears($string, $date = NULL) {
$datetime = new DrupalDateTime();
$this_year = $datetime
->format('Y');
list($min_year, $max_year) = explode(':', $string);
// Valid patterns would be -5:+5, 0:+1, 2008:2010.
$plus_pattern = '@[\\+|\\-][0-9]{1,4}@';
$year_pattern = '@^[0-9]{4}@';
if (!preg_match($year_pattern, $min_year, $matches)) {
if (preg_match($plus_pattern, $min_year, $matches)) {
$min_year = $this_year + $matches[0];
}
else {
$min_year = $this_year;
}
}
if (!preg_match($year_pattern, $max_year, $matches)) {
if (preg_match($plus_pattern, $max_year, $matches)) {
$max_year = $this_year + $matches[0];
}
else {
$max_year = $this_year;
}
}
// We expect the $min year to be less than the $max year. Some custom values
// for -99:+99 might not obey that.
if ($min_year > $max_year) {
$temp = $max_year;
$max_year = $min_year;
$min_year = $temp;
}
// If there is a current value, stretch the range to include it.
$value_year = $date instanceof DrupalDateTime ? $date
->format('Y') : '';
if (!empty($value_year)) {
$min_year = min($value_year, $min_year);
$max_year = max($value_year, $max_year);
}
return [
$min_year,
$max_year,
];
}
/**
* Format custom date.
*
* @param string $custom_format
* A PHP date format string suitable for input to date().
* @param int $timestamp
* (optional) A UNIX timestamp to format.
*
* @return string
* Formatted date.
*/
protected static function formatDate($custom_format, $timestamp = NULL) {
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
return $date_formatter
->format($timestamp ?: \Drupal::time()
->getRequestTime(), 'custom', $custom_format);
}
/**
* Determine if the the jQuery UI date picker is supported.
*
* @return bool
* TRUE if Drupal 8 or for Drupal 9 support the jQuery UI date picker
* module is installed.
*
* @see \webform_library_info_alter
*/
protected function datePickerExists() {
return floatval(\Drupal::VERSION) < 9 || $this->moduleHandler
->moduleExists('jquery_ui_datepicker');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DateBase:: |
protected | property | The date formatter service. | |
DateBase:: |
public static | function | After build handler for date elements. | 1 |
DateBase:: |
public | function |
Build an element's export row. Overrides WebformElementBase:: |
|
DateBase:: |
public static | function |
Creates an instance of the plugin. Overrides WebformElementBase:: |
|
DateBase:: |
protected | function | Determine if the the jQuery UI date picker is supported. | |
DateBase:: |
protected static | function | Specifies the start and end year to use as a date range. | |
DateBase:: |
protected | function |
Define an element's default properties. Overrides WebformElementBase:: |
3 |
DateBase:: |
public | function |
Gets the actual configuration webform array to be built. Overrides WebformElementBase:: |
3 |
DateBase:: |
protected static | function | Format custom date. | |
DateBase:: |
protected | function |
Format an element's value as text. Overrides WebformElementBase:: |
|
DateBase:: |
protected | function | Get the type of date/time element. | |
DateBase:: |
public | function |
Get an element's default single value format name. Overrides WebformElementBase:: |
|
DateBase:: |
public | function |
Get an element's available single value formats. Overrides WebformElementBase:: |
|
DateBase:: |
public | function |
Get test values for an element. Overrides WebformElementBase:: |
|
DateBase:: |
protected | function | Parse GNU Date Input Format. | |
DateBase:: |
public | function |
Prepare an element to be rendered within a webform. Overrides WebformElementBase:: |
3 |
DateBase:: |
protected | function |
Prepare an element's validation callbacks. Overrides WebformElementBase:: |
|
DateBase:: |
public static | function | Webform element pre validation handler for Date elements. | |
DateBase:: |
public | function |
Set an element's default value using saved data. Overrides WebformElementBase:: |
1 |
DateBase:: |
protected | function | Set GNU date input format error. | |
DateBase:: |
public | function |
Form validation handler. Overrides WebformElementBase:: |
1 |
DateBase:: |
public static | function | Webform element validation handler for date elements. | |
DateBase:: |
protected | function | Validate GNU date input format. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. | ||
WebformCompositeFormElementTrait:: |
public static | function | Adds form element theming to an element if its title or description is set. | 1 |
WebformElementBase:: |
protected | property | The configuration factory. | |
WebformElementBase:: |
protected | property | The current user. | |
WebformElementBase:: |
protected | property | An associative array of an element's default properties names and values. | |
WebformElementBase:: |
protected | property | A element info manager. | |
WebformElementBase:: |
protected | property | The webform element manager. | |
WebformElementBase:: |
protected | property |
The entity type manager. Overrides WebformEntityStorageTrait:: |
|
WebformElementBase:: |
protected | property | The webform libraries manager. | |
WebformElementBase:: |
protected | property | A logger instance. | |
WebformElementBase:: |
protected | property | The module handler. | |
WebformElementBase:: |
protected | property | The token manager. | |
WebformElementBase:: |
protected | property | An indexed array of an element's translated properties. | |
WebformElementBase:: |
public | function |
Alter an element's associated form. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
protected | function | Build an element as text or HTML. | 4 |
WebformElementBase:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
3 |
WebformElementBase:: |
protected | function | Build configuration form tabs. | 1 |
WebformElementBase:: |
public | function |
Build an element's export header. Overrides WebformElementInterface:: |
4 |
WebformElementBase:: |
public | function |
Get an element's export options webform. Overrides WebformElementInterface:: |
4 |
WebformElementBase:: |
public | function |
Build an element as HTML element. Overrides WebformElementInterface:: |
2 |
WebformElementBase:: |
public | function |
Build an element as text element. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
protected | function | Checks an access rule against a user account's roles and id. | |
WebformElementBase:: |
public | function |
Check element access (rules). Overrides WebformElementInterface:: |
|
WebformElementBase:: |
protected | function | Define default base properties used by all elements. | 4 |
WebformElementBase:: |
protected | function | Define default multiple properties used by most elements. | 1 |
WebformElementBase:: |
protected | function | Define an element's translatable properties. | 17 |
WebformElementBase:: |
public | function |
Display element disabled warning. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function |
Finalize an element to be rendered within a webform. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
protected | function | Format an element's value as HTML or plain text. | 2 |
WebformElementBase:: |
protected | function | Format an element's item using custom HTML or plain text. | 2 |
WebformElementBase:: |
protected | function | Format an element's items using custom HTML or plain text. | |
WebformElementBase:: |
public | function |
Format an element's value as HTML. Overrides WebformElementInterface:: |
2 |
WebformElementBase:: |
protected | function | Format an element's value as HTML. | 19 |
WebformElementBase:: |
protected | function | Format an element's items as HTML. | 2 |
WebformElementBase:: |
public | function |
Format an element's table column value. Overrides WebformElementInterface:: |
4 |
WebformElementBase:: |
public | function |
Format an element's value as plain text. Overrides WebformElementInterface:: |
2 |
WebformElementBase:: |
protected | function | Format an element's items as text. | 2 |
WebformElementBase:: |
public | function |
Get an element's admin label (#admin_title, #title or #webform_key). Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Get an associative array of element properties from configuration webform. Overrides WebformElementInterface:: |
3 |
WebformElementBase:: |
protected | function | Get configuration property value. | 1 |
WebformElementBase:: |
public | function |
Gets the element's default key. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function |
Get default properties. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Get an element's default property value. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
protected | function | Get a render element's default property. | |
WebformElementBase:: |
public | function |
Get an element's property value. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
protected | function | Get an element's (sub)inputs selectors as options. | 9 |
WebformElementBase:: |
public | function |
Get an element's (sub)input selector value. Overrides WebformElementInterface:: |
5 |
WebformElementBase:: |
public | function |
Get an element's selectors as options. Overrides WebformElementInterface:: |
11 |
WebformElementBase:: |
public | function |
Get an element's selectors source values. Overrides WebformElementInterface:: |
3 |
WebformElementBase:: |
public | function |
Get an element's supported states as options. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function |
Get an element's default export options. Overrides WebformElementInterface:: |
5 |
WebformElementBase:: |
public | function |
Get the Webform element's form element class definition. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
protected | function | Get form--inline container which is used for side-by-side element layout. | |
WebformElementBase:: |
public | function |
Retrieves the default properties for the defined element type. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Get element's single value format name by looking for '#format' property, global settings, and finally default settings. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function |
Get an element's default multiple value format name. Overrides WebformElementInterface:: |
2 |
WebformElementBase:: |
public | function |
Get element's multiple value format name by looking for '#format' property, global settings, and finally default settings. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Get an element's available multiple value formats. Overrides WebformElementInterface:: |
2 |
WebformElementBase:: |
public | function |
Get an element's key/name. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Get an element's label (#title or #webform_key). Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Get configuration form's off-canvas width. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function |
Get link to element's API documentation. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Get the URL for the element's API documentation. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Gets the category of the plugin instance. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Gets the description of the plugin instance. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Gets the label of the plugin instance. Overrides WebformElementInterface:: |
3 |
WebformElementBase:: |
public | function |
Get an element's submission raw value. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Get related element types. Overrides WebformElementInterface:: |
6 |
WebformElementBase:: |
public | function |
Get element's table column(s) settings. Overrides WebformElementInterface:: |
4 |
WebformElementBase:: |
public | function |
Get translatable properties. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Gets the type name (aka id) of the plugin instance with the 'webform_' prefix. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Get an element's submission value. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
protected | function | Determine if the element has a composite field wrapper. | |
WebformElementBase:: |
public | function |
Determine if the element is or includes a managed_file upload element. Overrides WebformElementInterface:: |
2 |
WebformElementBase:: |
public | function |
Checks if the element value has multiple values. Overrides WebformElementInterface:: |
6 |
WebformElementBase:: |
public | function |
Checks if the element uses the 'webform_multiple' element. Overrides WebformElementInterface:: |
3 |
WebformElementBase:: |
public | function |
Determine if the element supports a specified property. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Determine if an element's has a submission value. Overrides WebformElementInterface:: |
2 |
WebformElementBase:: |
public | function |
Checks if the element has a wrapper. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public static | function | Webform element #after_build callback. | |
WebformElementBase:: |
public | function |
Initialize an element to be displayed, rendered, or exported. Overrides WebformElementInterface:: |
8 |
WebformElementBase:: |
public | function |
Checks if the element is a composite element. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Checks if the element is a container that can contain elements. Overrides WebformElementInterface:: |
9 |
WebformElementBase:: |
public | function |
Checks if the element is disabled. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Checks if an empty element is excluded. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function |
Checks if the element is enabled. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function |
Checks if the element is excluded via webform.settings. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
public | function |
Checks if the element is hidden. Overrides WebformElementInterface:: |
2 |
WebformElementBase:: |
public | function |
Checks if the element carries a value. Overrides WebformElementInterface:: |
11 |
WebformElementBase:: |
public | function |
Checks if the element value could contain multiple lines. Overrides WebformElementInterface:: |
2 |
WebformElementBase:: |
public | function |
Checks if the element is a root element. Overrides WebformElementInterface:: |
3 |
WebformElementBase:: |
public | function |
Acts on a webform submission element after it is created. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function |
Delete any additional value associated with an element. Overrides WebformElementInterface:: |
5 |
WebformElementBase:: |
public | function |
Acts on loaded webform submission. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function |
Acts on a saved webform submission element before the insert or update hook is invoked. Overrides WebformElementInterface:: |
5 |
WebformElementBase:: |
public | function |
Changes the values of an entity before it is created. Overrides WebformElementInterface:: |
1 |
WebformElementBase:: |
public | function | 1 | |
WebformElementBase:: |
protected | function | Prefix an element's export header. | |
WebformElementBase:: |
protected | function | Replace Core's composite #pre_render with Webform's composite #pre_render. | |
WebformElementBase:: |
protected | function | Prepare an element's pre render callbacks. | 3 |
WebformElementBase:: |
protected | function | Set multiple element wrapper. | 1 |
WebformElementBase:: |
protected | function | Set an elements #states and flexbox wrapper. | 1 |
WebformElementBase:: |
public static | function | Fix flexbox wrapper. | |
WebformElementBase:: |
public static | function | Fix state wrapper. | |
WebformElementBase:: |
public | function |
Acts on a webform submission element before the presave hook is invoked. Overrides WebformElementInterface:: |
4 |
WebformElementBase:: |
public | function |
Generate a renderable preview of the element. Overrides WebformElementInterface:: |
37 |
WebformElementBase:: |
public | function |
Replace tokens for all element properties. Overrides WebformElementInterface:: |
|
WebformElementBase:: |
protected | function | Set an element's configuration webform element default value. | 2 |
WebformElementBase:: |
protected | function | Set configuration webform default values recursively. | |
WebformElementBase:: |
protected | function | Set element's default callback. | |
WebformElementBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
WebformElementBase:: |
public | function |
Checks if the element supports multiple values. Overrides WebformElementInterface:: |
8 |
WebformElementBase:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
1 |
WebformElementBase:: |
public static | function | Form API callback. Validate element #minlength value. | |
WebformElementBase:: |
public static | function | Form API callback. Validate element #multiple > 1 value. | |
WebformElementBase:: |
public static | function | Form API callback. Validate element #unique value. | |
WebformElementBase:: |
public static | function | Form API callback. Validate element #unique multiple values. | |
WebformEntityInjectionTrait:: |
protected | property | The webform. | 1 |
WebformEntityInjectionTrait:: |
protected | property | The webform submission. | 1 |
WebformEntityInjectionTrait:: |
public | function | Get the webform that this handler is attached to. | |
WebformEntityInjectionTrait:: |
public | function | Set webform and webform submission entity. | |
WebformEntityInjectionTrait:: |
public | function | Reset webform and webform submission entity. | |
WebformEntityInjectionTrait:: |
public | function | ||
WebformEntityInjectionTrait:: |
public | function | Set the webform that this is handler is attached to. | |
WebformEntityInjectionTrait:: |
public | function | Get the webform submission that this handler is handling. | |
WebformEntityStorageTrait:: |
protected | property | An associate array of entity type storage aliases. | |
WebformEntityStorageTrait:: |
protected | function | Retrieves the entity storage. | |
WebformEntityStorageTrait:: |
protected | function | Retrieves the webform submission storage. | |
WebformEntityStorageTrait:: |
protected | function | Retrieves the webform storage. | |
WebformEntityStorageTrait:: |
public | function | Implements the magic __get() method. |