You are here

public function SmartDateWidgetBase::massageFormValues in Smart Date 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  2. 3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  3. 3.0.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  4. 3.1.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  5. 3.2.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  6. 3.3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  7. 3.4.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::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 DateTimeWidgetBase::massageFormValues

File

src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php, line 98

Class

SmartDateWidgetBase
Base class for the 'smartdate_*' widgets.

Namespace

Drupal\smart_date\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 timestamp.
  foreach ($values as &$item) {
    if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) {

      /** @var \Drupal\Core\Datetime\DrupalDateTime $start_time */
      $start_time = $item['value'];

      // Adjust the date for storage.
      $item['value'] = $start_time
        ->getTimestamp();
    }
    if (!empty($item['end_value']) && $item['end_value'] instanceof DrupalDateTime) {

      /** @var \Drupal\Core\Datetime\DrupalDateTime $end_time */
      $end_time = $item['end_value'];

      // Adjust the date for storage.
      $item['end_value'] = $end_time
        ->getTimestamp();
    }
    if ($item['duration'] == 'custom') {

      // If using a custom duration, calculate based on start and end times.
      if (isset($start_time) && isset($end_time) && $start_time instanceof DrupalDateTime && $end_time instanceof DrupalDateTime) {
        $item['duration'] = (int) ($item['end_value'] - $item['value']) / 60;
      }
    }
  }
  return $values;
}