/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); $default_value = isset($items[$delta]->value) ? DrupalDateTime::createFromTimestamp($items[$delta]->value) : ''; $element['value'] = $element + array('#type' => 'datetime', '#default_value' => $default_value, '#date_year_range' => '1902:2037'); $element['value']['#description'] = $this->t('Format: %format. Leave blank to use the time of form submission.', array('%format' => Datetime::formatExample($date_format . ' ' . $time_format))); return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $element = parent::formElement($items, $delta, $element, $form, $form_state); // Remove 'Leave blank to use the time of form submission' which is in the // #description inherited from TimestampDatetimeWidget. The text here is not // used because it is entirely replaced in scheduler_form_node_form_alter() // However the widget is generic and may be used elsewhere in future. $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); $element['value']['#description'] = $this->t('Format: %format. Leave blank for no date.', array('%format' => Datetime::formatExample($date_format . ' ' . $time_format))); // Set the callback function to allow interception of the submitted user // input and add the default time if needed. It is too late to try this in // function massageFormValues as the validation has already been done. $element['value']['#value_callback'] = array($this, 'valueCallback'); return $element; }