Commit 13f9f384 authored by mark_fullmer's avatar mark_fullmer Committed by Mingsong Hu

Issue #3165143 by mark_fullmer: Support default views other than "listYear"

parent 0c5caac4
......@@ -6,15 +6,15 @@ use Drupal\Component\Utility\Xss;
use Drupal\Core\Datetime\DrupalDateTime;
class FullcalendarViewPreprocess {
protected static $viewIndex = 0;
/**
* Process the view variable array.
*
*
* @param array $variables
* Template variables.
*/
public function process(array &$variables) {
public function process(array &$variables) {
/* @var \Drupal\views\ViewExecutable $view */
$view = $variables['view'];
// View index.
......@@ -22,10 +22,10 @@ class FullcalendarViewPreprocess {
$style = $view->style_plugin;
$options = $style->options;
$fields = $view->field;
// Get current language.
$language = \Drupal::languageManager()->getCurrentLanguage();
// Current user.
$user = $variables['user'];
// CSRF token.
......@@ -33,7 +33,7 @@ class FullcalendarViewPreprocess {
if (!$user->isAnonymous()) {
$token = \Drupal::csrfToken()->get($user->id());
}
//
//
// New event bundle type.
$event_bundle_type = $options['bundle_type'];
$entity_type = $view->getBaseEntityType();
......@@ -49,7 +49,7 @@ class FullcalendarViewPreprocess {
$add_form = str_replace('{' . $entity_type->id() . '}', $event_bundle_type, $entity_links['add-page']);
}
}
// Can the user add a new event?
$entity_manager = \Drupal::entityTypeManager();
$access_handler = $entity_manager->getAccessControlHandler($entity_type->id());
......@@ -81,17 +81,17 @@ class FullcalendarViewPreprocess {
$duration_field = isset($options['duration']) ? $options['duration'] : NULL;
// Field machine name of excluding dates field.
$rrule_field = isset($options['rrule']) ? $options['rrule'] : NULL;
// Default date of the calendar.
switch ($options['default_date_source']) {
case 'now':
$default_date = date('Y-m-d');
break;
case 'fixed':
$default_date = $options['defaultDate'];
break;
default:
// Don't do anything, we'll set it below.
}
......@@ -114,7 +114,7 @@ class FullcalendarViewPreprocess {
// Right side buttons.
$right_buttons = Xss::filter($options['right_buttons']);
$entries = [];
if (!empty($start_field)) {
// Allowed tags for title markup.
$title_allowed_tags = Xss::getAdminTagList();
......@@ -146,7 +146,7 @@ class FullcalendarViewPreprocess {
// Calendar event start date.
$start_dates = $current_entity->get($start_field)->getValue();
// Calendar event end date.
$end_dates = empty($end_field) || !$current_entity->hasField($end_field) ? '' :
$end_dates = empty($end_field) || !$current_entity->hasField($end_field) ? '' :
$current_entity->get($end_field)->getValue();
// Render all other fields to so they can be used in rewrite.
foreach ($fields as $name => $field) {
......@@ -215,21 +215,21 @@ class FullcalendarViewPreprocess {
}
}
}
// A user who doesn't have the permission can't edit an event.
if (!$current_entity->access('update')) {
$entry['editable'] = FALSE;
}
// If we don't yet know the default_date (we're configured to use the
// date from the first row, and we haven't set it yet), do so now.
if (!isset($default_date)) {
// Only use the first 10 digits since we only care about the date.
$default_date = substr($start_date_value, 0, 10);
}
$all_day = (strlen($start_date_value) < 11) ? TRUE : FALSE;
if ($all_day) {
$entry['start'] = $start_date_value;
$entry['allDay'] = true;
......@@ -245,7 +245,7 @@ class FullcalendarViewPreprocess {
// If not, skip this row.
continue 2;
}
// Deal with the end date in the same way as start date above.
if (!empty($end_dates[$i])) {
if ($end_field_option['type'] === 'timestamp') {
......@@ -263,7 +263,7 @@ class FullcalendarViewPreprocess {
else {
$end_date = $end_dates[$i]['value'];
}
if (!empty($end_date)) {
$all_day = (strlen($end_date) < 11) ? TRUE : FALSE;
if ($all_day) {
......@@ -306,7 +306,7 @@ class FullcalendarViewPreprocess {
}
}
}
// Remove the row_index property as we don't it anymore.
unset($view->row_index);
// Fullcalendar options.
......@@ -344,7 +344,7 @@ class FullcalendarViewPreprocess {
'font-size' => '1rem'
]
];
// Load the fullcalendar js library.
$variables['#attached']['library'][] = 'fullcalendar_view/fullcalendar';
if ($options['dialogWindow']) {
......@@ -362,7 +362,7 @@ class FullcalendarViewPreprocess {
'languageSelector' => $options['languageSelector'],
// Event update confirmation pop-up dialog.
// If it is 1, a confirmation dialog will pop-up after dragging and dropping an event.
'updateConfirm' => $options['updateConfirm'],
'updateConfirm' => $options['updateConfirm'],
// Open event links in dialog window.
// If it is 1, event links in the calendar will open in a dialog window.
'dialogWindow' => $options['dialogWindow'],
......@@ -381,7 +381,7 @@ class FullcalendarViewPreprocess {
// CSRF token.
'token' => $token,
// Show an event details in a new window (tab).
'openEntityInNewTab' => $options['openEntityInNewTab'],
'openEntityInNewTab' => $options['openEntityInNewTab'],
// The options of the Fullcalendar object.
'calendar_options' => json_encode($calendar_options),
// The options of the pop-up dialog object.
......@@ -389,7 +389,7 @@ class FullcalendarViewPreprocess {
];
}
}
/**
* Map Drupal language codes to those used by FullCalendar.
*
......
......@@ -77,7 +77,12 @@ class FullCalendarDisplay extends StylePluginBase {
$options['color_taxonomies'] = ['default' => []];
$options['vocabularies'] = ['default' => ''];
$options['right_buttons'] = [
'default' => 'dayGridMonth,timeGridWeek,timeGridDay,listYear',
'default' => [
'dayGridMonth',
'timeGridWeek',
'timeGridDay',
'listYear',
],
];
$options['left_buttons'] = [
'default' => 'prev,next today',
......@@ -160,39 +165,49 @@ class FullCalendarDisplay extends StylePluginBase {
'#title' => $this->t('Display'),
'#description' => $this->t('Calendar display settings.'),
];
// Right side buttons.
$form['right_buttons'] = [
'#type' => 'textfield',
'#fieldset' => 'display',
'#default_value' => (empty($this->options['right_buttons'])) ? [] : $this->options['right_buttons'],
'#title' => $this->t('Right side buttons'),
'#description' => $this->t('Right side buttons. Buttons are seperated by commas or space. See the %fullcalendar_doc for availabel buttons.',
[
'%fullcalendar_doc' => Link::fromTextAndUrl($this->t('Fullcalendar documentation'), Url::fromUri('https://fullcalendar.io/docs/header', array('attributes' => array('target' => '_blank'))))->toString(),
]),
$fullcalendar_displays = [
'dayGridMonth' => $this->t('Month'),
'timeGridWeek' => $this->t('Week'),
'timeGridDay' => $this->t('Day'),
'listYear' => $this->t('List (Year)'),
'listMonth' => $this->t('List (Month)'),
'listWeek' => $this->t('List (Week)'),
'listDay' => $this->t('List (Day)'),
];
// Right side buttons.
$display_defaults = (empty($this->options['right_buttons'])) ? [] : $this->options['right_buttons'];
if (is_string($display_defaults)) {
$display_defaults = explode(',', $display_defaults);
}
// Left side buttons.
$form['left_buttons'] = [
'#type' => 'textfield',
'#fieldset' => 'display',
'#default_value' => (empty($this->options['left_buttons'])) ? [] : $this->options['left_buttons'],
'#title' => $this->t('Left side buttons'),
'#description' => $this->t('Left side buttons. Buttons are seperated by commas or space. See the %fullcalendar_doc for availabel buttons.',
'#description' => $this->t(
'Left side buttons. Buttons are separated by commas or space. See the %fullcalendar_doc for available buttons.',
[
'%fullcalendar_doc' => Link::fromTextAndUrl($this->t('Fullcalendar documentation'), Url::fromUri('https://fullcalendar.io/docs/header', array('attributes' => array('target' => '_blank'))))->toString(),
]
),
];
$form['right_buttons'] = [
'#type' => 'checkboxes',
'#fieldset' => 'display',
'#options' => $fullcalendar_displays,
'#default_value' => $display_defaults,
'#title' => $this->t('Display toggles'),
'#description' => $this->t('Shown as buttons on the right side of the calendar view. See the %fullcalendar_doc.',
[
'%fullcalendar_doc' => Link::fromTextAndUrl($this->t('Fullcalendar documentation'), Url::fromUri('https://fullcalendar.io/docs/header', array('attributes' => array('target' => '_blank'))))->toString(),
'%fullcalendar_doc' => Link::fromTextAndUrl($this->t('Fullcalendar "Views" documentation'), Url::fromUri('https://fullcalendar.io/docs', array('attributes' => array('target' => '_blank'))))->toString(),
]),
];
// Default view.
// Todo: filter out disabled view from options.
$form['default_view'] = [
'#type' => 'radios',
'#fieldset' => 'display',
'#options' => [
'dayGridMonth' => $this->t('Month'),
'timeGridWeek' => $this->t('Week'),
'timeGridDay' => $this->t('Day'),
'listYear' => $this->t('List'),
],
'#options' => $fullcalendar_displays,
'#default_value' => (empty($this->options['default_view'])) ? 'month' : $this->options['default_view'],
'#title' => $this->t('Default view'),
];
......@@ -372,7 +387,7 @@ class FullCalendarDisplay extends StylePluginBase {
'#title' => $this->t('Legend Colors'),
'#description' => $this->t('Set color value of legends for each content type or each taxonomy.'),
];
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('taxonomy')) {
// All vocabularies.
......@@ -452,7 +467,7 @@ class FullCalendarDisplay extends StylePluginBase {
'#type' => 'color',
];
}
// Recurring event.
$form['recurring'] = [
'#type' => 'details',
......@@ -490,7 +505,7 @@ class FullCalendarDisplay extends StylePluginBase {
],
],
];
// New event bundle type.
$form['bundle_type'] = [
'#title' => $this->t('Event bundle (Content) type'),
......@@ -508,6 +523,21 @@ class FullCalendarDisplay extends StylePluginBase {
];
}
/**
* Options form validation handle function.
*
* @see \Drupal\views\Plugin\views\PluginBase::validateOptionsForm()
*/
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$style_options = &$form_state->getValue('style_options');
$selected_displays = $style_options['right_buttons'];
$default_display = $style_options['default_view'];
if (!in_array($default_display, array_filter(array_values($selected_displays)))) {
$form_state->setErrorByName('style_options][default_view', $this->t('The default view must be one of the selected display toggles.'));
}
}
/**
* Options form submit handle function.
*
......@@ -523,7 +553,8 @@ class FullCalendarDisplay extends StylePluginBase {
$options['color_taxonomies'][$id] = $color;
}
}
$options['right_buttons'] = isset($input_value['style_options']['right_buttons']) ? implode(',', array_filter(array_values($input_value['style_options']['right_buttons']))) : '';
// Sanitize user input.
$options['timeFormat'] = Xss::filter($options['timeFormat']);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment