annotate core/modules/media/media.install @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
rev   line source
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@17 8 use Drupal\Core\Url;
Chris@17 9 use Drupal\media\MediaTypeInterface;
Chris@17 10 use Drupal\media\Plugin\media\Source\OEmbedInterface;
Chris@0 11 use Drupal\user\RoleInterface;
Chris@14 12 use Drupal\user\Entity\Role;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Implements hook_install().
Chris@0 16 */
Chris@0 17 function media_install() {
Chris@0 18 $source = drupal_get_path('module', 'media') . '/images/icons';
Chris@0 19 $destination = \Drupal::config('media.settings')->get('icon_base_uri');
Chris@0 20 file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
Chris@0 21
Chris@0 22 $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/');
Chris@0 23 foreach ($files as $file) {
Chris@0 24 // When reinstalling the media module we don't want to copy the icons when
Chris@0 25 // they already exist. The icons could be replaced (by a contrib module or
Chris@0 26 // manually), so we don't want to replace the existing files. Removing the
Chris@0 27 // files when we uninstall could also be a problem if the files are
Chris@0 28 // referenced somewhere else. Since showing an error that it was not
Chris@0 29 // possible to copy the files is also confusing, we silently do nothing.
Chris@0 30 if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) {
Chris@0 31 file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_ERROR);
Chris@0 32 }
Chris@0 33 }
Chris@0 34
Chris@0 35 // Grant the "view media" permission to all users by default.
Chris@0 36 if (\Drupal::moduleHandler()->moduleExists('user')) {
Chris@0 37 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['view media']);
Chris@0 38 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['view media']);
Chris@0 39 }
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Implements hook_requirements().
Chris@0 44 */
Chris@0 45 function media_requirements($phase) {
Chris@0 46 $requirements = [];
Chris@0 47 if ($phase == 'install') {
Chris@0 48 $destination = 'public://media-icons/generic';
Chris@0 49 file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
Chris@0 50 $is_writable = is_writable($destination);
Chris@0 51 $is_directory = is_dir($destination);
Chris@0 52 if (!$is_writable || !$is_directory) {
Chris@0 53 if (!$is_directory) {
Chris@0 54 $error = t('The directory %directory does not exist.', ['%directory' => $destination]);
Chris@0 55 }
Chris@0 56 else {
Chris@0 57 $error = t('The directory %directory is not writable.', ['%directory' => $destination]);
Chris@0 58 }
Chris@0 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']);
Chris@0 60 if (!empty($error)) {
Chris@0 61 $description = $error . ' ' . $description;
Chris@0 62 $requirements['media']['description'] = $description;
Chris@0 63 $requirements['media']['severity'] = REQUIREMENT_ERROR;
Chris@0 64 }
Chris@0 65 }
Chris@0 66
Chris@0 67 // Prevent installation if the 1.x branch of the contrib module is enabled.
Chris@0 68 if (\Drupal::moduleHandler()->moduleExists('media_entity')) {
Chris@0 69 $info = system_get_info('module', 'media_entity');
Chris@0 70 if (version_compare($info['version'], '8.x-2') < 0) {
Chris@0 71 $requirements['media_module_incompatibility'] = [
Chris@0 72 'title' => t('Media'),
Chris@0 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.', [
Chris@0 74 ':url' => 'https://drupal.org/project/media_entity',
Chris@0 75 ]),
Chris@0 76 'severity' => REQUIREMENT_ERROR,
Chris@0 77 ];
Chris@0 78 }
Chris@0 79 }
Chris@0 80 }
Chris@17 81 elseif ($phase === 'runtime') {
Chris@17 82 // Check that oEmbed content is served in an iframe on a different domain,
Chris@17 83 // and complain if it isn't.
Chris@17 84 $domain = \Drupal::config('media.settings')->get('iframe_domain');
Chris@17 85
Chris@17 86 if (!\Drupal::service('media.oembed.iframe_url_helper')->isSecure($domain)) {
Chris@17 87 // Find all media types which use a source plugin that implements
Chris@17 88 // OEmbedInterface.
Chris@17 89 $media_types = \Drupal::entityTypeManager()
Chris@17 90 ->getStorage('media_type')
Chris@17 91 ->loadMultiple();
Chris@17 92
Chris@17 93 $oembed_types = array_filter($media_types, function (MediaTypeInterface $media_type) {
Chris@17 94 return $media_type->getSource() instanceof OEmbedInterface;
Chris@17 95 });
Chris@17 96
Chris@17 97 if ($oembed_types) {
Chris@17 98 // @todo Potentially allow site administrators to suppress this warning
Chris@17 99 // permanently. See https://www.drupal.org/project/drupal/issues/2962753
Chris@17 100 // for more information.
Chris@17 101 $requirements['media_insecure_iframe'] = [
Chris@17 102 'title' => t('Media'),
Chris@17 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>.', [
Chris@17 104 ':url' => Url::fromRoute('media.settings')->setAbsolute()->toString(),
Chris@17 105 ]),
Chris@17 106 'severity' => REQUIREMENT_WARNING,
Chris@17 107 ];
Chris@17 108 }
Chris@17 109 }
Chris@17 110 }
Chris@0 111
Chris@0 112 return $requirements;
Chris@0 113 }
Chris@14 114
Chris@14 115 /**
Chris@14 116 * Introduce per-bundle permissions.
Chris@14 117 */
Chris@14 118 function media_update_8500() {
Chris@14 119 $media_types = \Drupal::entityQuery('media_type')->execute();
Chris@14 120
Chris@14 121 /** @var \Drupal\user\RoleInterface $role */
Chris@14 122 foreach (Role::loadMultiple() as $role) {
Chris@14 123 if ($role->hasPermission('update media')) {
Chris@14 124 foreach ($media_types as $media_type) {
Chris@14 125 $role->grantPermission("edit own $media_type media");
Chris@14 126 }
Chris@14 127 }
Chris@14 128
Chris@14 129 if ($role->hasPermission('update any media')) {
Chris@14 130 foreach ($media_types as $media_type) {
Chris@14 131 $role->grantPermission("edit any $media_type media");
Chris@14 132 }
Chris@14 133 }
Chris@14 134
Chris@14 135 if ($role->hasPermission('delete media')) {
Chris@14 136 foreach ($media_types as $media_type) {
Chris@14 137 $role->grantPermission("delete own $media_type media");
Chris@14 138 }
Chris@14 139 }
Chris@14 140
Chris@14 141 if ($role->hasPermission('delete any media')) {
Chris@14 142 foreach ($media_types as $media_type) {
Chris@14 143 $role->grantPermission("delete any $media_type media");
Chris@14 144 }
Chris@14 145 }
Chris@14 146
Chris@14 147 if ($role->hasPermission('create media')) {
Chris@14 148 foreach ($media_types as $media_type) {
Chris@14 149 $role->grantPermission("create $media_type media");
Chris@14 150 }
Chris@14 151 }
Chris@14 152
Chris@14 153 $role->save();
Chris@14 154 }
Chris@14 155 }
Chris@17 156
Chris@17 157 /**
Chris@17 158 * Updates media.settings to support OEmbed.
Chris@17 159 */
Chris@17 160 function media_update_8600() {
Chris@17 161 \Drupal::configFactory()->getEditable('media.settings')
Chris@17 162 ->set('iframe_domain', '')
Chris@17 163 ->set('oembed_providers_url', 'https://oembed.com/providers.json')
Chris@17 164 ->save(TRUE);
Chris@17 165 }