View source
<?php
declare (strict_types=1);
namespace Drupal\date_recur_modular;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DateHelper;
use Drupal\date_recur\DateRecurRuleInterface;
use Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem;
trait DateRecurModularWidgetFieldsTrait {
protected function getFieldTimeZone(?string $timeZone) : array {
$zones = $this
->getTimeZoneOptions();
return [
'#type' => 'select',
'#title' => $this
->t('Time zone'),
'#default_value' => $timeZone,
'#options' => $zones,
];
}
protected function getFieldMonth(?DateRecurRuleInterface $rule) : array {
$parts = $rule ? $rule
->getParts() : [];
$monthOptions = DateHelper::monthNames(TRUE);
$monthDefault = isset($parts['BYMONTH']) ? explode(',', $parts['BYMONTH']) : [];
return [
'#type' => 'checkboxes',
'#title' => $this
->t('Months'),
'#title_display' => 'invisible',
'#options' => $monthOptions,
'#default_value' => $monthDefault,
];
}
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);
$firstDayInt = $this->configFactory
->get('system.date')
->get('first_day');
$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,
];
}
protected function getFieldMode(DateRecurItem $item) : array {
$modes = $this
->getModes();
return [
'#type' => 'select',
'#title' => $this
->t('Mode'),
'#options' => $modes,
'#default_value' => $this
->getMode($item),
'#access' => count($modes) > 0,
];
}
protected function getFieldEndsMode() : array {
return [
'#type' => 'radios',
'#title' => $this
->t('Ends'),
'#options' => [
DateRecurModularWidgetOptions::ENDS_MODE_INFINITE => $this
->t('Never'),
DateRecurModularWidgetOptions::ENDS_MODE_OCCURRENCES => $this
->t('After number of occurrences'),
DateRecurModularWidgetOptions::ENDS_MODE_ON_DATE => $this
->t('On date'),
],
];
}
protected function getVisibilityStates(array $element, array $modes) : array {
$modeFieldName = $this
->getName($element, [
'mode',
]);
$conditions = [];
foreach ($modes as $mode) {
if (count($conditions) > 0) {
$conditions[] = 'or';
}
$conditions[] = [
':input[name="' . $modeFieldName . '"]' => [
'value' => $mode,
],
];
}
return [
'visible' => $conditions,
];
}
}