Skip to content
Snippets Groups Projects
Commit 6ba332f6 authored by Sean Blommaert's avatar Sean Blommaert
Browse files

Issue #3450772: Media items created via the media library widget should have...

Issue #3450772: Media items created via the media library widget should have the same langcode as their parent entity
parent 8778ac18
No related branches found
No related tags found
No related merge requests found
......@@ -499,8 +499,9 @@ protected function processInputValues(array $source_field_values, array $form, F
$media_type = $this->getMediaType($form_state);
$media_storage = $this->entityTypeManager->getStorage('media');
$source_field_name = $this->getSourceFieldName($media_type);
$media = array_map(function ($source_field_value) use ($media_type, $media_storage, $source_field_name) {
return $this->createMediaFromValue($media_type, $media_storage, $source_field_name, $source_field_value);
$langcode = $this->getMediaLibraryState($form_state)->getOpenerParameters()['langcode'] ?? NULL;
$media = array_map(function ($source_field_value) use ($media_type, $media_storage, $source_field_name, $langcode) {
return $this->createMediaFromValue($media_type, $media_storage, $source_field_name, $source_field_value, $langcode);
}, $source_field_values);
// Re-key the media items before setting them in the form state.
$form_state->set('media', array_values($media));
......@@ -526,16 +527,21 @@ protected function processInputValues(array $source_field_values, array $form, F
* The name of the media type's source field.
* @param mixed $source_field_value
* The value for the source field of the media item.
* @param mixed $langcode
* The langcode of the media item.
*
* @return \Drupal\media\MediaInterface
* 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, $langcode = NULL) {
$media = $media_storage->create([
'bundle' => $media_type->id(),
$source_field_name => $source_field_value,
]);
$media->setName($media->getName());
if ($langcode) {
$media->set('langcode', $langcode);
}
return $media;
}
......
......@@ -321,7 +321,7 @@ public function uploadButtonSubmit(array $form, FormStateInterface $form_state)
/**
* {@inheritdoc}
*/
protected function createMediaFromValue(MediaTypeInterface $media_type, EntityStorageInterface $media_storage, $source_field_name, $file) {
protected function createMediaFromValue(MediaTypeInterface $media_type, EntityStorageInterface $media_storage, $source_field_name, $file, $langcode = NULL) {
if (!($file instanceof FileInterface)) {
throw new \InvalidArgumentException('Cannot create a media item without a file entity.');
}
......@@ -337,7 +337,7 @@ protected function createMediaFromValue(MediaTypeInterface $media_type, EntitySt
throw new \RuntimeException("Unable to move file to '$upload_location'");
}
return parent::createMediaFromValue($media_type, $media_storage, $source_field_name, $file);
return parent::createMediaFromValue($media_type, $media_storage, $source_field_name, $file, $langcode);
}
/**
......
......@@ -495,6 +495,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'entity_type_id' => $entity->getEntityTypeId(),
'bundle' => $entity->bundle(),
'field_name' => $field_name,
'langcode' => $entity->language()->getId(),
];
// Only add the entity ID when we actually have one. The entity ID needs to
// be a string to ensure that the media library state generates its
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment