You are here

protected function DateRecurModularOscarWidget::getFieldMonthlyByDayOrdinals 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::getFieldMonthlyByDayOrdinals()
  2. 2.x src/Plugin/Field/FieldWidget/DateRecurModularOscarWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularOscarWidget::getFieldMonthlyByDayOrdinals()

Ordinals (BYSETPOS).

Designed for MONTHLY combined with BYDAY.

1 call to DateRecurModularOscarWidget::getFieldMonthlyByDayOrdinals()
DateRecurModularOscarWidget::formElement in src/Plugin/Field/FieldWidget/DateRecurModularOscarWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/DateRecurModularOscarWidget.php, line 548

Class

DateRecurModularOscarWidget
Date recur opening hours widget.

Namespace

Drupal\date_recur_modular\Plugin\Field\FieldWidget

Code

protected function getFieldMonthlyByDayOrdinals($element, ?DateRecurRuleInterface $rule) : array {
  $parts = $rule ? $rule
    ->getParts() : [];
  $ordinals = [];
  $bySetPos = !empty($parts['BYSETPOS']) ? explode(',', $parts['BYSETPOS']) : [];
  if (count($bySetPos) > 0) {
    $weekdayCount = count($element['weekdays']['#default_value']);
    sort($bySetPos);

    // Collapse all ordinals into simplified ordinals.
    $chunks = array_chunk($bySetPos, $weekdayCount);
    foreach ($chunks as $chunk) {
      $first = reset($chunk);
      $end = $first < 0 ? min($chunk) : max($chunk);
      $ordinals[] = $end / $weekdayCount;
    }
  }
  return [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Ordinals'),
    '#options' => [
      1 => $this
        ->t('First'),
      2 => $this
        ->t('Second'),
      3 => $this
        ->t('Third'),
      4 => $this
        ->t('Fourth'),
      5 => $this
        ->t('Fifth'),
      -1 => $this
        ->t('Last'),
      -2 => $this
        ->t('2nd to last'),
    ],
    '#default_value' => $ordinals,
  ];
}