-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite_gallery.module
278 lines (239 loc) · 8.95 KB
/
site_gallery.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* @file
* Main file for the site_gallery.module.
*/
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Render\Element;
use Drupal\node\Entity\Node;
/**
* Implements hook_theme().
*/
function site_gallery_theme($existing, $type, $theme, $path) {
return [
'taxonomy_term__site_gallery' => [
'variables' => [],
'template' => 'taxonomy-term--site-gallery',
],
'node__site_gallery' => [
'render element' => 'elements',
'template' => 'node--site-gallery',
],
'site_gallery_default_image_formatter' => [
'variables' => ['item' => NULL, 'item_attributes' => NULL, 'url' => NULL, 'image_style' => NULL],
],
'site_gallery_main_image_formatter' => [
'variables' => ['item' => NULL, 'item_attributes' => NULL, 'url' => NULL, 'image_style' => NULL],
],
'site_gallery_taxonomy_term' => [
'variables' => ['view' => NULL, 'rows' => NULL],
],
'views_view_field__site_gallery__page__name' => [
'variables' => ['view' => NULL, 'field' => NULL, 'row' => NULL],
'template' => 'views-view-field--site-gallery--page--name',
],
];
}
/**
* Implements hook_page_attachments().
*/
function site_gallery_page_attachments(array &$attachments) {
$current_path = \Drupal::service('path.current')->getPath();
if (strpos($current_path, "/gallery/") >= 0) {
$attachments['#attached']['library'][] = 'site_gallery/module';
}
}
/**
* Implements hook_entity_presave().
*/
function site_gallery_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'node') {
if ($entity->bundle() == 'site_gallery') {
$images = $entity->field_image->getValue();
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
foreach ($images as $image) {
if (!empty($image['alt'])) {
$fid = $image['target_id'];
$file = \Drupal\file\Entity\File::load($fid);
// Расширение файла.
$image_factory = \Drupal::service('image.factory')->get($file->getFileUri());
$extension = image_type_to_extension($image_factory->getToolkit()->getType(), FALSE);
// Проверяем изменилось ли описание файла.
$filename = $file->getFilename();
$filename_without_extension = rtrim($filename, '.' . $extension);
if ($filename_without_extension != $image['alt']) {
// Получаем текущий URI без имени файла.
$file_lenght = strlen($filename);
$uri = $file->getFileUri();
$uri_dirname = pathinfo($uri, PATHINFO_DIRNAME);
// Получаем новое имя файла.
$filename = $image['alt'] . '.' . $extension;
$filename = \Drupal::transliteration()->transliterate($filename, $langcode, '');
// Очистка имени файла.
$filename = str_replace(' ', '-', $filename);
$filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);
$filename = preg_replace('/(_)_+|(\.)\.+|(-)-+/', '\\1\\2\\3', $filename);
$filename = Unicode::strtolower($filename);
// Переименовываем файл.
$destination = $uri_dirname . '/' . $filename;
$file = file_move($file, $destination, FILE_EXISTS_RENAME);
// Устанавливаем новое имя файла в БД.
$file->setFilename($image['alt'] . '.' . $extension);
$file->save();
}
}
}
}
}
}
/**
* Prepares variables for taxonomy-term--site-gallery.html.twig template.
*
* @param array $variables
*/
function template_preprocess_taxonomy_term__site_gallery(&$variables) {
// Описание категории.
$term = $variables['elements']['#taxonomy_term'];
$variables['term_description'] = $term->getDescription();
}
/**
* Prepares variables for node--site-gallery.html.twig template.
*
* @param array $variables
*/
function template_preprocess_node__site_gallery(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['teaser'] = $variables['view_mode'] == 'teaser';
$variables['node'] = $variables['elements']['#node'];
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['node'];
$variables['date'] = \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'long');
unset($variables['elements']['created']);
$variables['author_name'] = drupal_render($variables['elements']['uid']);
unset($variables['elements']['uid']);
$variables['url'] = $node->url('canonical', array(
'language' => $node->language(),
));
$variables['label'] = $variables['elements']['title'];
unset($variables['elements']['title']);
$variables['page'] = ($variables['view_mode'] == 'full' && (node_is_page($node)) || (isset($node->in_preview) && in_array($node->preview_view_mode, array('full', 'default'))));
$variables += array('content' => array());
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
$variables['attributes']['role'] = 'article';
// Изображения фотоальбома.
if ($variables['page']) {
$variables['images'] = [];
if (isset($variables['content']['field_image']['#items'])) {
$variables['count_image'] = $variables['content']['field_image']['#items']->count();
$i = 0;
while ($i <= $variables['count_image'] - 1):
$variables['images'][$i] = $variables['content']['field_image'][$i];
$i++;
endwhile;
}
} else {
$variables['content']['field_image'][0]['#theme'] = 'site_gallery_main_image_formatter';
}
}
/**
* Implements template_preprocess_site_gallery_default_image_formatter().
*/
function template_preprocess_site_gallery_default_image_formatter(&$variables) {
if ($variables['image_style']) {
$variables['image'] = [
'#theme' => 'image_style',
'#style_name' => $variables['image_style'],
];
} else {
$variables['image'] = [
'#theme' => 'image',
];
}
$variables['image']['#attributes'] = $variables['item_attributes'];
$item = $variables['item'];
if (Unicode::strlen($item->title) != 0) {
$variables['image']['#title'] = $item->title;
}
if (($entity = $item->entity) && empty($item->uri)) {
$variables['image']['#uri'] = $entity->getFileUri();
} else {
$variables['image']['#uri'] = $item->uri;
}
foreach (['width', 'height', 'alt'] as $key) {
$variables['image']["#$key"] = $item->$key;
}
$variables['is_node'] = FALSE;
if (\Drupal::routeMatch()->getRouteName() == 'entity.node.canonical') {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node->getTitle() != $variables['image']["#alt"]) {
$variables['is_node'] = TRUE;
}
}
unset($variables['image']['#width']);
unset($variables['image']['#height']);
}
/**
* Implements template_preprocess_site_gallery_main_image_formatter().
*/
function template_preprocess_site_gallery_main_image_formatter(&$variables) {
if ($variables['image_style']) {
$variables['image'] = [
'#theme' => 'image_style',
'#style_name' => $variables['image_style'],
];
} else {
$variables['image'] = [
'#theme' => 'image',
];
}
$variables['image']['#attributes'] = $variables['item_attributes'];
$item = $variables['item'];
if (Unicode::strlen($item->title) != 0) {
$variables['image']['#title'] = $item->title;
}
if (($entity = $item->entity) && empty($item->uri)) {
$variables['image']['#uri'] = $entity->getFileUri();
} else {
$variables['image']['#uri'] = $item->uri;
}
foreach (['width', 'height', 'alt'] as $key) {
$variables['image']["#$key"] = $item->$key;
}
$variables['is_node'] = FALSE;
if (\Drupal::routeMatch()->getRouteName() == 'entity.node.canonical') {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node->getTitle() != $variables['image']["#alt"]) {
$variables['is_node'] = TRUE;
}
}
unset($variables['image']['#width']);
unset($variables['image']['#height']);
}
/**
* Prepares variables for views site-gallery-taxonomy-term template.
*
* Template: site-gallery-taxonomy-term.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The view object.
* - rows: An array of row items. Each row is an array of content.
*/
function template_preprocess_site_gallery_taxonomy_term(&$variables) {
template_preprocess_views_view_unformatted($variables);
}
/**
* Prepares variables for views views-view-field--site_gallery--page--name template.
*
* Template: views-view-field--site_gallery--page--name.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The view object.
* - rows: An array of row items. Each row is an array of content.
*/
function template_preprocess_views_view_field__site_gallery__page__name(&$variables) {
template_preprocess_views_view_field($variables);
}