-
Notifications
You must be signed in to change notification settings - Fork 1
/
wmmedia.module
265 lines (229 loc) · 7.53 KB
/
wmmedia.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
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
use Drupal\media\Entity\Media;
use Drupal\wmmedia\Install\MediaBaseFieldDefinitions;
use Drupal\wmmedia\Plugin\Field\FieldType\MediaImageExtras;
use Drupal\wmmedia\Service\UsageManager;
/**
* Implements @see hook_theme().
*/
function wmmedia_theme(): array
{
$data = [];
$data['wmmedia_gallery'] = [
'template' => 'wmmedia-gallery',
'variables' => [
'_data' => [],
],
'preprocess functions' => [
'wmmedia_theme_set_variables',
],
];
$data['wmmedia_image'] = [
'template' => 'wmmedia-image',
'variables' => [
'field' => null,
'image_style' => 'default',
],
];
return $data;
}
function wmmedia_theme_set_variables(&$variables)
{
// Skip if no data is set or not set as an array
if (!isset($variables['_data']) || !is_array($variables['_data'])) {
return;
}
$variables = array_merge($variables, $variables['_data']);
unset($variables['_data']);
}
/**
* Implements @see hook_preprocess_HOOK().
*/
function wmmedia_preprocess_wmmedia_image(array &$variables): void
{
if (!isset($variables['field']) || !$variables['field'] instanceof MediaImageExtras) {
return;
}
if (!$file = $variables['field']->getFile()) {
return;
}
$path = $file->getFileUri();
$imageStyleName = $variables['image_style'] ?? $variables['preset'] ?? 'medium';
if (!$imageStyle = ImageStyle::load($variables['image_style'])) {
return;
}
if (!$imageStyle->supportsUri($path)) {
return;
}
$variables['uri'] = \Drupal::service('file_url_generator')->transformRelative($imageStyle->buildUrl($path));
$variables['alternate'] = $variables['field']->getAlternate();
$variables['caption'] = $variables['field']->getDescription();
$variables['copyright'] = $variables['field']->getCopyright();
$variables['title'] = $variables['field']->getTitle();
$variables['image_style'] = $imageStyleName;
}
/**
* Implements @see hook_menu_local_actions_alter().
*/
function wmmedia_menu_local_actions_alter(&$local_actions)
{
if (isset($local_actions['media.add'])) {
$appearsOn = &$local_actions['media.add']['appears_on'];
$key = array_search('entity.media.collection', $appearsOn, true);
if ($key !== false) {
unset($appearsOn[$key]);
}
}
}
/**
* Implements @see hook_locale_translation_projects_alter().
*/
function wmmedia_locale_translation_projects_alter(&$projects)
{
$path = \Drupal::getContainer()->get('extension.path.resolver')->getPath('module', 'wmmedia') . '/translations/%language.po';
$projects['wmmedia']['info']['interface translation server pattern'] = $path;
}
/**
* Implements @see hook_entity_operation_alter().
*/
function wmmedia_entity_operation_alter(array &$operations, EntityInterface $entity)
{
if ($entity instanceof Media) {
/* @var UsageManager $service */
$service = \Drupal::service('wmmedia.usage');
$service->setOperations($entity, $operations);
}
}
/**
* Implements @see hook_form_FORM_ID_alter().
*/
function wmmedia_form_media_form_alter(&$form, FormStateInterface $formState, $formId)
{
\Drupal::getContainer()
->get('wmmedia.form_alter.subscriber')
->mediaFormAlter($form, $formState);
}
/**
* Implements @see hook_form_FORM_ID_alter().
*/
function wmmedia_form_media_file_delete_form_alter(&$form, FormStateInterface $formState)
{
\Drupal::getContainer()
->get('wmmedia.usage.subscriber.form')
->addWarning($form, $formState);
}
/**
* Implements @see hook_inline_entity_form_entity_form_alter().
*/
function wmmedia_inline_entity_form_entity_form_alter(array &$form, FormStateInterface $formState)
{
\Drupal::getContainer()
->get('wmmedia.form_alter.subscriber')
->entityBrowserImagesFormAlter($form, $formState);
}
/**
* Implements @see hook_entity_presave().
*/
function wmmedia_entity_presave(EntityInterface $entity)
{
\Drupal::getContainer()
->get('wmmedia.entity.image.subscriber')
->onPreSave($entity);
\Drupal::getContainer()
->get('wmmedia.entity_form_display.subscriber')
->onPreSave($entity);
}
/**
* Implements @see hook_entity_delete().
*/
function wmmedia_entity_delete(EntityInterface $entity)
{
\Drupal::getContainer()
->get('wmmedia.entity.delete.subscriber')
->deleteFile($entity);
\Drupal::getContainer()
->get('wmmedia.usage.subscriber.entity')
->clearUsage($entity);
}
/**
* Implements @see hook_entity_insert().
*/
function wmmedia_entity_insert(EntityInterface $entity)
{
\Drupal::getContainer()
->get('wmmedia.usage.subscriber.entity')
->trackUsage($entity);
}
/**
* Implements @see hook_entity_update().
*/
function wmmedia_entity_update(EntityInterface $entity)
{
\Drupal::getContainer()
->get('wmmedia.usage.subscriber.entity')
->trackUsage($entity);
}
/**
* Implements @see hook_library_info_alter().
*/
function wmmedia_library_info_alter(array &$libraries, string $extension): void
{
if ($extension !== 'wmmedia') {
return;
}
if (_wmmedia_theme_is_active('gin')) {
unset($libraries['image_browser']['css']['theme']['assets/dist/image_browser_checkbox.css']);
}
}
/**
* Implements hook_entity_base_field_info().
*/
function wmmedia_entity_base_field_info(EntityTypeInterface $entityType): array
{
if ($entityType->id() !== 'media') {
return [];
}
return MediaBaseFieldDefinitions::getBaseFieldDefinitions();
}
function _wmmedia_theme_is_active(string $themeNameToCheck): bool
{
$themeHandler = \Drupal::service('theme_handler')->listInfo();
$themeConfig = \Drupal::config('system.theme');
$themeNames = [];
foreach (['default', 'admin'] as $type) {
if (!$themeName = $themeConfig->get($type)) {
continue;
}
$themeNames[$themeName] = $themeName;
if (isset($themeHandler[$themeName]->base_themes)) {
$themeNames = array_merge($themeNames, $themeHandler[$themeName]->base_themes);
}
}
return array_key_exists($themeNameToCheck, $themeNames);
}
function wmmedia_ckeditor5_entity_browser_definitions_alter(array &$definitions): void
{
// drupal/ckeditor5_entity_browser uses the configured entity browser's
// Display URL (iframe/modal/etc) when opening the ckeditor dialog.
// We already have a custom route that's meant to be opened in a dialog,
// so let's use that instead.
// This is a workaround until we can provide a better solution, such as
// creating our own EntityBrowserDisplay plugin for entity_browser.
foreach ($definitions as $entity_browser_id => $config) {
// These are the entity browsers that wmmedia currently provides.
if (in_array($entity_browser_id, ['files_editor', 'files'])) {
// We also want to use a different label for the button that opens
// the dialog.
$definitions[$entity_browser_id]['label'] = t('Add Files');
// Alter the dialog url to use our custom route.
// This requires https://www.drupal.org/project/ckeditor5_entity_browser/issues/3445512
$definitions[$entity_browser_id]['browser_display_url'] = Url::fromRoute('wmmedia.file.browser.editor')->toString();
}
}
}