diff --git a/core/modules/media/config/schema/media.schema.yml b/core/modules/media/config/schema/media.schema.yml index b0044089cf4640e89d90ea8fc5104d009d5adce7..6cde29c621dee53c602ff13a7b4f0c7be3509d59 100644 --- a/core/modules/media/config/schema/media.schema.yml +++ b/core/modules/media/config/schema/media.schema.yml @@ -36,6 +36,10 @@ media.type.*: sequence: type: string +media.handler.file: + type: media.handler.field_aware + label: 'File handler configuration' + action.configuration.media_delete_action: type: action_configuration_default label: 'Delete media configuration' diff --git a/core/modules/media/src/Plugin/media/Handler/File.php b/core/modules/media/src/Plugin/media/Handler/File.php new file mode 100644 index 0000000000000000000000000000000000000000..d0b209401ea64222bf26412c8c8e376a83633ed2 --- /dev/null +++ b/core/modules/media/src/Plugin/media/Handler/File.php @@ -0,0 +1,125 @@ + $this->t('MIME type'), + 'size' => $this->t('Size'), + ]; + } + + /** + * {@inheritdoc} + */ + public function getField(MediaInterface $media, $name) { + $source_field = $this->configuration['source_field']; + + // Get the file. + /** @var \Drupal\file\FileInterface $file */ + $file = $media->{$source_field}->entity; + + // Return the field. + switch ($name) { + case 'mime': + return $file->getMimeType() ?: FALSE; + + case 'size': + $size = $file->getSize(); + return is_numeric($size) ? $size : FALSE; + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function thumbnail(MediaInterface $media) { + $source_field = $this->configuration['source_field']; + /** @var \Drupal\file\FileInterface $file */ + $file = $media->{$source_field}->entity; + $icon_base = $this->configFactory->get('media.settings')->get('icon_base'); + $thumbnail = FALSE; + + // We try to magically use the most specific icon present in the $icon_base + // directory, based on the mime information. For instance, if an icon file + // named "pdf.png" is present, it will be used if the file matches this + // mime type. + if ($file) { + $mimetype = $file->getMimeType(); + $mimetype = explode('/', $mimetype); + $thumbnail = $icon_base . "/{$mimetype[0]}--{$mimetype[1]}.png"; + + if (!is_file($thumbnail)) { + $thumbnail = $icon_base . "/{$mimetype[1]}.png"; + } + } + + if (!is_file($thumbnail)) { + $thumbnail = $icon_base . '/generic.png'; + } + + return $thumbnail; + } + + /** + * {@inheritdoc} + */ + public function getDefaultName(MediaInterface $media) { + // The default name will be the filename of the source_field, if present. + $source_field = $this->configuration['source_field']; + + /** @var \Drupal\file\FileInterface $file */ + if (!empty($source_field) && ($file = $media->{$source_field}->entity)) { + return $file->getFilename(); + } + + return parent::getDefaultName($media); + } + + /** + * {@inheritdoc} + */ + protected function createSourceFieldStorage() { + return $this->entityTypeManager + ->getStorage('field_storage_config') + ->create([ + 'entity_type' => 'media', + 'field_name' => $this->getSourceFieldName(), + 'type' => 'file', + ]); + } + + /** + * {@inheritdoc} + */ + protected function createSourceField(MediaTypeInterface $type) { + return $this->entityTypeManager + ->getStorage('field_config') + ->create([ + 'field_storage' => $this->getSourceFieldStorage(), + 'bundle' => $type->id(), + ]); + } + +} diff --git a/core/modules/media/tests/src/FunctionalJavascript/FileTest.php b/core/modules/media/tests/src/FunctionalJavascript/FileTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0e4e08c35f0e30c8d3ccefe1e8fd016197aaa3b8 --- /dev/null +++ b/core/modules/media/tests/src/FunctionalJavascript/FileTest.php @@ -0,0 +1,50 @@ +getSession(); + $page = $session->getPage(); + $assert_session = $this->assertSession(); + + // We rely on an automatic source field being created at this point. + // @see MediaTypeBase::getSourceFieldName(). + $source_field_name = 'field_media_file'; + $bundle_name = strtolower($this->randomMachineName(12)); + $provided_fields = ['mime', 'size']; + $type = $this->createMediaTypeTest($bundle_name, 'file', $provided_fields); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); + // Adjust the allowed extensions on the source field. + $this->drupalGet("admin/structure/media/manage/$bundle_name/fields/media.$bundle_name.$source_field_name"); + $page->fillField('settings[file_extensions]', 'txt'); + $page->pressButton('Save settings'); + // Hide the media name to test default name generation. + $this->hideMediaField('name', $bundle_name); + // Create a media item. + $this->drupalGet("media/add/$bundle_name"); + $page->attachFileToField('files[' . $source_field_name . '_0]', \Drupal::root() . '/sites/README.txt'); + $assert_session->assertWaitOnAjaxRequest(); + $page->pressButton('Save and publish'); + + $assert_session->addressEquals('media/1'); + // Make sure the thumbnail shows up. + $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'generic.png'); + // Load the media and check its default name. + $media = Media::load(1); + $this->assertEquals('README.txt', $media->label()); + + } + +} diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaHandlerTestBase.php b/core/modules/media/tests/src/FunctionalJavascript/MediaHandlerTestBase.php new file mode 100644 index 0000000000000000000000000000000000000000..b176020b8ec1d7e145c24b9db0c8fa3540009d69 --- /dev/null +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaHandlerTestBase.php @@ -0,0 +1,97 @@ +removeComponent($field_name)->save(); + } + + /** + * Helper to test a generic type (bundle) creation. + * + * @param string $type_name + * The type machine name. + * @param string $type_id + * The bundle type ID. + * @param array $provided_fields + * (optional) An array of field machine names this type provides. + * + * @return \Drupal\media\MediaTypeInterface + * The type created. + */ + public function createMediaTypeTest($type_name, $type_id, array $provided_fields = []) { + $session = $this->getSession(); + $page = $session->getPage(); + $assert_session = $this->assertSession(); + + $this->drupalGet('admin/structure/media/add'); + $page->fillField('label', $type_name); + // assertWaitOnAjaxRequest() doesn't work on the machine name element. + $session->wait(5000, "jQuery('.machine-name-value').text() === '$type_name'"); + + // Make sure the type is available. + $assert_session->optionExists('handler', $type_id); + $page->selectFieldOption('handler', $type_id); + $assert_session->assertWaitOnAjaxRequest(); + + // Make sure the provided fields are visible on the form. + if (!empty($provided_fields)) { + foreach ($provided_fields as $provided_field) { + $assert_session->selectExists("field_mapping[$provided_field]"); + } + } + + // Save the page to create the type. + $page->pressButton('Save'); + $assert_session->pageTextContains('The media type ' . $type_name . ' has been added.'); + $this->drupalGet('admin/structure/media'); + $assert_session->pageTextContains($type_name); + + // Bundle definitions are statically cached in the context of the test, we + // need to make sure we have updated information before proceeding with the + // actions on the UI. + \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); + + return MediaType::load($type_name); + } + + /** + * Helper to assert presence/absence of select options. + * + * @param string $select + * One of id|name|label|value for the select field. + * @param array $expected_options + * An indexed array of expected option names. + * @param array $non_expected_options + * An indexed array of non-expected option names. + * + * @see \Drupal\Tests\WebAssert::optionExists() + * @see \Drupal\Tests\WebAssert::optionNotExists() + */ + protected function assertSelectOptions($select, $expected_options = [], $non_expected_options = []) { + $assert_session = $this->assertSession(); + foreach ($expected_options as $expected_option) { + $assert_session->optionExists($select, $expected_option); + } + foreach ($non_expected_options as $non_expected_option) { + $assert_session->optionNotExists($select, $non_expected_option); + } + } + +} diff --git a/core/profiles/standard/config/optional/field.field.media.file.field_media_file.yml b/core/profiles/standard/config/optional/field.field.media.file.field_media_file.yml new file mode 100644 index 0000000000000000000000000000000000000000..bb3ec25ab71799a6b8a31646f5acc3846ae5fafa --- /dev/null +++ b/core/profiles/standard/config/optional/field.field.media.file.field_media_file.yml @@ -0,0 +1,29 @@ +langcode: en +status: true +dependencies: + config: + - field.storage.media.field_media_file + - media_entity.bundle.file + module: + - file + enforced: + module: + - media_entity +id: media.file.field_media_file +field_name: field_media_file +entity_type: media +bundle: file +label: File +description: '' +required: true +translatable: true +default_value: { } +default_value_callback: '' +settings: + file_directory: '[date:custom:Y]-[date:custom:m]' + file_extensions: 'txt pdf' + max_filesize: '' + handler: 'default:file' + handler_settings: { } + description_field: false +field_type: file diff --git a/core/profiles/standard/config/optional/field.storage.media.field_media_file.yml b/core/profiles/standard/config/optional/field.storage.media.field_media_file.yml new file mode 100644 index 0000000000000000000000000000000000000000..1bab41feb481b53f72329ef6e96b8e39dfcdcb8c --- /dev/null +++ b/core/profiles/standard/config/optional/field.storage.media.field_media_file.yml @@ -0,0 +1,25 @@ +langcode: en +status: true +dependencies: + module: + - file + - media_entity + enforced: + module: + - media_entity +id: media.field_media_file +field_name: field_media_file +entity_type: media +type: file +settings: + uri_scheme: public + target_type: file + display_field: false + display_default: false +module: file +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/core/profiles/standard/config/optional/media.bundle.file.yml b/core/profiles/standard/config/optional/media.bundle.file.yml new file mode 100644 index 0000000000000000000000000000000000000000..4aa3524c167c6b5c80a5e955be873919ec0c687a --- /dev/null +++ b/core/profiles/standard/config/optional/media.bundle.file.yml @@ -0,0 +1,14 @@ +langcode: en +status: true +dependencies: + module: + - media +id: file +label: File +description: 'Use the "File" media type for uploading local files.' +type: file +queue_thumbnail_downloads: false +new_revision: false +type_configuration: + source_field: field_media_file +field_map: { }