View source
<?php
namespace Drupal\fullcalendar_view\Plugin\views\style;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\fullcalendar_view\TaxonomyColor;
use Drupal\core\form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\Datetime\DrupalDateTime;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FullCalendarDisplay extends StylePluginBase {
protected $usesFields = TRUE;
protected $taxonomyColorService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, TaxonomyColor $taxonomyColorService) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->taxonomyColorService = $taxonomyColorService;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('fullcalendar_view.taxonomy_color'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['defaultDate'] = [
'default' => '',
];
$options['start'] = [
'default' => '',
];
$options['end'] = [
'default' => '',
];
$options['business_start'] = [
'default' => '',
];
$options['business_end'] = [
'default' => '',
];
$options['content_type'] = [
'default' => '',
];
$options['tax_field'] = [
'default' => '',
];
$options['color_contents'] = [
'default' => [],
];
$options['color_taxonomies'] = [
'default' => [],
];
$options['vocabularies'] = [
'default' => '',
];
$options['right_buttons'] = [
'default' => [
'agendaWeek' => 'agendaWeek',
'agendaDay' => 'agendaDay',
'listYear' => 'listYear',
],
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
if (isset($form['grouping'])) {
unset($form['grouping']);
}
$form['defaultDate'] = [
'#type' => 'date',
'#title' => t('Default Date'),
'#default_value' => isset($this->options['defaultDate']) ? $this->options['defaultDate'] : '',
'#description' => t('The initial date displayed when the calendar first loads. It should be in ISO 8601 format. For example: 2018-01-24'),
];
$field_names = $this->displayHandler
->getFieldLabels();
$form['start'] = [
'#title' => $this
->t('Start Date Field'),
'#type' => 'select',
'#options' => $field_names,
'#default_value' => !empty($this->options['start']) ? $this->options['start'] : '',
];
$form['end'] = [
'#title' => $this
->t('End Date Field'),
'#type' => 'select',
'#options' => $field_names,
'#empty_value' => '',
'#default_value' => !empty($this->options['end']) ? $this->options['end'] : '',
];
$form['display'] = [
'#type' => 'details',
'#title' => t('Display'),
'#description' => t('Calendar display settings.'),
];
$form['right_buttons'] = [
'#type' => 'checkboxes',
'#fieldset' => 'display',
'#options' => [
'agendaWeek' => $this
->t('Week'),
'agendaDay' => $this
->t('Day'),
'listYear' => $this
->t('List'),
],
'#default_value' => empty($this->options['right_buttons']) ? [] : $this->options['right_buttons'],
'#title' => $this
->t('Right side buttons'),
];
$form['colors'] = [
'#type' => 'details',
'#title' => t('Legend Colors'),
'#description' => t('Set color value of legends for each content type or each taxonomy.'),
];
$cabNames = taxonomy_vocabulary_get_names();
$tax_fields = [];
foreach ($field_names as $field_name => $lable) {
$field_conf = FieldStorageConfig::loadByName('node', $field_name);
if (empty($field_conf)) {
continue;
}
if ($field_conf
->getType() == 'entity_reference') {
$tax_fields[$field_name] = $lable;
}
}
$form['tax_field'] = [
'#title' => $this
->t('Event Taxonomy Field'),
'#description' => t('In order to specify colors for event taxonomies, you must select a taxonomy reference field for the View.'),
'#type' => 'select',
'#options' => $tax_fields,
'#empty_value' => '',
'#disabled' => empty($tax_fields),
'#fieldset' => 'colors',
'#default_value' => !empty($this->options['tax_field']) ? $this->options['tax_field'] : '',
];
$form['vocabularies'] = [
'#title' => $this
->t('Vocabularies'),
'#type' => 'select',
'#options' => $cabNames,
'#empty_value' => '',
'#fieldset' => 'colors',
'#description' => t('Specify which vocabulary is using for calendar event color. If the vocabulary selected is not the one that the taxonomy field belonging to, the color setting would be ignored.'),
'#default_value' => !empty($this->options['vocabularies']) ? $this->options['vocabularies'] : '',
'#states' => [
'invisible' => [
[
':input[name="style_options[tax_field]"]' => [
'value' => '',
],
],
],
],
'#ajax' => [
'callback' => 'Drupal\\fullcalendar_view\\Plugin\\views\\style\\FullCalendarDisplay::taxonomyColorCallback',
'event' => 'change',
'wrapper' => 'color-taxonomies-div',
'progress' => [
'type' => 'throbber',
'message' => t('Verifying entry...'),
],
],
];
if (!isset($form_state
->getUserInput()['style_options'])) {
$form['color_taxonomies'] = $this->taxonomyColorService
->colorInputBoxs($this->options['vocabularies'], $this->options['color_taxonomies']);
}
$form['color_contents'] = [
'#type' => 'details',
'#title' => t('Colors for Content Types'),
'#description' => t('Specify colors for each content type. If taxonomy color is specified, this settings would be ignored.'),
'#fieldset' => 'colors',
];
$contentTypes = \Drupal::service('entity_type.manager')
->getStorage('node_type')
->loadMultiple();
$contentTypesList = [];
foreach ($contentTypes as $contentType) {
$id = $contentType
->id();
$label = $contentType
->label();
$contentTypesList[$id] = $label;
$form['color_contents'][$id] = [
'#title' => $label,
'#default_value' => isset($this->options['color_contents'][$id]) ? $this->options['color_contents'][$id] : '#3a87ad',
'#type' => 'color',
];
}
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler
->moduleExists('calendar_recurring_event')) {
$form['recurring'] = [
'#type' => 'details',
'#title' => t('Recurring event settings'),
];
$form['business_start'] = [
'#type' => 'datetime',
'#title' => t('Business start time'),
'#description' => t('The time of a day when a recurring all day event starts. The recurring events whose start date include hour and minute will use their respective start time instead.'),
'#fieldset' => 'recurring',
'#default_value' => empty($this->options['business_start']) ? new DrupalDateTime('2018-02-24T08:00:00') : new DrupalDateTime($this->options['business_start']),
'#date_date_element' => 'none',
'#date_time_element' => 'time',
'#date_time_format' => 'H:i',
];
$form['business_end'] = [
'#type' => 'datetime',
'#title' => t('Business end time'),
'#description' => t('The time of a day when a recurring event ends. The recurring events whose end date include hour and minute will use their respective end time instead.'),
'#fieldset' => 'recurring',
'#default_value' => empty($this->options['business_end']) ? new DrupalDateTime('2018-02-24T18:00:00') : new DrupalDateTime($this->options['business_end']),
'#date_date_element' => 'none',
'#date_time_element' => 'time',
'#date_time_format' => 'H:i',
];
}
$form['content_type'] = [
'#title' => $this
->t('Event content type'),
'#description' => $this
->t('The content type of a new event. Once this is set, you can create a new event by double clicking a calendar entry.'),
'#type' => 'select',
'#options' => $contentTypesList,
'#default_value' => !empty($this->options['content_type']) ? $this->options['content_type'] : '',
];
$form['classes'] = [
'#type' => 'textfield',
'#title' => t('CSS classes'),
'#default_value' => isset($this->options['classes']) ? $this->options['classes'] : '',
'#description' => t('CSS classes for further customization of this view.'),
];
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$options =& $form_state
->getValue('style_options');
$input_value = $form_state
->getUserInput();
$input_colors = isset($input_value['style_options']['color_taxonomies']) ? $input_value['style_options']['color_taxonomies'] : [];
foreach ($input_colors as $id => $color) {
if (!empty($color)) {
$options['color_taxonomies'][$id] = $color;
}
}
if (isset($options['business_start'])) {
$options['business_start'] = $options['business_start']
->format(DATETIME_DATETIME_STORAGE_FORMAT);
}
if (isset($options['business_end'])) {
$options['business_end'] = $options['business_end']
->format(DATETIME_DATETIME_STORAGE_FORMAT);
}
parent::submitOptionsForm($form, $form_state);
}
public static function taxonomyColorCallback(array &$form, FormStateInterface $form_state) {
$options = $form_state
->getValue('style_options');
$vid = $options['vocabularies'];
$taxonomy_color_service = \Drupal::service('fullcalendar_view.taxonomy_color');
if (isset($options['color_taxonomies'])) {
$defaultValues = $options['color_taxonomies'];
}
else {
$defaultValues = [];
}
$form['color_taxonomies'] = $taxonomy_color_service
->colorInputBoxs($vid, $defaultValues, TRUE);
return $form['color_taxonomies'];
}
}