comparison core/modules/media/media.install @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
3 /** 3 /**
4 * @file 4 * @file
5 * Install, uninstall and update hooks for Media module. 5 * Install, uninstall and update hooks for Media module.
6 */ 6 */
7 7
8 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
9 use Drupal\Core\File\Exception\FileException;
10 use Drupal\Core\File\FileSystemInterface;
8 use Drupal\Core\Url; 11 use Drupal\Core\Url;
9 use Drupal\media\MediaTypeInterface; 12 use Drupal\media\MediaTypeInterface;
10 use Drupal\media\Plugin\media\Source\OEmbedInterface; 13 use Drupal\media\Plugin\media\Source\OEmbedInterface;
14 use Drupal\user\Entity\Role;
11 use Drupal\user\RoleInterface; 15 use Drupal\user\RoleInterface;
12 use Drupal\user\Entity\Role;
13 16
14 /** 17 /**
15 * Implements hook_install(). 18 * Implements hook_install().
16 */ 19 */
17 function media_install() { 20 function media_install() {
18 $source = drupal_get_path('module', 'media') . '/images/icons'; 21 $source = drupal_get_path('module', 'media') . '/images/icons';
19 $destination = \Drupal::config('media.settings')->get('icon_base_uri'); 22 $destination = \Drupal::config('media.settings')->get('icon_base_uri');
20 file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); 23 /** @var \Drupal\Core\File\FileSystemInterface $file_system */
24 $file_system = \Drupal::service('file_system');
25 $file_system->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
21 26
22 $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/'); 27 $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/');
23 foreach ($files as $file) { 28 foreach ($files as $file) {
24 // When reinstalling the media module we don't want to copy the icons when 29 // When reinstalling the media module we don't want to copy the icons when
25 // they already exist. The icons could be replaced (by a contrib module or 30 // they already exist. The icons could be replaced (by a contrib module or
26 // manually), so we don't want to replace the existing files. Removing the 31 // manually), so we don't want to replace the existing files. Removing the
27 // files when we uninstall could also be a problem if the files are 32 // files when we uninstall could also be a problem if the files are
28 // referenced somewhere else. Since showing an error that it was not 33 // referenced somewhere else. Since showing an error that it was not
29 // possible to copy the files is also confusing, we silently do nothing. 34 // possible to copy the files is also confusing, we silently do nothing.
30 if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) { 35 if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) {
31 file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_ERROR); 36 try {
37 $file_system->copy($file->uri, $destination, FileSystemInterface::EXISTS_ERROR);
38 }
39 catch (FileException $e) {
40 // Ignore and continue.
41 }
42
32 } 43 }
33 } 44 }
34 45
35 // Grant the "view media" permission to all users by default. 46 // Grant the "view media" permission to all users by default.
36 if (\Drupal::moduleHandler()->moduleExists('user')) { 47 if (\Drupal::moduleHandler()->moduleExists('user')) {
44 */ 55 */
45 function media_requirements($phase) { 56 function media_requirements($phase) {
46 $requirements = []; 57 $requirements = [];
47 if ($phase == 'install') { 58 if ($phase == 'install') {
48 $destination = 'public://media-icons/generic'; 59 $destination = 'public://media-icons/generic';
49 file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); 60 \Drupal::service('file_system')->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
50 $is_writable = is_writable($destination); 61 $is_writable = is_writable($destination);
51 $is_directory = is_dir($destination); 62 $is_directory = is_dir($destination);
52 if (!$is_writable || !$is_directory) { 63 if (!$is_writable || !$is_directory) {
53 if (!$is_directory) { 64 if (!$is_directory) {
54 $error = t('The directory %directory does not exist.', ['%directory' => $destination]); 65 $error = t('The directory %directory does not exist.', ['%directory' => $destination]);
161 \Drupal::configFactory()->getEditable('media.settings') 172 \Drupal::configFactory()->getEditable('media.settings')
162 ->set('iframe_domain', '') 173 ->set('iframe_domain', '')
163 ->set('oembed_providers_url', 'https://oembed.com/providers.json') 174 ->set('oembed_providers_url', 'https://oembed.com/providers.json')
164 ->save(TRUE); 175 ->save(TRUE);
165 } 176 }
177
178 /**
179 * Set the 'owner' entity key and update the field.
180 */
181 function media_update_8700() {
182 $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
183 $entity_type = $definition_update_manager->getEntityType('media');
184 $database = \Drupal::database();
185
186 if (\Drupal::entityTypeManager()->getStorage('media') instanceof SqlEntityStorageInterface) {
187 if ($database->schema()->tableExists($entity_type->getDataTable())) {
188 $database->update($entity_type->getDataTable())
189 ->fields(['uid' => 0])
190 ->isNull('uid')
191 ->execute();
192 }
193
194 if ($database->schema()->tableExists($entity_type->getRevisionDataTable())) {
195 $database->update($entity_type->getRevisionDataTable())
196 ->fields(['uid' => 0])
197 ->isNull('uid')
198 ->execute();
199 }
200 }
201
202 $keys = $entity_type->getKeys();
203 $keys['owner'] = 'uid';
204 $entity_type->set('entity_keys', $keys);
205 $definition_update_manager->updateEntityType($entity_type);
206 $definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'media'));
207 }