protected function DateRecurModularAlphaWidget::getFieldMonthlyByDayOrdinals in Recurring Date Field Modular Widgets 3.x
Same name and namespace in other branches
- 8 src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularAlphaWidget::getFieldMonthlyByDayOrdinals()
- 2.x src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularAlphaWidget::getFieldMonthlyByDayOrdinals()
Ordinals (BYSETPOS).
Designed for MONTHLY combined with BYDAY.
Parameters
array $element: The currently built element.
\Drupal\date_recur\DateRecurRuleInterface|null $rule: Optional rule for which default value is derived.
Return value
array A render array.
1 call to DateRecurModularAlphaWidget::getFieldMonthlyByDayOrdinals()
- DateRecurModularAlphaWidget::formElement in src/
Plugin/ Field/ FieldWidget/ DateRecurModularAlphaWidget.php - Returns the form for a single field widget.
File
- src/
Plugin/ Field/ FieldWidget/ DateRecurModularAlphaWidget.php, line 464
Class
- DateRecurModularAlphaWidget
- Date recur alpha widget.
Namespace
Drupal\date_recur_modular\Plugin\Field\FieldWidgetCode
protected function getFieldMonthlyByDayOrdinals(array $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,
];
}