Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / media / media.install
1 <?php
2
3 /**
4  * @file
5  * Install, uninstall and update hooks for Media module.
6  */
7
8 use Drupal\user\RoleInterface;
9 use Drupal\user\Entity\Role;
10
11 /**
12  * Implements hook_install().
13  */
14 function media_install() {
15   $source = drupal_get_path('module', 'media') . '/images/icons';
16   $destination = \Drupal::config('media.settings')->get('icon_base_uri');
17   file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
18
19   $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/');
20   foreach ($files as $file) {
21     // When reinstalling the media module we don't want to copy the icons when
22     // they already exist. The icons could be replaced (by a contrib module or
23     // manually), so we don't want to replace the existing files. Removing the
24     // files when we uninstall could also be a problem if the files are
25     // referenced somewhere else. Since showing an error that it was not
26     // possible to copy the files is also confusing, we silently do nothing.
27     if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) {
28       file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_ERROR);
29     }
30   }
31
32   // Grant the "view media" permission to all users by default.
33   if (\Drupal::moduleHandler()->moduleExists('user')) {
34     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view media']);
35     user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view media']);
36   }
37 }
38
39 /**
40  * Implements hook_requirements().
41  */
42 function media_requirements($phase) {
43   $requirements = [];
44   if ($phase == 'install') {
45     $destination = 'public://media-icons/generic';
46     file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
47     $is_writable = is_writable($destination);
48     $is_directory = is_dir($destination);
49     if (!$is_writable || !$is_directory) {
50       if (!$is_directory) {
51         $error = t('The directory %directory does not exist.', ['%directory' => $destination]);
52       }
53       else {
54         $error = t('The directory %directory is not writable.', ['%directory' => $destination]);
55       }
56       $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']);
57       if (!empty($error)) {
58         $description = $error . ' ' . $description;
59         $requirements['media']['description'] = $description;
60         $requirements['media']['severity'] = REQUIREMENT_ERROR;
61       }
62     }
63
64     // Prevent installation if the 1.x branch of the contrib module is enabled.
65     if (\Drupal::moduleHandler()->moduleExists('media_entity')) {
66       $info = system_get_info('module', 'media_entity');
67       if (version_compare($info['version'], '8.x-2') < 0) {
68         $requirements['media_module_incompatibility'] = [
69           'title' => t('Media'),
70           '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.', [
71             ':url' => 'https://drupal.org/project/media_entity',
72           ]),
73           'severity' => REQUIREMENT_ERROR,
74         ];
75       }
76     }
77   }
78
79   return $requirements;
80 }
81
82 /**
83  * Introduce per-bundle permissions.
84  */
85 function media_update_8500() {
86   $media_types = \Drupal::entityQuery('media_type')->execute();
87
88   /** @var \Drupal\user\RoleInterface $role */
89   foreach (Role::loadMultiple() as $role) {
90     if ($role->hasPermission('update media')) {
91       foreach ($media_types as $media_type) {
92         $role->grantPermission("edit own $media_type media");
93       }
94     }
95
96     if ($role->hasPermission('update any media')) {
97       foreach ($media_types as $media_type) {
98         $role->grantPermission("edit any $media_type media");
99       }
100     }
101
102     if ($role->hasPermission('delete media')) {
103       foreach ($media_types as $media_type) {
104         $role->grantPermission("delete own $media_type media");
105       }
106     }
107
108     if ($role->hasPermission('delete any media')) {
109       foreach ($media_types as $media_type) {
110         $role->grantPermission("delete any $media_type media");
111       }
112     }
113
114     if ($role->hasPermission('create media')) {
115       foreach ($media_types as $media_type) {
116         $role->grantPermission("create $media_type media");
117       }
118     }
119
120     $role->save();
121   }
122 }