Skip to content

Commit 71b00b8

Browse files
committed
Drupal 9 updates
1 parent 500cb49 commit 71b00b8

5 files changed

+82
-26
lines changed

gallerysite.info.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: Gallery Guide Updates
1+
name: Gallery Guide Site Module
22
type: module
3-
description: Site updates for The Gallery Guide
4-
core: 8.x
3+
description: Site tweaks and updates for The Gallery Guide
4+
core_version_requirement: ^8.8 || ^9
55
package: The Gallery Guide

gallerysite.install

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3+
use Drupal\Core\Messenger\MessengerInterface as MessengerInterface;
4+
35
function _gallerysite_replace_internal_url($table, $column, $field, $entity_type) {
46
// Get all websites where the URL starts with a slash.
5-
$db = \Drupal\Core\Database\Database::getConnection();
7+
$db = \Drupal::database();
68
$query = $db->select($table, 'fw')
79
->fields('fw', array('entity_id', $column));
810
$query->condition($column, "%" . $query->escapeLike('internal') . "%", 'LIKE');
@@ -41,9 +43,10 @@ function gallerysite_update_8002() {
4143
* Delete tags with no related content.
4244
*/
4345
function gallerysite_update_8003() {
44-
$result = $query = db_query("SELECT tid, name FROM {taxonomy_term_field_data} WHERE vid = 'tags'
45-
AND tid NOT IN (SELECT tid FROM {taxonomy_index})
46-
AND tid NOT IN (SELECT field_tags_target_id FROM {taxonomy_term__field_tags})
46+
$connection = \Drupal::database();
47+
$result = $query = $connection->query("SELECT tid, name FROM {taxonomy_term_field_data} WHERE vid = 'tags'
48+
AND tid NOT IN (SELECT tid FROM {taxonomy_index})
49+
AND tid NOT IN (SELECT field_tags_target_id FROM {taxonomy_term__field_tags})
4750
AND tid NOT IN (SELECT field_tags_target_id FROM {node__field_tags})");
4851

4952
$count = 0;
@@ -55,6 +58,6 @@ function gallerysite_update_8003() {
5558
$message = 'Term ' . $row->tid . ' - ' . $row->name . ' deleted';
5659
\Drupal::logger('gallerysite')->notice($message);
5760
}
58-
59-
drupal_set_message($count . ' tags deleted');
61+
62+
\Drupal::messenger()->addMessage($count . ' tags deleted');
6063
}

gallerysite.module

+68-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Drupal\Core\Link;
4+
use Drupal\Core\Messenger\MessengerInterface;
45

56
/**
67
* Implements hook_form_FORM_ID_alter().
@@ -40,9 +41,11 @@ function gallerysite_term_submit(&$form, \Drupal\Core\Form\FormStateInterface $f
4041
* Implements hook_form_FORM_ID_alter().
4142
*/
4243
function gallerysite_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
44+
$user = \Drupal::currentUser();
45+
4346
$form['artists']['widget']['add_more']['#value'] = t('Add another artist');
4447
$form['tags']['widget']['add_more']['#value'] = t('Add another tag');
45-
48+
4649
// Hide revision log fields
4750
$form['revision']['#access'] = FALSE;
4851
$form['revision_log']['#access'] = FALSE;
@@ -52,29 +55,79 @@ function gallerysite_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInt
5255

5356
// Remove votes open/closed.
5457
$form['field_rating']['#access'] = FALSE;
55-
58+
5659
// 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');
5861

5962
// Add a class if the address has no default value.
6063
if (!empty($form['field_address']) && empty($form['field_address']['widget'][0]['address']['#default_value']['locality'])) {
6164
$form['field_address']['#attributes']['class'][] = 'field--widget-address-blank';
6265
}
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);
6389
}
6490

6591
/**
6692
* Implements hook_form_FORM_ID_alter().
6793
*/
6894
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+
}
70106

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();
71121
}
72122

73123
/**
74124
* Implements hook_ENTITY_TYPE_insert().
75125
*/
76126
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+
}
78131
}
79132

80133
/**
@@ -99,7 +152,7 @@ function gallerysite_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInt
99152
function gallerysite_form_user_register_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
100153
// Hide the content form options.
101154
$form['contact']['#access'] = FALSE;
102-
155+
103156
$link_options = ['absolute' => TRUE];
104157

105158
$terms_nid = 6345;
@@ -114,43 +167,43 @@ function gallerysite_form_user_register_form_alter(&$form, \Drupal\Core\Form\For
114167
function gallerysite_default_image() {
115168

116169
$connection = Database::getConnection();
117-
170+
118171
$query = $connection->select('file_managed', 'fm')
119172
->condition('uri', 'public://imagefield_default_images/anon_large.gif')
120173
->fields('fm', array('fid', 'uuid'))
121174
->execute();
122-
175+
123176
// $connection->update('file_managed', 'fm')
124177
// ->condition('fid')
125178

126-
179+
127180
}
128181

129182
/**
130183
* Implements hook_cron().
131184
*/
132185
function gallerysite_cron() {
133186
// 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());
135188
$query = \Drupal::entityQuery('node')
136189
->condition('type', 'exhibition')
137190
->condition('status', 1)
138191
->condition('promote', 1)
139192
->condition('field_date_to.value', $date, '<');
140-
193+
141194
$nids = $query->execute();
142-
195+
143196
// Unpromote the nodes.
144197
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
145198
foreach ($nodes as $node) {
146199
$node->set('promote', 0);
147200
$node->save();
148201
}
149-
202+
150203
// Truncate tables that grow too large.
151204
$connection = \Drupal::database();
152205
$tables = array(
153-
'cache_render',
206+
'cache_render',
154207
'cache_dynamic_page_cache',
155208
);
156209
foreach ($tables as $table) {
@@ -202,7 +255,7 @@ function _recursively_alter_query_conditions(&$conditions) {
202255

203256
/**
204257
* Actually alter the condition if relevant.
205-
*/
258+
*/
206259
function _alter_query_condition(&$condition) {
207260
if (isset($condition['field']) && ($condition['field'] === 'SUBSTRING(node_field_data.title, 1, 1) = :node_field_data_title')) {
208261
$condition['field'] = "SUBSTRING(views_natural_sort.content, 1, 1) = :node_field_data_title";

gallerysite.services.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
theme.negotiator.galledit_theme:
33
class: Drupal\gallerysite\Theme\GalleryThemeNegotiator
4-
arguments: ['@current_user', '@config.factory', '@entity.manager', '@router.admin_context']
4+
arguments: ['@current_user', '@config.factory', '@entity_type.manager', '@router.admin_context']
55
tags:
66
- { name: theme_negotiator, priority: 1000 }

src/Theme/GalleryThemeNegotiator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GalleryThemeNegotiator implements ThemeNegotiatorInterface {
1414
* {@inheritdoc}
1515
*/
1616
public function applies(RouteMatchInterface $route_match) {
17-
17+
1818
// Use this theme on a certain route.
1919
$edit_routes = array(
2020
'node.add',

0 commit comments

Comments
 (0)