You are here

public function WebformEntityReferenceWidgetTrait::massageFormValues in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/Field/FieldWidget/WebformEntityReferenceWidgetTrait.php \Drupal\webform\Plugin\Field\FieldWidget\WebformEntityReferenceWidgetTrait::massageFormValues()

File

src/Plugin/Field/FieldWidget/WebformEntityReferenceWidgetTrait.php, line 296

Class

WebformEntityReferenceWidgetTrait
Trait for webform entity reference and autocomplete widget.

Namespace

Drupal\webform\Plugin\Field\FieldWidget

Code

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

  // Massage open/close dates.
  // @see \Drupal\webform\WebformEntitySettingsForm::save
  // @see \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase::massageFormValues
  foreach ($values as &$item) {
    $item += $item['settings'];
    unset($item['settings']);

    // Set default values.
    $item += [
      'target_id' => '',
      'default_data' => NULL,
      'status' => '',
      'open' => '',
      'close' => '',
    ];
    if ($item['status'] === WebformInterface::STATUS_SCHEDULED) {
      $states = [
        'open',
        'close',
      ];
      foreach ($states as $state) {
        if (!empty($item['scheduled'][$state]) && $item['scheduled'][$state] instanceof DrupalDateTime) {
          $item[$state] = WebformDateHelper::formatStorage($item['scheduled'][$state]);
        }
        else {
          $item[$state] = '';
        }
      }
    }
    else {
      $item['open'] = '';
      $item['close'] = '';
    }
    unset($item['scheduled']);
  }
  return $values;
}