-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathckeditor_mentions.install
executable file
·171 lines (151 loc) · 5.07 KB
/
ckeditor_mentions.install
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
<?php
/**
* @file
* Contains installation and update scripts.
*/
use Drupal\editor\Entity\Editor;
use Drupal\image\Entity\ImageStyle;
/**
* Implements hook_install().
*/
function ckeditor_mentions_install() {
// Create tiny icon image style.
$style = ImageStyle::create(
[
'name' => 'mentions_icon',
'label' => 'CKEditor Mentions Icon',
]
);
// Create effect.
$configuration = [
'uuid' => NULL,
'id' => 'image_scale_and_crop',
'weight' => 0,
'data' => [
'width' => 60,
'height' => 60,
],
];
$effect = \Drupal::service('plugin.manager.image.effect')->createInstance($configuration['id'], $configuration);
// Add it to the image style and save.
$style->addImageEffect($effect->getConfiguration());
$style->save();
}
/**
* Implements hook_uninstall().
*/
function ckeditor_mentions_uninstall() {
// Remove tiny icon image style.
\Drupal::configFactory()->getEditable('image.style.mentions_icon')->delete();
}
/**
* Implements hook_requirements().
*/
function ckeditor_mentions_requirements($phase) {
$requirements = [];
if ($phase === 'runtime') {
$uninstalled_plugins = _ckeditor_mentions_get_uninstalled_plugins();
if (empty($uninstalled_plugins)) {
$requirements['mentions'] = [
'title' => t('CKEditor Mentions'),
'value' => t('Plugins detected'),
'severity' => REQUIREMENT_OK,
];
}
else {
$requirements['mentions'] = [
'title' => t('CKEditor Mentions'),
'value' => t('Plugin not detected'),
'severity' => REQUIREMENT_ERROR,
'description' => t('The following plugins are missing @list. Please download them and place them in "libraries/ckeditor/plugins/{plugin}" directory. Check the README.md for more information.',
['@list' => implode(', ', $uninstalled_plugins)]
),
];
}
}
return $requirements;
}
/**
* Get uninstalled plugins.
*
* @return array
* Array of uninstalled plugins.
*/
function _ckeditor_mentions_get_uninstalled_plugins() {
return [];
$uninstalled_plugins = [];
/** @var \Drupal\ckeditor5\Plugin\CKEditor5PluginManager $ckeditor_plugin_manager */
$ckeditor_plugin_manager = \Drupal::service('plugin.manager.ckeditor5.plugin');
/** @var \Drupal\ckeditor_mentions\Plugin\CKEditor5Plugin\Mentions $mentions_plugin */
$mentions_plugin = $ckeditor_plugin_manager->createInstance('ckeditor_mentions_mentions');
// We just need to mock-up editor, and to make sure that it exists.
// getDependencies mentions plugin method doesn't use editor at all.
$editor = Editor::create(['editor' => 'ckeditor']);
$dependencies = $mentions_plugin->getDependencies($editor);
foreach ($dependencies + ['mentions'] as $dependency) {
$instance = ($dependency != 'mentions') ? $ckeditor_plugin_manager->createInstance($dependency) : $mentions_plugin;
$file_path = $instance->getFile();
if (!file_exists($file_path)) {
$uninstalled_plugins[] = $dependency;
}
}
return $uninstalled_plugins;
}
/**
* Update configuration for project using realname module.
*
* For backward compatibility enable the ckeditor_mentions_realname (CMR)
* submodule, and set the realname mentions as default plugin.
*/
function ckeditor_mentions_update_8002() {
if (!\Drupal::moduleHandler()->moduleExists('ckeditor_mentions_realname')) {
\Drupal::service('module_installer')->install(['ckeditor_mentions_realname']);
}
_ckeditor_mentions_mentions_map(function (&$settings) {
$mentions_config = &$settings['plugins']['mentions'];
if (!isset($mentions_config['mentions_type'])) {
$mentions_config = [
'timeout' => '500',
'charcount' => '3',
'mentions_type' => 'realname',
'enable' => TRUE,
];
return TRUE;
}
return FALSE;
});
}
/**
* Adds missing additional configurations.
*/
function ckeditor_mentions_update_8003() {
_ckeditor_mentions_mentions_map(function (&$settings) {
$mentions_config = &$settings['plugins']['mentions'];
if (!isset($mentions_config['marker'])) {
$mentions_config['marker'] = '@';
$mentions_config['pattern'] = '';
$mentions_config['advanced_pattern'] = '';
$mentions_config['use_advanced_pattern'] = FALSE;
return TRUE;
}
return FALSE;
});
}
/**
* Applies the callback to all enabled 'mentions' plugins.
*/
function _ckeditor_mentions_mentions_map(callable $settings_processor) {
$editors = \Drupal::entityTypeManager()->getStorage('editor')->loadMultiple();
/** @var \Drupal\ckeditor5\Plugin\CKEditor5PluginManager $ckeditor_plugin_manager */
$ckeditor_plugin_manager = \Drupal::service('plugin.manager.ckeditor5.plugin');
/** @var \Drupal\ckeditor_mentions\Plugin\CKEditorPlugin\Mentions $mentions_plugin */
$mentions_plugin = $ckeditor_plugin_manager->createInstance('mentions');
/** @var \Drupal\editor\Entity\Editor $editor */
foreach ($editors as $editor) {
$settings = $editor->getSettings();
if ($mentions_plugin->isEnabled($editor) && $settings_processor($settings)) {
$editor->setSettings($settings);
$editor->save();
}
}
}