5 * Install, uninstall and update hooks for Media module.
9 use Drupal\media\MediaTypeInterface;
10 use Drupal\media\Plugin\media\Source\OEmbedInterface;
11 use Drupal\user\RoleInterface;
12 use Drupal\user\Entity\Role;
15 * Implements hook_install().
17 function media_install() {
18 $source = drupal_get_path('module', 'media') . '/images/icons';
19 $destination = \Drupal::config('media.settings')->get('icon_base_uri');
20 file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
22 $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/');
23 foreach ($files as $file) {
24 // 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
26 // 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
28 // referenced somewhere else. Since showing an error that it was not
29 // possible to copy the files is also confusing, we silently do nothing.
30 if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) {
31 file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_ERROR);
35 // Grant the "view media" permission to all users by default.
36 if (\Drupal::moduleHandler()->moduleExists('user')) {
37 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view media']);
38 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view media']);
43 * Implements hook_requirements().
45 function media_requirements($phase) {
47 if ($phase == 'install') {
48 $destination = 'public://media-icons/generic';
49 file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
50 $is_writable = is_writable($destination);
51 $is_directory = is_dir($destination);
52 if (!$is_writable || !$is_directory) {
54 $error = t('The directory %directory does not exist.', ['%directory' => $destination]);
57 $error = t('The directory %directory is not writable.', ['%directory' => $destination]);
59 $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']);
61 $description = $error . ' ' . $description;
62 $requirements['media']['description'] = $description;
63 $requirements['media']['severity'] = REQUIREMENT_ERROR;
67 // Prevent installation if the 1.x branch of the contrib module is enabled.
68 if (\Drupal::moduleHandler()->moduleExists('media_entity')) {
69 $info = system_get_info('module', 'media_entity');
70 if (version_compare($info['version'], '8.x-2') < 0) {
71 $requirements['media_module_incompatibility'] = [
72 'title' => t('Media'),
73 '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.', [
74 ':url' => 'https://drupal.org/project/media_entity',
76 'severity' => REQUIREMENT_ERROR,
81 elseif ($phase === 'runtime') {
82 // Check that oEmbed content is served in an iframe on a different domain,
83 // and complain if it isn't.
84 $domain = \Drupal::config('media.settings')->get('iframe_domain');
86 if (!\Drupal::service('media.oembed.iframe_url_helper')->isSecure($domain)) {
87 // Find all media types which use a source plugin that implements
89 $media_types = \Drupal::entityTypeManager()
90 ->getStorage('media_type')
93 $oembed_types = array_filter($media_types, function (MediaTypeInterface $media_type) {
94 return $media_type->getSource() instanceof OEmbedInterface;
98 // @todo Potentially allow site administrators to suppress this warning
99 // permanently. See https://www.drupal.org/project/drupal/issues/2962753
100 // for more information.
101 $requirements['media_insecure_iframe'] = [
102 'title' => t('Media'),
103 '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>.', [
104 ':url' => Url::fromRoute('media.settings')->setAbsolute()->toString(),
106 'severity' => REQUIREMENT_WARNING,
112 return $requirements;
116 * Introduce per-bundle permissions.
118 function media_update_8500() {
119 $media_types = \Drupal::entityQuery('media_type')->execute();
121 /** @var \Drupal\user\RoleInterface $role */
122 foreach (Role::loadMultiple() as $role) {
123 if ($role->hasPermission('update media')) {
124 foreach ($media_types as $media_type) {
125 $role->grantPermission("edit own $media_type media");
129 if ($role->hasPermission('update any media')) {
130 foreach ($media_types as $media_type) {
131 $role->grantPermission("edit any $media_type media");
135 if ($role->hasPermission('delete media')) {
136 foreach ($media_types as $media_type) {
137 $role->grantPermission("delete own $media_type media");
141 if ($role->hasPermission('delete any media')) {
142 foreach ($media_types as $media_type) {
143 $role->grantPermission("delete any $media_type media");
147 if ($role->hasPermission('create media')) {
148 foreach ($media_types as $media_type) {
149 $role->grantPermission("create $media_type media");
158 * Updates media.settings to support OEmbed.
160 function media_update_8600() {
161 \Drupal::configFactory()->getEditable('media.settings')
162 ->set('iframe_domain', '')
163 ->set('oembed_providers_url', 'https://oembed.com/providers.json')