Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Install, uninstall and update hooks for Media module.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@18
|
8 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
|
Chris@18
|
9 use Drupal\Core\File\Exception\FileException;
|
Chris@18
|
10 use Drupal\Core\File\FileSystemInterface;
|
Chris@17
|
11 use Drupal\Core\Url;
|
Chris@17
|
12 use Drupal\media\MediaTypeInterface;
|
Chris@17
|
13 use Drupal\media\Plugin\media\Source\OEmbedInterface;
|
Chris@18
|
14 use Drupal\user\Entity\Role;
|
Chris@0
|
15 use Drupal\user\RoleInterface;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Implements hook_install().
|
Chris@0
|
19 */
|
Chris@0
|
20 function media_install() {
|
Chris@0
|
21 $source = drupal_get_path('module', 'media') . '/images/icons';
|
Chris@0
|
22 $destination = \Drupal::config('media.settings')->get('icon_base_uri');
|
Chris@18
|
23 /** @var \Drupal\Core\File\FileSystemInterface $file_system */
|
Chris@18
|
24 $file_system = \Drupal::service('file_system');
|
Chris@18
|
25 $file_system->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
|
Chris@0
|
26
|
Chris@0
|
27 $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/');
|
Chris@0
|
28 foreach ($files as $file) {
|
Chris@0
|
29 // When reinstalling the media module we don't want to copy the icons when
|
Chris@0
|
30 // they already exist. The icons could be replaced (by a contrib module or
|
Chris@0
|
31 // manually), so we don't want to replace the existing files. Removing the
|
Chris@0
|
32 // files when we uninstall could also be a problem if the files are
|
Chris@0
|
33 // referenced somewhere else. Since showing an error that it was not
|
Chris@0
|
34 // possible to copy the files is also confusing, we silently do nothing.
|
Chris@0
|
35 if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) {
|
Chris@18
|
36 try {
|
Chris@18
|
37 $file_system->copy($file->uri, $destination, FileSystemInterface::EXISTS_ERROR);
|
Chris@18
|
38 }
|
Chris@18
|
39 catch (FileException $e) {
|
Chris@18
|
40 // Ignore and continue.
|
Chris@18
|
41 }
|
Chris@18
|
42
|
Chris@0
|
43 }
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 // Grant the "view media" permission to all users by default.
|
Chris@0
|
47 if (\Drupal::moduleHandler()->moduleExists('user')) {
|
Chris@0
|
48 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view media']);
|
Chris@0
|
49 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view media']);
|
Chris@0
|
50 }
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Implements hook_requirements().
|
Chris@0
|
55 */
|
Chris@0
|
56 function media_requirements($phase) {
|
Chris@0
|
57 $requirements = [];
|
Chris@0
|
58 if ($phase == 'install') {
|
Chris@0
|
59 $destination = 'public://media-icons/generic';
|
Chris@18
|
60 \Drupal::service('file_system')->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
|
Chris@0
|
61 $is_writable = is_writable($destination);
|
Chris@0
|
62 $is_directory = is_dir($destination);
|
Chris@0
|
63 if (!$is_writable || !$is_directory) {
|
Chris@0
|
64 if (!$is_directory) {
|
Chris@0
|
65 $error = t('The directory %directory does not exist.', ['%directory' => $destination]);
|
Chris@0
|
66 }
|
Chris@0
|
67 else {
|
Chris@0
|
68 $error = t('The directory %directory is not writable.', ['%directory' => $destination]);
|
Chris@0
|
69 }
|
Chris@0
|
70 $description = t('An automated attempt to create this directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see INSTALL.txt or the <a href=":handbook_url">online handbook</a>.', [':handbook_url' => 'https://www.drupal.org/server-permissions']);
|
Chris@0
|
71 if (!empty($error)) {
|
Chris@0
|
72 $description = $error . ' ' . $description;
|
Chris@0
|
73 $requirements['media']['description'] = $description;
|
Chris@0
|
74 $requirements['media']['severity'] = REQUIREMENT_ERROR;
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 // Prevent installation if the 1.x branch of the contrib module is enabled.
|
Chris@0
|
79 if (\Drupal::moduleHandler()->moduleExists('media_entity')) {
|
Chris@0
|
80 $info = system_get_info('module', 'media_entity');
|
Chris@0
|
81 if (version_compare($info['version'], '8.x-2') < 0) {
|
Chris@0
|
82 $requirements['media_module_incompatibility'] = [
|
Chris@0
|
83 'title' => t('Media'),
|
Chris@0
|
84 'description' => t('The Media module is not compatible with contrib <a href=":url">Media Entity</a> 1.x branch. Please check the 2.x branch of that module for an upgrade path.', [
|
Chris@0
|
85 ':url' => 'https://drupal.org/project/media_entity',
|
Chris@0
|
86 ]),
|
Chris@0
|
87 'severity' => REQUIREMENT_ERROR,
|
Chris@0
|
88 ];
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91 }
|
Chris@17
|
92 elseif ($phase === 'runtime') {
|
Chris@17
|
93 // Check that oEmbed content is served in an iframe on a different domain,
|
Chris@17
|
94 // and complain if it isn't.
|
Chris@17
|
95 $domain = \Drupal::config('media.settings')->get('iframe_domain');
|
Chris@17
|
96
|
Chris@17
|
97 if (!\Drupal::service('media.oembed.iframe_url_helper')->isSecure($domain)) {
|
Chris@17
|
98 // Find all media types which use a source plugin that implements
|
Chris@17
|
99 // OEmbedInterface.
|
Chris@17
|
100 $media_types = \Drupal::entityTypeManager()
|
Chris@17
|
101 ->getStorage('media_type')
|
Chris@17
|
102 ->loadMultiple();
|
Chris@17
|
103
|
Chris@17
|
104 $oembed_types = array_filter($media_types, function (MediaTypeInterface $media_type) {
|
Chris@17
|
105 return $media_type->getSource() instanceof OEmbedInterface;
|
Chris@17
|
106 });
|
Chris@17
|
107
|
Chris@17
|
108 if ($oembed_types) {
|
Chris@17
|
109 // @todo Potentially allow site administrators to suppress this warning
|
Chris@17
|
110 // permanently. See https://www.drupal.org/project/drupal/issues/2962753
|
Chris@17
|
111 // for more information.
|
Chris@17
|
112 $requirements['media_insecure_iframe'] = [
|
Chris@17
|
113 'title' => t('Media'),
|
Chris@17
|
114 'description' => t('It is potentially insecure to display oEmbed content in a frame that is served from the same domain as your main Drupal site, as this may allow execution of third-party code. <a href=":url">You can specify a different domain for serving oEmbed content here</a>.', [
|
Chris@17
|
115 ':url' => Url::fromRoute('media.settings')->setAbsolute()->toString(),
|
Chris@17
|
116 ]),
|
Chris@17
|
117 'severity' => REQUIREMENT_WARNING,
|
Chris@17
|
118 ];
|
Chris@17
|
119 }
|
Chris@17
|
120 }
|
Chris@17
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 return $requirements;
|
Chris@0
|
124 }
|
Chris@14
|
125
|
Chris@14
|
126 /**
|
Chris@14
|
127 * Introduce per-bundle permissions.
|
Chris@14
|
128 */
|
Chris@14
|
129 function media_update_8500() {
|
Chris@14
|
130 $media_types = \Drupal::entityQuery('media_type')->execute();
|
Chris@14
|
131
|
Chris@14
|
132 /** @var \Drupal\user\RoleInterface $role */
|
Chris@14
|
133 foreach (Role::loadMultiple() as $role) {
|
Chris@14
|
134 if ($role->hasPermission('update media')) {
|
Chris@14
|
135 foreach ($media_types as $media_type) {
|
Chris@14
|
136 $role->grantPermission("edit own $media_type media");
|
Chris@14
|
137 }
|
Chris@14
|
138 }
|
Chris@14
|
139
|
Chris@14
|
140 if ($role->hasPermission('update any media')) {
|
Chris@14
|
141 foreach ($media_types as $media_type) {
|
Chris@14
|
142 $role->grantPermission("edit any $media_type media");
|
Chris@14
|
143 }
|
Chris@14
|
144 }
|
Chris@14
|
145
|
Chris@14
|
146 if ($role->hasPermission('delete media')) {
|
Chris@14
|
147 foreach ($media_types as $media_type) {
|
Chris@14
|
148 $role->grantPermission("delete own $media_type media");
|
Chris@14
|
149 }
|
Chris@14
|
150 }
|
Chris@14
|
151
|
Chris@14
|
152 if ($role->hasPermission('delete any media')) {
|
Chris@14
|
153 foreach ($media_types as $media_type) {
|
Chris@14
|
154 $role->grantPermission("delete any $media_type media");
|
Chris@14
|
155 }
|
Chris@14
|
156 }
|
Chris@14
|
157
|
Chris@14
|
158 if ($role->hasPermission('create media')) {
|
Chris@14
|
159 foreach ($media_types as $media_type) {
|
Chris@14
|
160 $role->grantPermission("create $media_type media");
|
Chris@14
|
161 }
|
Chris@14
|
162 }
|
Chris@14
|
163
|
Chris@14
|
164 $role->save();
|
Chris@14
|
165 }
|
Chris@14
|
166 }
|
Chris@17
|
167
|
Chris@17
|
168 /**
|
Chris@17
|
169 * Updates media.settings to support OEmbed.
|
Chris@17
|
170 */
|
Chris@17
|
171 function media_update_8600() {
|
Chris@17
|
172 \Drupal::configFactory()->getEditable('media.settings')
|
Chris@17
|
173 ->set('iframe_domain', '')
|
Chris@17
|
174 ->set('oembed_providers_url', 'https://oembed.com/providers.json')
|
Chris@17
|
175 ->save(TRUE);
|
Chris@17
|
176 }
|
Chris@18
|
177
|
Chris@18
|
178 /**
|
Chris@18
|
179 * Set the 'owner' entity key and update the field.
|
Chris@18
|
180 */
|
Chris@18
|
181 function media_update_8700() {
|
Chris@18
|
182 $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
Chris@18
|
183 $entity_type = $definition_update_manager->getEntityType('media');
|
Chris@18
|
184 $database = \Drupal::database();
|
Chris@18
|
185
|
Chris@18
|
186 if (\Drupal::entityTypeManager()->getStorage('media') instanceof SqlEntityStorageInterface) {
|
Chris@18
|
187 if ($database->schema()->tableExists($entity_type->getDataTable())) {
|
Chris@18
|
188 $database->update($entity_type->getDataTable())
|
Chris@18
|
189 ->fields(['uid' => 0])
|
Chris@18
|
190 ->isNull('uid')
|
Chris@18
|
191 ->execute();
|
Chris@18
|
192 }
|
Chris@18
|
193
|
Chris@18
|
194 if ($database->schema()->tableExists($entity_type->getRevisionDataTable())) {
|
Chris@18
|
195 $database->update($entity_type->getRevisionDataTable())
|
Chris@18
|
196 ->fields(['uid' => 0])
|
Chris@18
|
197 ->isNull('uid')
|
Chris@18
|
198 ->execute();
|
Chris@18
|
199 }
|
Chris@18
|
200 }
|
Chris@18
|
201
|
Chris@18
|
202 $keys = $entity_type->getKeys();
|
Chris@18
|
203 $keys['owner'] = 'uid';
|
Chris@18
|
204 $entity_type->set('entity_keys', $keys);
|
Chris@18
|
205 $definition_update_manager->updateEntityType($entity_type);
|
Chris@18
|
206 $definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'media'));
|
Chris@18
|
207 }
|