You are here

public function DateTimeDayWidgetBase::massageFormValues in Date time day 8

Massages the form values into the format expected for field values.

Parameters

array $values: The submitted form values produced by the widget.

  • If the widget does not manage multiple values itself, the array holds the values generated by the multiple copies of the $element generated by the formElement() method, keyed by delta.
  • If the widget manages multiple values, the array holds the values of the form element generated by the formElement() method.

array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array An array of field values, keyed by delta.

Overrides DateTimeWidgetBase::massageFormValues

File

src/Plugin/Field/FieldWidget/DateTimeDayWidgetBase.php, line 60

Class

DateTimeDayWidgetBase
Base class for the 'datetimeday_*' widgets.

Namespace

Drupal\date_time_day\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

  // The widget form element type has transformed the value to a
  // DrupalDateTime object at this point. We need to convert it back to the
  // storage timezone and format.
  foreach ($values as &$item) {
    if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) {

      /** @var \Drupal\Core\Datetime\DrupalDateTime $value_date */
      $value_date = $item['value'];
      $value_format = DateTimeItemInterface::DATE_STORAGE_FORMAT;

      // Adjust the date for storage.
      $value_date
        ->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
      $item['value'] = $value_date
        ->format($value_format);
    }
    if (!empty($item['start_time_value']) && $item['start_time_value'] instanceof DrupalDateTime) {

      /** @var \Drupal\Core\Datetime\DrupalDateTime $start_time_date */
      $start_time_date = $item['start_time_value'];
      $start_time_format = '';
      switch ($this
        ->getFieldSetting('time_type')) {
        case DateTimeDayItem::DATEDAY_TIME_DEFAULT_TYPE_FORMAT:
          $start_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_FORMAT_STORAGE_FORMAT;
          break;
        case DateTimeDayItem::DATEDAY_TIME_TYPE_SECONDS_FORMAT:
          $start_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;
          break;
        default:
          $start_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;
          break;
      }

      // Adjust the date for storage.
      $start_time_date
        ->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
      $item['start_time_value'] = $start_time_date
        ->format($start_time_format);
    }
    if (!empty($item['end_time_value']) && $item['end_time_value'] instanceof DrupalDateTime) {

      /** @var \Drupal\Core\Datetime\DrupalDateTime $end_time_date */
      $end_time_date = $item['end_time_value'];
      $end_time_format = '';
      switch ($this
        ->getFieldSetting('time_type')) {
        case DateTimeDayItem::DATEDAY_TIME_DEFAULT_TYPE_FORMAT:
          $end_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_FORMAT_STORAGE_FORMAT;
          break;
        case DateTimeDayItem::DATEDAY_TIME_TYPE_SECONDS_FORMAT:
          $end_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;
          break;
        default:
          $end_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;
          break;
      }

      // Adjust the date for storage.
      $end_time_date
        ->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
      $item['end_time_value'] = $end_time_date
        ->format($end_time_format);
    }
  }
  return $values;
}