You are here

public function OpignoDateRangeWidget::massageFormValues in Opigno calendar 8

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldWidget/OpignoDateRangeWidget.php \Drupal\opigno_calendar\Plugin\Field\FieldWidget\OpignoDateRangeWidget::massageFormValues()

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 WidgetBase::massageFormValues

File

src/Plugin/Field/FieldWidget/OpignoDateRangeWidget.php, line 273

Class

OpignoDateRangeWidget
Plugin implementation of the 'opigno_daterange' widget.

Namespace

Drupal\opigno_calendar\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
  if (!empty($form_state
    ->getErrors())) {
    return $values;
  }
  $storage_timezone = new \DateTimeZone('UTC');
  $storage_format = 'Y-m-d\\TH:i:s';
  $pattern = $this
    ->getDatePattern();
  foreach ($values as &$item) {
    if (!empty($item['value_wrapper']['date'])) {
      $date = static::createDateTimeFromWrapper($item['value_wrapper'], $pattern);
      $item['value'] = $date
        ->setTimezone($storage_timezone)
        ->format($storage_format);
      unset($item['value_wrapper']);
    }
    if (!empty($item['end_value_wrapper']['date'])) {
      $end_date = static::createDateTimeFromWrapper($item['end_value_wrapper'], $pattern);
      $item['end_value'] = $end_date
        ->setTimezone($storage_timezone)
        ->format($storage_format);
      unset($item['end_value_wrapper']);
    }
  }
  return $values;
}