1
1
<?php
2
2
3
3
use Drupal\Core\Link;
4
+ use Drupal\Core\Messenger\MessengerInterface;
4
5
5
6
/**
6
7
* Implements hook_form_FORM_ID_alter().
@@ -40,9 +41,11 @@ function gallerysite_term_submit(&$form, \Drupal\Core\Form\FormStateInterface $f
40
41
* Implements hook_form_FORM_ID_alter().
41
42
*/
42
43
function gallerysite_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
44
+ $user = \Drupal::currentUser();
45
+
43
46
$form['artists']['widget']['add_more']['#value'] = t('Add another artist');
44
47
$form['tags']['widget']['add_more']['#value'] = t('Add another tag');
45
-
48
+
46
49
// Hide revision log fields
47
50
$form['revision']['#access'] = FALSE;
48
51
$form['revision_log']['#access'] = FALSE;
@@ -52,29 +55,79 @@ function gallerysite_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInt
52
55
53
56
// Remove votes open/closed.
54
57
$form['field_rating']['#access'] = FALSE;
55
-
58
+
56
59
// Restrict access to metatag.
57
- $form['field_metatags']['#access'] = \Drupal::currentUser() ->hasPermission('administer meta tags');
60
+ $form['field_metatags']['#access'] = $user ->hasPermission('administer meta tags');
58
61
59
62
// Add a class if the address has no default value.
60
63
if (!empty($form['field_address']) && empty($form['field_address']['widget'][0]['address']['#default_value']['locality'])) {
61
64
$form['field_address']['#attributes']['class'][] = 'field--widget-address-blank';
62
65
}
66
+
67
+ // If the user already has published content, set the node as published by default.
68
+ if (gallerysite_has_content($user)) {
69
+ $form['status']['widget']['value']['#default_value'] = TRUE;
70
+ }
71
+
72
+ }
73
+
74
+ /**
75
+ * Check if a user has published content.
76
+ *
77
+ * @param \Drupal\Core\Session\AccountProxy $user
78
+ * The user.
79
+ *
80
+ * @return bool
81
+ * TRUE if the user has published content.
82
+ */
83
+ function gallerysite_has_content($user) {
84
+ $ids = \Drupal::entityQuery('node')
85
+ ->condition('uid', $user->id())
86
+ ->condition('status', 1)
87
+ ->execute();
88
+ return !empty($ids);
63
89
}
64
90
65
91
/**
66
92
* Implements hook_form_FORM_ID_alter().
67
93
*/
68
94
function gallerysite_form_node_gallery_delete_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
69
- // TODO: Prevent deletion if the gallery has any exhibitions.
95
+ // Does this gallery have any exhibitions?
96
+ $build_info = $form_state->getBuildInfo();
97
+ $gallery = $build_info['callback_object']->getEntity();
98
+ $exhibitions = gallerysite_get_exhibitions($gallery);
99
+ if (!empty($exhibitions)) {
100
+ // Prevent deletion.
101
+ $form['actions']['submit']['#access'] = FALSE;
102
+ $form['description']['#access'] = FALSE;
103
+ \Drupal::messenger()->addError(t('Galleries with exhibitions cannot be deleted.'));
104
+ }
105
+ }
70
106
107
+ /**
108
+ * Get exhibitions for a gallery.
109
+ *
110
+ * @param \Drupal\node\Entity\Node $gallery
111
+ * The gallery node.
112
+ *
113
+ * @return array
114
+ * The node IDs of exhibitions at this gallery.
115
+ */
116
+ function gallerysite_get_exhibitions($gallery) {
117
+ return \Drupal::entityQuery('node')
118
+ ->condition('type', 'exhibition')
119
+ ->condition('field_exhib_gallery', $gallery->id())
120
+ ->execute();
71
121
}
72
122
73
123
/**
74
124
* Implements hook_ENTITY_TYPE_insert().
75
125
*/
76
126
function gallerysite_node_insert(Drupal\Core\Entity\EntityInterface $entity) {
77
- drupal_set_message(t('Your listing has been created, and is awaiting moderation. Our team will publish it as soon as possible.'));
127
+ $published = $entity->isPublished();
128
+ if (!$published) {
129
+ \Drupal::messenger()->addMessage(t('Your listing has been created, and is awaiting moderation. Our team will publish it as soon as possible.'));
130
+ }
78
131
}
79
132
80
133
/**
@@ -99,7 +152,7 @@ function gallerysite_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInt
99
152
function gallerysite_form_user_register_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
100
153
// Hide the content form options.
101
154
$form['contact']['#access'] = FALSE;
102
-
155
+
103
156
$link_options = ['absolute' => TRUE];
104
157
105
158
$terms_nid = 6345;
@@ -114,43 +167,43 @@ function gallerysite_form_user_register_form_alter(&$form, \Drupal\Core\Form\For
114
167
function gallerysite_default_image() {
115
168
116
169
$connection = Database::getConnection();
117
-
170
+
118
171
$query = $connection->select('file_managed', 'fm')
119
172
->condition('uri', 'public://imagefield_default_images/anon_large.gif')
120
173
->fields('fm', array('fid', 'uuid'))
121
174
->execute();
122
-
175
+
123
176
// $connection->update('file_managed', 'fm')
124
177
// ->condition('fid')
125
178
126
-
179
+
127
180
}
128
181
129
182
/**
130
183
* Implements hook_cron().
131
184
*/
132
185
function gallerysite_cron() {
133
186
// Get all promoted exhibitions which have already finished.
134
- $date = date('Y-m-d', REQUEST_TIME );
187
+ $date = date('Y-m-d', \Drupal::time()->getRequestTime() );
135
188
$query = \Drupal::entityQuery('node')
136
189
->condition('type', 'exhibition')
137
190
->condition('status', 1)
138
191
->condition('promote', 1)
139
192
->condition('field_date_to.value', $date, '<');
140
-
193
+
141
194
$nids = $query->execute();
142
-
195
+
143
196
// Unpromote the nodes.
144
197
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
145
198
foreach ($nodes as $node) {
146
199
$node->set('promote', 0);
147
200
$node->save();
148
201
}
149
-
202
+
150
203
// Truncate tables that grow too large.
151
204
$connection = \Drupal::database();
152
205
$tables = array(
153
- 'cache_render',
206
+ 'cache_render',
154
207
'cache_dynamic_page_cache',
155
208
);
156
209
foreach ($tables as $table) {
@@ -202,7 +255,7 @@ function _recursively_alter_query_conditions(&$conditions) {
202
255
203
256
/**
204
257
* Actually alter the condition if relevant.
205
- */
258
+ */
206
259
function _alter_query_condition(&$condition) {
207
260
if (isset($condition['field']) && ($condition['field'] === 'SUBSTRING(node_field_data.title, 1, 1) = :node_field_data_title')) {
208
261
$condition['field'] = "SUBSTRING(views_natural_sort.content, 1, 1) = :node_field_data_title";
0 commit comments