Skip to content
Snippets Groups Projects
Commit 06a5bfbe authored by Oden's avatar Oden
Browse files

Issue #3185321: Only set a default name for the media item if mapped in the media source

parent 5e0d1efa
No related branches found
No related tags found
1 merge request!7993Issue #3185321: Only set a default name for the media item if mapped in the media source
...@@ -531,11 +531,19 @@ protected function processInputValues(array $source_field_values, array $form, F ...@@ -531,11 +531,19 @@ protected function processInputValues(array $source_field_values, array $form, F
* An unsaved media entity. * An unsaved media entity.
*/ */
protected function createMediaFromValue(MediaTypeInterface $media_type, EntityStorageInterface $media_storage, $source_field_name, $source_field_value) { protected function createMediaFromValue(MediaTypeInterface $media_type, EntityStorageInterface $media_storage, $source_field_name, $source_field_value) {
/** @var \Drupal\media\MediaInterface $media */
$media = $media_storage->create([ $media = $media_storage->create([
'bundle' => $media_type->id(), 'bundle' => $media_type->id(),
$source_field_name => $source_field_value, $source_field_name => $source_field_value,
]); ]);
$media->setName($media->getName());
// Set a default name for the media item if mapped in the media source.
$media_source = $media_type->getSource();
$name_attribute = array_search('name', $media_type->getFieldMap(), TRUE);
if ($name_attribute !== FALSE) {
$media->setName($media_source->getMetadata($media, $name_attribute));
}
return $media; return $media;
} }
......
...@@ -13,4 +13,5 @@ source_configuration: ...@@ -13,4 +13,5 @@ source_configuration:
providers: providers:
- YouTube - YouTube
- Vimeo - Vimeo
field_map: { } field_map:
title: name
...@@ -9,4 +9,5 @@ queue_thumbnail_downloads: false ...@@ -9,4 +9,5 @@ queue_thumbnail_downloads: false
new_revision: false new_revision: false
source_configuration: source_configuration:
source_field: field_media_test_image source_field: field_media_test_image
field_map: { } field_map:
name: name
...@@ -9,4 +9,5 @@ queue_thumbnail_downloads: false ...@@ -9,4 +9,5 @@ queue_thumbnail_downloads: false
new_revision: false new_revision: false
source_configuration: source_configuration:
source_field: field_media_test_image source_field: field_media_test_image
field_map: { } field_map:
name: name
...@@ -64,6 +64,7 @@ protected function createMediaItems(array $media_items) { ...@@ -64,6 +64,7 @@ protected function createMediaItems(array $media_items) {
*/ */
protected function waitForText($text, $timeout = 10000) { protected function waitForText($text, $timeout = 10000) {
$result = $this->assertSession()->waitForText($text, $timeout); $result = $this->assertSession()->waitForText($text, $timeout);
print_r($result);
$this->assertNotEmpty($result, "\"$text\" not found"); $this->assertNotEmpty($result, "\"$text\" not found");
} }
......
...@@ -760,4 +760,61 @@ public function testWidgetUploadAdvancedUi() { ...@@ -760,4 +760,61 @@ public function testWidgetUploadAdvancedUi() {
$assert_session->fieldValueEquals('Alternative text', $filenames[0], $media_item_one); $assert_session->fieldValueEquals('Alternative text', $filenames[0], $media_item_one);
} }
/**
* Tests that the name field is automatically mapped as expected.
*/
public function testWidgetUploadSetName(): void {
$assert_session = $this->assertSession();
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = $this->container->get('file_system');
$image = $this->getTestFiles('image')[0];
if (!isset($image)) {
$this->fail('Expected test files not present.');
}
// Create a user that can only add media of type four.
$user = $this->drupalCreateUser([
'access administration pages',
'access content',
'create basic_page content',
'create type_four media',
'view media',
]);
$admin = $this->drupalCreateUser([
'access administration pages',
'administer media types',
]);
// Visit a node create page and open the media library.
$this->drupalLogin($user);
$this->drupalGet('node/add/basic_page');
$this->openMediaLibraryForField('field_twin_media');
// Upload the test image file.
$this->switchToMediaType('Four');
$image_uri = $file_system->copy($image->uri, 'public://');
$this->addMediaFileToField('Add file', $file_system->realpath($image_uri));
$this->waitForFieldExists('Name');
// Test that the name field has been pre-filled with the filename.
$assert_session->fieldValueEquals('Name', $image->name);
// Remove the field map for name.
$this->drupalLogin($admin);
$this->drupalGet('admin/structure/media/manage/type_four');
$edit = [
'field_map[name]' => '_none',
];
$this->submitForm($edit, 'Save');
$this->drupalLogin($user);
$this->drupalGet('node/add/basic_page');
$this->openMediaLibraryForField('field_twin_media');
$this->switchToMediaType('Four');
$this->addMediaFileToField('Add file', $file_system->realpath($image_uri));
$this->waitForFieldExists('Name');
// Test that the name field has not been pre-filled.
$assert_session->fieldValueEquals('Name', '');
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment