function smart_date_recur_widget_extra_fields in Smart Date 3.3.x
Same name and namespace in other branches
- 8.2 modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
- 3.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
- 3.0.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
- 3.1.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
- 3.2.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
- 3.4.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
Helper function to add extra fields to Smart Date widgets.
1 call to smart_date_recur_widget_extra_fields()
- SmartDateWidgetBase::formElement in src/
Plugin/ Field/ FieldWidget/ SmartDateWidgetBase.php - Returns the form for a single field widget.
2 string references to 'smart_date_recur_widget_extra_fields'
- SmartDateWidgetBase::formElement in src/
Plugin/ Field/ FieldWidget/ SmartDateWidgetBase.php - Returns the form for a single field widget.
- SmartDateWidgetBase::massageFormValues in src/
Plugin/ Field/ FieldWidget/ SmartDateWidgetBase.php - Massages the form values into the format expected for field values.
File
- modules/
smart_date_recur/ smart_date_recur.module, line 120 - Field hooks for a field that stores a start and end date as timestamps.
Code
function smart_date_recur_widget_extra_fields(&$element, $item, $context) {
$default_repeat = '';
$default_end_count = '';
$default_end_date = '';
$limit_type = '';
$defaults = [
'interval' => NULL,
'which' => '',
'day' => '',
'byday' => [],
'byhour' => [],
'byminute' => [],
];
// Get current user.
$user = \Drupal::currentUser();
// Check for permission.
$recur_permitted = $user
->hasPermission('make smart dates recur');
if ($item->rrule) {
$rrule = SmartDateRule::load($item->rrule);
if ($rrule) {
$default_repeat = $rrule
->get('freq')
->getString();
if ($rrule
->get('limit')
->getString()) {
list($limit_type, $limit_value) = explode('=', $rrule
->get('limit')
->getString());
if ($limit_type == 'COUNT') {
$default_end_count = $limit_value;
}
elseif ($limit_type == 'UNTIL') {
$default_end_date = $limit_value;
}
}
$defaults = $rrule
->getParametersArray();
}
$element['rrule'] = [
'#type' => 'hidden',
'#title' => t('Existing Rule ID'),
'#value' => $item->rrule,
];
if (!$recur_permitted) {
$element['repeat'] = [
'#type' => 'hidden',
'#title' => t('Existing repeat frequency'),
'#value' => $default_repeat,
];
$element['repeat-end'] = [
'#type' => 'hidden',
'#title' => t('Existing repeat limit type'),
'#value' => $limit_type,
];
$element['repeat-end-count'] = [
'#type' => 'hidden',
'#title' => t('Existing maximum instances'),
'#value' => $default_end_count,
];
$element['repeat-end-date'] = [
'#type' => 'hidden',
'#title' => t('Existing last instance date'),
'#value' => $default_end_date,
];
if ($rule_text = $rrule
->getTextRule()) {
$element['repeat-text'] = [
'#markup' => '<span class="clearfix"></span>
<h4 class="label">' . t('Repeats') . '</h4>
<p class="repeat-text"> ' . $rule_text . '</p>',
];
}
return;
}
// Also insert a link to the interface for managing interfaces.
$modal = SmartDateRule::getThirdPartyFallback($context, 'modal', 1);
$url = Url::fromRoute('smart_date_recur.instances', [
'rrule' => $item->rrule,
'modal' => $modal,
]);
$instances_link = Link::fromTextAndUrl(t('Manage Instances'), $url);
$instances_link = $instances_link
->toRenderable();
// Add some classes.
$instances_link['#attributes'] = [
'class' => [
'button',
'button--small',
'manage-instances',
],
];
if ($modal) {
$instances_link['#attributes']['class'][] = 'use-ajax';
$instances_link['#attached']['library'][] = 'core/drupal.dialog.ajax';
}
$instances_link['#weight'] = 100;
$element['manage-instances'] = $instances_link;
}
elseif (!$recur_permitted) {
return;
}
$element['#attached']['library'][] = 'smart_date_recur/smart_date_recur';
$select_repeat = 'select[name$="[' . $element['#delta'] . '][repeat]"]';
$select_repeat_end = 'select[name$="[' . $element['#delta'] . '][repeat-end]"]';
$element['repeat-label'] = [
'#type' => 'label',
'#title' => t('Repeats') . ' ',
'#prefix' => '<div class="clearfix"></div>',
'#attributes' => [
'class' => [
'repeat--label',
],
],
];
$element['interval'] = [
'#type' => 'number',
'#title' => t('every'),
'#label_attributes' => [
'class' => [
'field-interval--label',
],
],
'#attributes' => [
'class' => [
'field-interval',
],
],
'#min' => 1,
'#step' => 1,
'#placeholder' => t('# of'),
'#default_value' => $defaults['interval'],
'#states' => [
// Show this textarea only if the 'repeat' select has a value.
'invisible' => [
$select_repeat => [
'value' => '',
],
],
],
];
$repeat_labels = [
'MINUTELY' => t('minutes'),
'HOURLY' => t('hours'),
'DAILY' => t('days'),
'WEEKLY' => t('weeks'),
'MONTHLY' => t('months'),
'YEARLY' => t('years'),
];
$labels = [
'' => t('never'),
];
$allowed_values = SmartDateRule::getThirdPartyFallback($context, 'allowed_recur_freq_values', _smart_date_recur_get_freq_defaults());
foreach ($allowed_values as $value) {
if (isset($repeat_labels[$value])) {
$labels[$value] = $repeat_labels[$value];
}
}
$element['repeat'] = [
'#type' => 'select',
'#title' => t('Repeats'),
'#title_display' => 'invisible',
'#options' => $labels,
'#default_value' => $default_repeat,
'#attributes' => [
'class' => [
'recur-repeat',
],
],
];
$element['repeat-end'] = [
'#type' => 'select',
'#title' => t('Ends'),
'#label_attributes' => [
'class' => [
'pad-left',
],
],
'#options' => [
'' => t('Never'),
'COUNT' => t('After'),
'UNTIL' => t('On Date'),
],
'#states' => [
// Show this textarea only if the 'repeat' select has a value.
'invisible' => [
$select_repeat => [
'value' => '',
],
],
],
'#default_value' => $limit_type,
];
$element['repeat-end-count'] = [
'#type' => 'number',
'#title' => t('Ends after'),
'#title_display' => t('invisible'),
'#min' => 2,
'#step' => 1,
'#field_suffix' => t('times'),
'#placeholder' => t('# of'),
'#states' => [
// Show this textarea only if the 'repeat' select has a value.
'visible' => [
$select_repeat => [
'!value' => '',
],
$select_repeat_end => [
'value' => 'COUNT',
],
],
],
'#default_value' => $default_end_count,
];
$element['repeat-end-date'] = [
'#type' => 'date',
'#title' => t('Ends on date'),
'#title_display' => t('invisible'),
'#states' => [
// Show this textarea only if the 'repeat' select has a value.
'visible' => [
$select_repeat => [
'!value' => '',
],
$select_repeat_end => [
'value' => 'UNTIL',
],
],
],
'#default_value' => $default_end_date,
'#attributes' => [
'class' => [
'repeat-end-date',
],
'type' => 'date',
],
];
$element['repeat-advanced'] = [
'#type' => 'details',
'#title' => t('Advanced'),
'#open' => $defaults['interval'] || $defaults['which'] || $defaults['day'] || $defaults['byday'],
'#states' => [
// Show this textarea only if the 'repeat' select has a value.
'invisible' => [
[
$select_repeat => [
'value' => '',
],
],
],
],
];
// Checkboxes to select which weekdays for weekly repeats.
$days = [
'SU' => t('Sunday'),
'MO' => t('Monday'),
'TU' => t('Tuesday'),
'WE' => t('Wednesday'),
'TH' => t('Thursday'),
'FR' => t('Friday'),
'SA' => t('Saturday'),
];
// Weekday int. 0-6 (Sun-Sat).
$firstDayInt = \Drupal::config('system.date')
->get('first_day');
// Rebuild weekday options where system first day is first option in list.
$days_by_config = array_merge(array_slice($days, $firstDayInt), array_slice($days, 0, $firstDayInt));
$element['repeat-advanced']['byday'] = [
'#type' => 'checkboxes',
'#title' => t('on days'),
'#title_display' => t('inline'),
'#options' => $days_by_config,
// Populate with any existing values.
'#default_value' => $defaults['byday'],
'#states' => [
// Show only if the repeat select has an appropriate value.
'visible' => [
[
$select_repeat => [
'value' => 'MINUTELY',
],
],
[
$select_repeat => [
'value' => 'HOURLY',
],
],
[
$select_repeat => [
'value' => 'DAILY',
],
],
[
$select_repeat => [
'value' => 'WEEKLY',
],
],
],
],
'#attributes' => [
'class' => [
'container-inline',
'byday-checkboxes',
],
],
];
$element['repeat-advanced']['which'] = [
'#type' => 'select',
'#title' => t('on the'),
'#options' => [
'' => t('- Select -'),
'1' => t('First'),
'2' => t('Second'),
'3' => t('Third'),
'4' => t('Fourth'),
'5' => t('Fifth'),
'-1' => t('Last'),
],
'#default_value' => $defaults['which'],
'#states' => [
// Show this textarea only if the repeat select has an appropriate value.
'visible' => [
[
$select_repeat => [
'value' => 'MONTHLY',
],
],
[
$select_repeat => [
'value' => 'YEARLY',
],
],
],
],
];
$element['repeat-advanced']['weekday'] = [
'#type' => 'select',
'#title' => t('Weekday'),
'#title_display' => t('invisible'),
'#label_attributes' => [
'class' => [
'pad-left',
],
],
'#options' => [
'' => t('- Day (any) -'),
'SU' => t('Sunday'),
'MO' => t('Monday'),
'TU' => t('Tuesday'),
'WE' => t('Wednesday'),
'TH' => t('Thursday'),
'FR' => t('Friday'),
'SA' => t('Saturday'),
'MO,TU,WE,TH,FR' => t('Weekday'),
'SA,SU' => t('Weekend day'),
],
'#default_value' => $defaults['day'],
'#states' => [
// Show this textarea only if the repeat select has an appropriate value.
'visible' => [
[
$select_repeat => [
'value' => 'MONTHLY',
],
],
[
$select_repeat => [
'value' => 'YEARLY',
],
],
],
],
];
$element['repeat-advanced']['restrict-hours'] = [
'#type' => 'details',
'#title' => t('Restrict to Specific Hours'),
'#open' => $defaults['byhour'],
'#states' => [
// Show this textarea only if the repeat select has an appropriate value.
'visible' => [
[
$select_repeat => [
'value' => 'MINUTELY',
],
],
[
$select_repeat => [
'value' => 'HOURLY',
],
],
],
],
];
$hour_options = [];
for ($i = 0; $i < 24; $i++) {
$label = sprintf("%02d", $i);
$hour_options[$label] = $i . ':00';
}
$element['repeat-advanced']['restrict-hours']['byhour'] = [
'#type' => 'checkboxes',
'#title' => t('Choose hours to include'),
'#description' => t('Leave empty to allow repeating at any hour.'),
'#options' => $hour_options,
// Populate with any existing values.
'#default_value' => $defaults['byhour'],
'#states' => [
// Show only if the repeat select has an appropriate value.
'visible' => [
[
$select_repeat => [
'value' => 'MINUTELY',
],
],
[
$select_repeat => [
'value' => 'HOURLY',
],
],
],
],
'#attributes' => [
'class' => [
'smart-date--hours',
'clearfix',
],
],
];
$element['repeat-advanced']['restrict-minutes'] = [
'#type' => 'details',
'#title' => t('Restrict to Specific Minutes on the Hour'),
'#open' => $defaults['byminute'],
'#states' => [
// Show this textarea only if the repeat select has an appropriate value.
'visible' => [
[
$select_repeat => [
'value' => 'MINUTELY',
],
],
],
],
];
$minute_options = [];
for ($i = 0; $i < 60; $i++) {
$label = sprintf("%02d", $i);
$minute_options[$label] = ':' . $label;
}
$element['repeat-advanced']['restrict-minutes']['byminute'] = [
'#type' => 'checkboxes',
'#title' => t('Choose minutes to include'),
'#description' => t('Leave empty to allow repeating at any value.'),
'#options' => $minute_options,
// Populate with any existing values.
'#default_value' => $defaults['byminute'],
'#states' => [
// Show only if the repeat select has an appropriate value.
'visible' => [
[
$select_repeat => [
'value' => 'MINUTELY',
],
],
],
],
'#attributes' => [
'class' => [
'smart-date--minutes',
'clearfix',
],
],
];
$element['#element_validate'][] = [
'Drupal\\smart_date_recur\\Entity\\SmartDateRule',
'validateRecurring',
];
}