You are here

public function SmartDateWidgetBase::massageFormValues in Smart Date 3.3.x

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. 8 src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  3. 3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  4. 3.0.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  5. 3.1.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::massageFormValues()
  6. 3.2.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

1 call to SmartDateWidgetBase::massageFormValues()
SmartDateInlineWidget::massageFormValues in src/Plugin/Field/FieldWidget/SmartDateInlineWidget.php
Massages the form values into the format expected for field values.
1 method overrides SmartDateWidgetBase::massageFormValues()
SmartDateInlineWidget::massageFormValues in src/Plugin/Field/FieldWidget/SmartDateInlineWidget.php
Massages the form values into the format expected for field values.

File

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

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 (!isset($item['storage']) || $item['storage'] != 'smartdate') {

      // Use the processing from core's Datetime Range.
      $core_range = new DateRangeWidgetBase($this
        ->getPluginId(), $this
        ->getPluginDefinition(), $this->fieldDefinition, $this
        ->getSettings(), $this->thirdPartySettings);
      $values = $core_range
        ->massageFormValues($values, $form, $form_state);
      return $values;
    }
    $timezone = NULL;
    if (!empty($item['timezone'])) {
      $timezone = new \DateTimezone($item['timezone']);
    }
    if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) {

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

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

      // If using a custom duration, calculate based on start and end times.
      if (!empty($item['end_value']) && !empty($item['value'])) {
        $item['duration'] = (int) ((int) $item['end_value'] - (int) $item['value']) / 60;
      }
    }
  }
  if (!$form_state
    ->isValidationComplete()) {

    // Make sure we only process once, after validation.
    return $values;
  }

  // Skip any additional processing if the field doesn't allow recurring.
  $field_def = $this->fieldDefinition;
  if ($field_def instanceof FieldConfigInterface) {
    $allow_recurring = $field_def
      ->getThirdPartySetting('smart_date_recur', 'allow_recurring');
  }
  elseif ($field_def instanceof BaseFieldDefinition) {

    // TODO: Document that for custom entities, you must enable recurring
    // functionality by adding ->setSetting('allow_recurring', TRUE)
    // to your field definition.
    $allow_recurring = $field_def
      ->getSetting('allow_recurring');
  }
  else {

    // Not sure what other method we can provide to define this.
    $allow_recurring = FALSE;
  }
  if ($allow_recurring && function_exists('smart_date_recur_widget_extra_fields')) {

    // Provide extra parameters to be stored with the recurrence rule.
    $month_limit = SmartDateRule::getMonthsLimit($field_def);
    if ($form_state
      ->getFormObject() instanceof EntityFormInterface) {
      $entity = $form_state
        ->getformObject()
        ->getEntity();
      $entity_type = $entity
        ->getEntityTypeId();
      $bundle = $entity
        ->bundle();
    }
    $field_name = $field_def
      ->getName();
    smart_date_recur_generate_rows($values, $entity_type, $bundle, $field_name, $month_limit);
  }
  return $values;
}