diff --git a/core/modules/media/media.install b/core/modules/media/media.install index 1fb0127..4d66989 100644 --- a/core/modules/media/media.install +++ b/core/modules/media/media.install @@ -11,5 +11,15 @@ function media_install() { $source = drupal_get_path('module', 'media') . '/images/icons'; $destination = \Drupal::config('media.settings')->get('icon_base'); - media_copy_icons($source, $destination); + if (!file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { + throw new \RuntimeException("Unable to create directory $destination."); + } + + $files = file_scan_directory($source, '/.*\.(png|jpg)$/'); + foreach ($files as $file) { + $result = file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_REPLACE); + if (!$result) { + throw new \RuntimeException("Unable to copy {$file->uri} to $destination."); + } + } } diff --git a/core/modules/media/media.module b/core/modules/media/media.module index 962090e..f5eb091 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -5,6 +5,10 @@ * Provides media entities. */ +use Drupal\Core\Access\AccessResult; +use Drupal\Core\Session\AccountInterface; +use Drupal\field\Entity\FieldConfig; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; @@ -49,11 +53,11 @@ function media_theme() { * * @todo: This can be removed when https://www.drupal.org/node/2836384 is fixed. */ -function media_entity_operation_alter(array &$operations, \Drupal\Core\Entity\EntityInterface $entity) { - if ($entity instanceof Drupal\field\Entity\FieldConfig && $entity->getTargetEntityTypeId() == 'media') { +function media_entity_operation_alter(array &$operations, EntityInterface $entity) { + if ($entity instanceof FieldConfig && $entity->getTargetEntityTypeId() == 'media') { /** @var \Drupal\media\MediaTypeInterface $media_type */ $media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($entity->getTargetBundle()); - if ($entity->id() == 'media.' . $media_type->id() . '.' . $media_type->getHandler()->getConfiguration()['source_field'] ) { + if ($entity->id() == 'media.' . $media_type->id() . '.' . $media_type->getHandler()->getConfiguration()['source_field']) { unset($operations['delete']); } } @@ -62,13 +66,13 @@ function media_entity_operation_alter(array &$operations, \Drupal\Core\Entity\En /** * Implements hook_entity_access(). */ -function media_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) { - if ($operation == 'delete' && $entity instanceof Drupal\field\Entity\FieldConfig && $entity->getTargetEntityTypeId() == 'media') { +function media_entity_access(EntityInterface $entity, $operation, AccountInterface $account) { + if ($operation == 'delete' && $entity instanceof FieldConfig && $entity->getTargetEntityTypeId() == 'media') { /** @var \Drupal\media\MediaTypeInterface $media_type */ $media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($entity->getTargetBundle()); - return \Drupal\Core\Access\AccessResult::forbiddenIf($entity->id() == 'media.' . $media_type->id() . '.' . $media_type->getHandler()->getConfiguration()['source_field']); + return AccessResult::forbiddenIf($entity->id() == 'media.' . $media_type->id() . '.' . $media_type->getHandler()->getConfiguration()['source_field']); } - return \Drupal\Core\Access\AccessResult::neutral(); + return AccessResult::neutral(); } /** @@ -85,28 +89,3 @@ function media_theme_suggestions_media(array $variables) { return $suggestions; } - -/** - * Copies the media file icons to files directory for use with image styles. - * - * @param string $source - * Source folder. - * @param string $destination - * Destination folder. - * - * @throws \RuntimeException - * Thrown when media icons can't be copied to their destination. - */ -function media_copy_icons($source, $destination) { - if (!file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { - throw new \RuntimeException("Unable to create directory $destination."); - } - - $files = file_scan_directory($source, '/.*\.(png|jpg)$/'); - foreach ($files as $file) { - $result = file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_REPLACE); - if (!$result) { - throw new \RuntimeException("Unable to copy {$file->uri} to $destination."); - } - } -} diff --git a/core/modules/media/tests/src/Kernel/BasicCreationTest.php b/core/modules/media/tests/src/Kernel/BasicCreationTest.php index 8b2eb9b..231bc64 100644 --- a/core/modules/media/tests/src/Kernel/BasicCreationTest.php +++ b/core/modules/media/tests/src/Kernel/BasicCreationTest.php @@ -2,8 +2,6 @@ namespace Drupal\Tests\media\Kernel; -use Drupal\Core\TypedData\PrimitiveInterface; -use Drupal\Core\TypedData\TypedDataInterface; use Drupal\field\Entity\FieldConfig; use Drupal\KernelTests\KernelTestBase; use Drupal\media\Entity\Media; @@ -101,7 +99,7 @@ public function testMediaEntityCreation() { $this->assertInstanceOf(MediaInterface::class, Media::load($media->id()), 'The new media entity has not been created in the database.'); $this->assertEquals($this->testBundle->id(), $media->bundle(), 'The media was not created with the correct type.'); $this->assertEquals('Unnamed', $media->label(), 'The media was not created with the correct name.'); - $this->assertEquals('Nation of sheep, ruled by wolves, owned by pigs.', $media->bundle->entity->getHandler()->getSourceValue($media)->getValue() , 'Handler returns correct source value.'); + $this->assertEquals('Nation of sheep, ruled by wolves, owned by pigs.', $media->bundle->entity->getHandler()->getSourceValue($media)->getValue(), 'Handler returns correct source value.'); // Test the creation of a media without user-defined label and check if a // default name is provided.