media.install 4.5 KB

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