-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallerysite.module
264 lines (224 loc) · 8.25 KB
/
gallerysite.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
use Drupal\Core\Link;
use Drupal\Core\Messenger\MessengerInterface;
/**
* Implements hook_form_FORM_ID_alter().
*/
function gallerysite_form_taxonomy_term_artists_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Redirect to the term view page after editing.
$form['actions']['submit']['#submit'][] = 'gallerysite_term_submit';
// Remove votes open/closed.
$form['field_rating']['#access'] = FALSE;
// Remove relations section.
$form['relations']['#access'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function gallerysite_form_taxonomy_term_tags_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Redirect to the term view page after editing.
$form['actions']['submit']['#submit'][] = 'gallerysite_term_submit';
}
/**
* Submit handler for taxonomy terms.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function gallerysite_term_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
// Redirect to the term view page after editing.
$tid = $form['tid']['#value'];
$form_state->setRedirect('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function gallerysite_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$user = \Drupal::currentUser();
$form['artists']['widget']['add_more']['#value'] = t('Add another artist');
$form['tags']['widget']['add_more']['#value'] = t('Add another tag');
// Hide revision log fields
$form['revision']['#access'] = FALSE;
$form['revision_log']['#access'] = FALSE;
// Remove preview functionality.
$form['actions']['preview']['#access'] = FALSE;
// Remove votes open/closed.
$form['field_rating']['#access'] = FALSE;
// Restrict access to metatag.
$form['field_metatags']['#access'] = $user->hasPermission('administer meta tags');
// Add a class if the address has no default value.
if (!empty($form['field_address']) && empty($form['field_address']['widget'][0]['address']['#default_value']['locality'])) {
$form['field_address']['#attributes']['class'][] = 'field--widget-address-blank';
}
// If the user already has published content, set the node as published by default.
if (gallerysite_has_content($user)) {
$form['status']['widget']['value']['#default_value'] = TRUE;
}
}
/**
* Check if a user has published content.
*
* @param \Drupal\Core\Session\AccountProxy $user
* The user.
*
* @return bool
* TRUE if the user has published content.
*/
function gallerysite_has_content($user) {
$ids = \Drupal::entityQuery('node')
->condition('uid', $user->id())
->condition('status', 1)
->execute();
return !empty($ids);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function gallerysite_form_node_gallery_delete_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Does this gallery have any exhibitions?
$build_info = $form_state->getBuildInfo();
$gallery = $build_info['callback_object']->getEntity();
$exhibitions = gallerysite_get_exhibitions($gallery);
if (!empty($exhibitions)) {
// Prevent deletion.
$form['actions']['submit']['#access'] = FALSE;
$form['description']['#access'] = FALSE;
\Drupal::messenger()->addError(t('Galleries with exhibitions cannot be deleted.'));
}
}
/**
* Get exhibitions for a gallery.
*
* @param \Drupal\node\Entity\Node $gallery
* The gallery node.
*
* @return array
* The node IDs of exhibitions at this gallery.
*/
function gallerysite_get_exhibitions($gallery) {
return \Drupal::entityQuery('node')
->condition('type', 'exhibition')
->condition('field_exhib_gallery', $gallery->id())
->execute();
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function gallerysite_node_insert(Drupal\Core\Entity\EntityInterface $entity) {
$published = $entity->isPublished();
if (!$published) {
\Drupal::messenger()->addMessage(t('Your listing has been created, and is awaiting moderation. Our team will publish it as soon as possible.'));
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function gallerysite_form_contact_message_contact_us_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Remove preview option from contact form.
$form['actions']['preview']['#access'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function gallerysite_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Hide the content form options.
$form['contact']['#access'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function gallerysite_form_user_register_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Hide the content form options.
$form['contact']['#access'] = FALSE;
$link_options = ['absolute' => TRUE];
$terms_nid = 6345;
$terms_link = Link::createFromRoute('Terms and Conditions', 'entity.node.canonical', ['node' => $terms_nid], $link_options);
$privacy_nid = 6343;
$privacy_link = Link::createFromRoute('Privacy Policy', 'entity.node.canonical', ['node' => $privacy_nid], $link_options);
$form['terms']['#markup'] = '<p>By creating an account you agree to the ' . $terms_link->toString() . ' and ' . $privacy_link->toString() . '.</p>';
}
function gallerysite_default_image() {
$connection = Database::getConnection();
$query = $connection->select('file_managed', 'fm')
->condition('uri', 'public://imagefield_default_images/anon_large.gif')
->fields('fm', array('fid', 'uuid'))
->execute();
// $connection->update('file_managed', 'fm')
// ->condition('fid')
}
/**
* Implements hook_cron().
*/
function gallerysite_cron() {
// Get all promoted exhibitions which have already finished.
$date = date('Y-m-d', \Drupal::time()->getRequestTime());
$query = \Drupal::entityQuery('node')
->condition('type', 'exhibition')
->condition('status', 1)
->condition('promote', 1)
->condition('field_date_to.value', $date, '<')
->accessCheck(FALSE);
$nids = $query->execute();
// Unpromote the nodes.
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
foreach ($nodes as $node) {
$node->set('promote', 0);
$node->save();
}
// Truncate tables that grow too large.
$connection = \Drupal::database();
$tables = array(
'cache_render',
'cache_dynamic_page_cache',
);
foreach ($tables as $table) {
$connection->delete($table)->execute();
}
}
/**
* Implements hook_views_query_alter().
*/
function views_natural_sort_views_query_alter($view, $query) {
// make sure we're altering the correct view
$view_id = $view->storage->get('id');
if (($view_id == 'galleries_a_z' || $view_id == 'exhibitions_a_z') && $view->current_display == 'page_1') {
$definition = [
'table' => 'views_natural_sort',
'field' => 'eid',
'left_table' => 'node_field_data',
'left_field' => 'nid',
];
$join = Drupal::service('plugin.manager.views.join')->createInstance('standard', $definition);
$query->addRelationship('views_natural_sort', $join, 'views_natural_sort');
// foreach top level condition group
foreach ($query->where as &$condition_group) {
// call helper function to recursively alter conditions
_recursively_alter_query_conditions($condition_group['conditions']);
}
}
}
/**
* Helper function to loop through query conditions.
*/
function _recursively_alter_query_conditions(&$conditions) {
// foreach condition in condition group
foreach ($conditions as &$condition) {
// if condition is itself a condition group
if (isset($condition['field']) && is_a($condition['field'], 'Drupal\Core\Database\Query\Condition')) {
// call the helper function on it
_recursively_alter_query_conditions($condition['field']->conditions());
}
else {
// check if we want to alter the condition and if so alter it
_alter_query_condition($condition);
}
}
}
/**
* Actually alter the condition if relevant.
*/
function _alter_query_condition(&$condition) {
if (isset($condition['field']) && ($condition['field'] === 'SUBSTRING(node_field_data.title, 1, 1) = :node_field_data_title')) {
$condition['field'] = "SUBSTRING(views_natural_sort.content, 1, 1) = :node_field_data_title";
}
}