You are here

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

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

Class

DateRecurModularOscarWidget
Date recur opening hours widget.

Namespace

Drupal\date_recur_modular\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
  $values = array_map(function (array $value) : array {

    // If each of start/end/timezone/zone contain invalid values, quit here.
    // Validation errors will show on form. Notably start and end day are
    // malformed arrays thanks to 'datetime' element.

    /** @var \Drupal\Core\Datetime\DrupalDateTime|null $start */
    $start = $value['times']['time_start'] ?? NULL;

    /** @var \Drupal\Core\Datetime\DrupalDateTime|null $end */
    $end = $value['times']['time_end'] ?? NULL;
    $timeZone = $value['time_zone'] ?? NULL;
    $mode = $value['mode'] ?? NULL;
    if (!$start instanceof DrupalDateTime || !$end instanceof DrupalDateTime || !is_string($timeZone) || !is_string($mode)) {
      return [];
    }
    return $value;
  }, $values);
  $dateStorageFormat = $this->fieldDefinition
    ->getSetting('datetime_type') == DateRecurItem::DATETIME_TYPE_DATE ? DateRecurItem::DATE_STORAGE_FORMAT : DateRecurItem::DATETIME_STORAGE_FORMAT;
  $dateStorageTimeZone = new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE);
  $grid = $this->partGrid;
  $returnValues = [];
  foreach ($values as $delta => $value) {
    $returnValues[$delta] = [];

    // Value may have been emptied by start/end/tz/mode validation above.
    if (empty($value)) {
      continue;
    }
    $item = [];
    $start = $value['times']['time_start'] ?? NULL;
    assert(!isset($start) || $start instanceof DrupalDateTime);
    $end = $value['times']['time_end'] ?? NULL;
    assert(!isset($end) || $end instanceof DrupalDateTime);
    $timeZone = $value['time_zone'] ?? NULL;
    $mode = $value['mode'] ?? 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);
      }
    }
    if (isset($rule['FREQ'])) {
      $rule = array_filter($rule);
      $item['rrule'] = $this
        ->buildRruleString($rule, $grid);
    }
    $returnValues[$delta] = $item;
  }
  return $returnValues;
}