You are here

protected function DateRecurModularWidgetFieldsTrait::getFieldByDay in Recurring Date Field Modular Widgets 2.x

Same name and namespace in other branches
  1. 8 src/DateRecurModularWidgetFieldsTrait.php \Drupal\date_recur_modular\DateRecurModularWidgetFieldsTrait::getFieldByDay()
  2. 3.x src/DateRecurModularWidgetFieldsTrait.php \Drupal\date_recur_modular\DateRecurModularWidgetFieldsTrait::getFieldByDay()

Get a BYDAY element.

Parameters

\Drupal\date_recur\DateRecurRuleInterface|null $rule: Optional rule for which default value is derived.

string $weekDayLabels: Specify length of weekday labels. Possible values:

  • 'full': Full weekday label.
  • 'abbreviated': Three character weekday label.

Return value

array A render array.

3 calls to DateRecurModularWidgetFieldsTrait::getFieldByDay()
DateRecurModularAlphaWidget::formElement in src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php
Returns the form for a single field widget.
DateRecurModularOscarWidget::formElement in src/Plugin/Field/FieldWidget/DateRecurModularOscarWidget.php
Returns the form for a single field widget.
DateRecurModularSierraModalForm::buildForm in src/Form/DateRecurModularSierraModalForm.php
Form constructor.

File

src/DateRecurModularWidgetFieldsTrait.php, line 72

Class

DateRecurModularWidgetFieldsTrait
Trait containing convenience methods for generating whole form fields.

Namespace

Drupal\date_recur_modular

Code

protected function getFieldByDay(?DateRecurRuleInterface $rule, string $weekDayLabels = 'full') : array {
  assert($this->configFactory instanceof ConfigFactoryInterface);
  $parts = $rule ? $rule
    ->getParts() : [];
  $weekdaysKeys = [
    'SU',
    'MO',
    'TU',
    'WE',
    'TH',
    'FR',
    'SA',
  ];
  $weekdayLabels = $weekDayLabels === 'full' ? DateHelper::weekDays(TRUE) : ($weekDayLabels === 'abbreviated' ? DateHelper::weekDaysAbbr(TRUE) : []);
  $weekdays = array_combine($weekdaysKeys, $weekdayLabels);

  // Weekday int. 0-6 (Sun-Sat).
  $firstDayInt = $this->configFactory
    ->get('system.date')
    ->get('first_day');

  // Rebuild weekday options where system first day is first option in list.
  $weekdayOptions = array_merge(array_slice($weekdays, $firstDayInt), array_slice($weekdays, 0, $firstDayInt));
  $weekDayDefault = isset($parts['BYDAY']) ? explode(',', $parts['BYDAY']) : [];
  return [
    '#type' => 'checkboxes',
    '#title_display' => 'invisible',
    '#title' => $this
      ->t('Weekdays'),
    '#options' => $weekdayOptions,
    '#default_value' => $weekDayDefault,
  ];
}