forked from dakala/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fullcalendar.module
204 lines (173 loc) · 5.49 KB
/
fullcalendar.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* @file
* Provides a views style plugin for FullCalendar
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\EntityField;
/**
* The minimum supported version of the FullCalendar plugin.
*/
define('FULLCALENDAR_MIN_PLUGIN_VERSION', '3.3.0');
/**
* Implements hook_help().
*/
function fullcalendar_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.fullcalendar':
$output = '';
$output .= '<h3>' . t('Fullcalendar') . '</h3>';
$output .= '<p>' . t('The Fullcalendar module is an integration of the <a href="!fullcalendar-uri">Adam Shaw\'s FullCalendar jQuery plugin</a> with Drupal.', [
'!fullcalendar-uri' => Url::fromUri('https://github.com/arshaw/fullcalendar'),
]) . '</p>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function fullcalendar_theme($existing, $type, $theme, $path) {
return [
'fullcalendar_event' => [
'variables' => [
'event' => NULL,
'entity' => NULL,
],
'file' => 'fullcalendar.theme.inc',
],
];
}
/**
* Implements hook_permission().
*
* @return array
* An array of valid permissions for the FullCalendar module.
*/
function fullcalendar_permission() {
return [
'update any fullcalendar event' => [
'title' => t('Update any FullCalendar event'),
'description' => t('Allow user to edit events, ignoring other permissions.'),
],
];
}
/**
* Implements hook_fullcalendar_classes().
*/
function fullcalendar_fullcalendar_classes(EntityInterface $entity) {
$classes = [
'fc-event-default',
$entity->bundle(),
];
// @todo: ???
// Add a class for the date field being used.
if (isset($entity->fullcalendar_date_field)) {
$classes[] = "fc-event-field-$entity->fullcalendar_date_field";
}
return $classes;
}
/**
* Implements hook_form_FORM_ID_alter() for views_ui_edit_display_form().
*
* Since we force the query to be distinct, reflect that in the UI.
*/
function fullcalendar_form_views_ui_edit_display_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$style = $form_state->get('view')
->get('executable')->display_handler->getOption('style');
if ($style['type'] != 'fullcalendar' || empty($form['options']['query']['options']['distinct'])) {
return;
}
$distinct = &$form['options']['query']['options']['distinct'];
if (!isset($distinct['#description'])) {
$distinct['#description'] = '';
}
else {
$distinct['#description'] .= '<br>';
}
$distinct['#disabled'] = TRUE;
$distinct['#description'] .= '<strong>' . t('FullCalendar requires that the query be distinct.') . '</strong>';
}
/**
* Implements hook_fullcalendar_editable().
*
* Use our access callback as the editable setting.
*/
function fullcalendar_fullcalendar_editable(EntityInterface $entity, $view) {
return \Drupal::service('access_check.fullcalendar.update')->check($entity);
}
/**
* Determines if a given field is a date field.
*
* @param \Drupal\views\Plugin\views\field\EntityField $field
* A Views field handler object.
* @param bool $include_gcal
* Boolean indicating whether or not to count gcal fields as a date field.
*
* @return bool
* Boolean, TRUE if the field is a date field, FALSE otherwise.
*/
function fullcalendar_field_is_date($field, $include_gcal = FALSE) {
if (!$field instanceof EntityField) {
return FALSE;
}
if ($include_gcal && $field->field == 'gcal') {
return TRUE;
}
$entity_type = $field->definition['entity_type'];
if (empty($entity_type)) {
return FALSE;
}
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
$field_manager = \Drupal::getContainer()->get('entity_field.manager');
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storages */
$field_storages = $field_manager->getFieldStorageDefinitions($entity_type);
if (isset($field_storages[$field->definition['field_name']])) {
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */
$field_storage = $field_storages[$field->definition['field_name']];
return in_array($field_storage->getType(), [
'datetime',
'daterange',
'date_recur',
]);
}
return FALSE;
}
/**
* Returns the version of FullCalendar plugin that is installed.
*
* This can be used by other modules' hook_requirements() to ensure that the
* proper version of FullCalendar plugin is installed.
*
* @see version_compare()
*/
function fullcalendar_get_version($fullcalendar_path = NULL) {
$version = &drupal_static(__FUNCTION__);
if (empty($version)) {
$version = 0;
$pattern = '#FullCalendar v([0-9\.a-z]+)#';
// No file is passed in so use the default location.
if (!$fullcalendar_path) {
$fullcalendar_path = fullcalendar_get_js_path();
}
// Return the version of FullCalendar plugin.
$fullcalendar_plugin = file_get_contents($fullcalendar_path, NULL, NULL, 0, 40);
if (preg_match($pattern, $fullcalendar_plugin, $matches)) {
$version = $matches[1];
}
}
return $version;
}
/**
* Returns the path to the FullCalendar plugin.
*/
function fullcalendar_get_js_path() {
$fullcalendar_file = [
'none' => 'fullcalendar.js',
'min' => 'fullcalendar.min.js',
];
$config = \Drupal::config('fullcalendar.settings');
return $config->get('path') . '/' . $fullcalendar_file[$config->get('compression')];
}