You are here

public function DateRecurModularAlphaWidget::massageFormValues in Recurring Date Field Modular Widgets 8

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularAlphaWidget::massageFormValues()
  2. 2.x src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularAlphaWidget::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 DateRecurModularWidgetBase::massageFormValues

File

src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php, line 352

Class

DateRecurModularAlphaWidget
Date recur alpha widget.

Namespace

Drupal\date_recur_modular\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
  $values = parent::massageFormValues($values, $form, $form_state);
  $dateStorageFormat = $this->fieldDefinition
    ->getSetting('datetime_type') == DateRecurItem::DATETIME_TYPE_DATE ? DateRecurItem::DATE_STORAGE_FORMAT : DateRecurItem::DATETIME_STORAGE_FORMAT;
  $dateStorageTimeZone = new \DateTimezone(DateRecurItem::STORAGE_TIMEZONE);
  $grid = $this->partGrid;
  $returnValues = [];
  foreach ($values as $delta => $value) {

    // Call to parent invalidates and empties individual values.
    if (empty($value)) {
      continue;
    }
    $item = [];
    $start = $value['start'] ?? NULL;
    assert(!isset($start) || $start instanceof DrupalDateTime);
    $end = $value['end'] ?? NULL;
    assert(!isset($end) || $end instanceof DrupalDateTime);
    $timeZone = $value['time_zone'];
    assert(is_string($timeZone));
    $mode = $value['mode'] ?? NULL;
    $endsMode = $value['ends_mode'] ?? NULL;

    /** @var \Drupal\Core\Datetime\DrupalDateTime|array|null $endsDate */
    $endsDate = $value['ends_date'] ?? NULL;

    // Adjust the date for storage.
    $start
      ->setTimezone($dateStorageTimeZone);
    $item['value'] = $start
      ->format($dateStorageFormat);
    $end
      ->setTimezone($dateStorageTimeZone);
    $item['end_value'] = $end
      ->format($dateStorageFormat);
    $item['timezone'] = $timeZone;
    $weekDays = array_values(array_filter($value['weekdays']));
    $byDayStr = implode(',', $weekDays);
    $rule = [];
    if ($mode === static::MODE_MULTIDAY) {
      $rule['FREQ'] = 'DAILY';
      $rule['INTERVAL'] = 1;
      $rule['COUNT'] = $value['daily_count'];
    }
    elseif ($mode === static::MODE_WEEKLY) {
      $rule['FREQ'] = 'WEEKLY';
      $rule['INTERVAL'] = 1;
      $rule['BYDAY'] = $byDayStr;
    }
    elseif ($mode === static::MODE_FORTNIGHTLY) {
      $rule['FREQ'] = 'WEEKLY';
      $rule['INTERVAL'] = 2;
      $rule['BYDAY'] = $byDayStr;
    }
    elseif ($mode === static::MODE_MONTHLY) {
      $rule['FREQ'] = 'MONTHLY';
      $rule['INTERVAL'] = 1;
      $rule['BYDAY'] = $byDayStr;

      // Funge ordinals appropriately.
      $ordinalCheckboxes = array_filter($value['ordinals']);
      $ordinals = [];
      if (count($ordinalCheckboxes) && count($weekDays)) {
        $weekdayCount = count($weekDays);

        // Expand simplified ordinals into spec compliant BYSETPOS ordinals.
        foreach ($ordinalCheckboxes as $ordinal) {
          $end = $ordinal * $weekdayCount;
          $diff = $weekdayCount - 1;
          $start = $end > 0 ? $end - $diff : $end + $diff;
          $range = range($start, $end);
          array_push($ordinals, ...$range);
        }

        // Order doesn't matter but simplifies testing.
        sort($ordinals);
        $rule['BYSETPOS'] = implode(',', $ordinals);
      }
    }

    // Ends mode.
    if ($endsMode === DateRecurModularWidgetOptions::ENDS_MODE_OCCURRENCES && $mode !== static::MODE_MULTIDAY) {
      $rule['COUNT'] = (int) $value['ends_count'];
    }
    elseif ($endsMode === DateRecurModularWidgetOptions::ENDS_MODE_ON_DATE && $endsDate instanceof DrupalDateTime) {
      $endsDateUtcAdjusted = (clone $endsDate)
        ->setTimezone(new \DateTimeZone('UTC'));
      $rule['UNTIL'] = $endsDateUtcAdjusted
        ->format('Ymd\\THis\\Z');
    }
    if (isset($rule['FREQ'])) {
      $rule = array_filter($rule);
      $item['rrule'] = $this
        ->buildRruleString($rule, $grid);
    }
    $returnValues[] = $item;
  }
  return $returnValues;
}