Steps to reproduce:

1. Create new entity browser for media entity bundles with images type provider
2. Add Entity reference field to the translatable node type
3. Select Entity browser widget for entity reference field and attach created entity browser
4. Create new content and upload image via Entity browser field, specify alt and title attributes for image
5. Add translation for created content
6. Try to overwrite entity browser's image attributes to new language (create translation for referenced media entity)
As result, it does not create new translation for referenced media entity

UPD:
Fast Fix PR:
https://github.com/drupal-media/entity_browser/pull/150

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexsergivan created an issue. See original summary.

Oleks Iv’s picture

Issue summary: View changes
Oleks Iv’s picture

Issue summary: View changes
slashrsm’s picture

Version: 8.x-1.0-alpha2 » 8.x-1.x-dev
Category: Bug report » Task
Issue tags: +Needs tests

Test would be nice.

slashrsm’s picture

Issue tags: +D8Media, +rc target
slashrsm’s picture

Issue tags: -rc target +RC blocker
Leksat’s picture

Status: Active » Needs review
FileSize
1.48 KB

A bit improved version of PR 150.

samuel.mortenson’s picture

I'll work on tests.

samuel.mortenson’s picture

I was looking through the code again and the patch seems very manual - I can't find examples of another Controller delivering an Entity Form and handling Translation this way. Has this issue been tested with any entity type other than Media Entities?

I ask this because forms like ContentEntityForm seem to do similar logic, which makes sense as the Entity Form in question here should be responsible for delivering the correct translation of an Entity - not Entity Browser. See ContentEntityForm::init() for what I'm referencing.

slashrsm’s picture

What about passing language via URL argument?

slashrsm’s picture

Status: Needs review » Needs work
blazey’s picture

FileSize
1.54 KB

Patch no longer applies. Attaching rerolled version.

blazey’s picture

Status: Needs work » Needs review
FileSize
1.61 KB

@samuel.mortenson Attaching a new, refactored version that takes your comments into account. Thanks to $form_state->set('langcode', $langcode); ContentEntityForm::init() takes over translation handling. It doesn't copy values from source entity when adding translation though, so the following part has been added to improve UX:

if (!$entity->hasTranslation($langcode)) {
  $entity = $entity->addTranslation($langcode, $entity->toArray());
}
badrange’s picture

Just tested adding this patch to my site under development and it did indeed fix the issue. Thanks.

samuel.mortenson’s picture

@blazey Thanks! This patch is starting to look better.

So ContentEntityForm::init() already adds a translation, but as you mentioned does not copy values from the existing Entity. Can you explain the why using our own $entity->addTranslation call is necessary here? What behavior is different with/without that call? I'm also wondering if there's an example of a controller doing this in core, since I would expect ContentEntityForm to handle all of this.

blazey’s picture

@samuel.mortenson Thanks for review!

Without the call to $entity->addTranslation media translation form is displayed blank. Initial field values are not copied from the translation source. IMHO it's very inconvenient to select the same file every time you add a translation. There are definitely cases when you have to do that anyway (like when it's a pdf in different language) but often you just want to translate alt and title of the image and it's just easier (and more intuitive) to have the values (including the original file) prefilled.

I have no idea why ContentEntityForm creates the translation but doesn't pass field values. It would be enough to change line 171 here http://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Core/Entity/Conte... from

(...) : $this->entity->addTranslation($langcode);

to

(...) : $this->entity->addTranslation($langcode, $this->entity->toArray());

Maybe it deserves a core issue? Anyway, for the time being it might be worth adding the translation ourselves as the UX gain is huge.

samuel.mortenson’s picture

@blazey You mention media translation specifically - as Entity Browser can be used with any Entity Type, would copying values to the new translation make sense for Nodes as well?

blazey’s picture

@samuel.mortenson To me it would make sense as I would like to know what I'm translating. The alternative is getting an empty form and having to look up the original entity in another browser tab.

Leksat’s picture

@samuel.mortenson,

would copying values to the new translation make sense for Nodes as well?

Yes it would. Currently ContentTranslationController does this for regular node forms here: https://github.com/drupal/drupal/blob/8.3.0-alpha1/core/modules/content_...

(Attached image is a screenshot of a customized "Media file selector" (media_generic) widget we used on one of our old D7 projects. Not the most intuitive UI :D)

Leksat’s picture

FileSize
8.41 KB

@slashrsm,

What about passing language via URL argument?

I agree that it would be nicer to use parent form language instead of the current content language.

So here is my attempt.
Additionally, I made that the edit form reflects the language select element value dynamically. If the language select element exists on the parent form.

samuel.mortenson’s picture

What benefit is there to passing the langcode with a query parameter, instead of trusting the core functionality of prefixing routes with the langcode (i.e. /fr/*)? I've never seen modules explicitly pass the langcode like this.

Leksat’s picture

Well, we can use system language prefixes/domains. But we have to be careful since language detection can be setup individually for different language type.
Also, using query parameter, it was easy to change langcode dynamically basing on the current language select value (in #20).

Lennard Westerveld’s picture

@Leksat,
Your patch wasn't working on drupal 8.3.x but i have made a fix see my patch.

Faibyshev’s picture

If add new translatable entity by entity browser, new entity will be add with default site language. I added fix for it.

Status: Needs review » Needs work

The last submitted patch, 24: entity_browser-problems-with-multilingual.patch, failed testing. View results

Lennard Westerveld’s picture

@Faibyshev: I think your way of getting the current language is not always the right?
Because of the types of language detections i think this is the right way: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Language%... and LanguageInterface::TYPE_CONTENT

Faibyshev’s picture

@Lennard Westerveld, Thanks for the advice, you are right. I have made changes. Thank you.

ziomizar’s picture

I've tested on drupal 8.3.x the patch at #23 and it works for me.

4. Create new content and upload image via Entity browser field, specify alt and title attributes for image
5. Add translation for created content
6. Try to overwrite entity browser's image attributes to new language (create translation for referenced media entity)
As result, it create new translation for referenced media entity.

Leksat’s picture

Status: Needs work » Needs review
FileSize
12.09 KB

Reroll of #27 on 8.x-1.x branch.

BTW, #27 still applies nicely to 8.x-1.4.

pavlosdan’s picture

Does this work with the View widget as well? or should I open a different issue? Currently on a translated node if you select a translated media entity, the original node changes to point to the selected translation as well. Expected behaviour was that the media on the original node would not change.

adr_p’s picture

I'm not sure if in fits the scope of this issue, but we needed something similar but from a media entity perspective: creating media entity translations when they're added to content in a different language (e.g. referencing "English" media entity in a "Spanish" content would create a Spanish translation of that media entity).

Please ignore the patch if it doesn't fit in here.

Status: Needs review » Needs work

The last submitted patch, 31: translate-on-select-2721665-31.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

adr_p’s picture

Here's the updated patch that covers LanguageInterface::LANGCODE_NOT_SPECIFIED.

samuel.mortenson’s picture

Status: Needs work » Needs review
Issue tags: +Nashville2018

Putting back into needs review for #29.

samuel.mortenson’s picture

Status: Needs review » Needs work
+++ b/js/entity_browser.common.js
@@ -143,4 +143,23 @@
+  // AJAX beforeSubmit callback.
+  var ajaxBeforeSubmitOriginal = Drupal.Ajax.prototype.beforeSubmit;
+  Drupal.Ajax.prototype.beforeSubmit = function(form_values, element, options) {
+    if (this.$form) {
+
+      // If there is a langcode select element on the form, use its value in the
+      // AJAX call.
+      var langcodeSelectElementName = $(this.element).data('entity-browser-langcode-select-element-name');
+      if (langcodeSelectElementName) {
+        var $langcode = this.$form.find('select[name="' + langcodeSelectElementName + '"]');
+        if ($langcode.length) {
+          options.url = options.url.replace(/([?&]langcode=)[^&]+/, '$1' + $langcode.val() + '$2');
+        }
+      }
+    }
+
+    ajaxBeforeSubmitOriginal.call(this, form_values, element, options);
+  };

Is there another (cleaner?) place we can do this? Overriding the prototype in this way seems like a workaround.

I also wonder if the logic this patch is adding makes sense for everyone. From reading it - if I am viewing a translation of a Node and edit one of the referenced entities using Entity Browser, I am automatically creating a translation of that entity. Will every user want this?

This patch also still needs tests, so setting back to needs work.

adinac’s picture

Reroll of #27 on 8.x-1.x branch.

badrange’s picture

Status: Needs work » Needs review

Putting back into needs review for #36

samuel.mortenson’s picture

Status: Needs review » Needs work

@badrange This issue still needs tests, and my feedback from #35 has not been addressed.

ayalon’s picture

We are using this patch on every multilingual site. Whithout it, the handling of translated entities is completely broken.

ziomizar’s picture

Priority: Normal » Major

This seems to be a major issue

ziomizar’s picture

Category: Task » Bug report

This lead also on lost user data in a multilanguage website, and should be marked as bug not task as it is now.

Imagine this scenario, multilanguage website in italian and english:
- user create the english version of a page "Type A" as alt in the image write "This is the title and alt of the image"
- user translate the "Type A" in italian and set as alt for the image "Questo è il testo per alt e titolo dell'immagine"

You lost the english version, as the italian overrided it.

s_leu’s picture

The patch in #36 solves the problem for me too, but it sure still needs tests.

dksdev01’s picture

Hi All,

Is there a patch for entity_browser 8.2.1 version? Patch #13 used to work with 2.0.0-alpha2.

thanks,

7thkey’s picture

This should be focus on 8.2.x, it is not really fixed!

stewest’s picture

I now get

 Updating drupal/entity_browser (2.0.0 => 2.2.0): Downloading (100%)
  - Applying patches for drupal/entity_browser

    patches/entity_browser/2721665-13-2.patch (In-place media translation (Entity Browser 2.O))

   Could not apply patch! Skipping. The error was: Cannot apply patch patches/entity_browser/2721665-13-2.patch

Does this mean this patch is in 2.2.0?

Spokje’s picture

@stewest

Does this mean this patch is in 2.2.0?

Sadly not, it means you got very lucky that such an old patch made for the 8.1 version survived for so long.

This really needs to get some TLC to get into the next release. I rerolled #36 against the latest 8.2.x-dev version to get this back on the radar.
Also upped the version from 8.x-1.x-dev to 8.x-2.x-dev.
Looking at #41 (and due to the lack of tests) I've left it at Needs work

Spokje’s picture

Spokje’s picture

D8.6 test failures are caused by https://www.drupal.org/project/entity_browser/issues/3054210, which makes running tests for this module on D8.6 fail as mentioned by oknate here: https://www.drupal.org/project/entity_browser/issues/3054210#comment-131...

Note that the tests fail because of using a function that exists only from D8.7 upwards, so it doesn't mean the test is failing, just that the test couldn't run. The mentioned function is only used in the tests, not in the actual module, so using this patch on D8.6 is still OK.

Ghost of Drupal Past’s picture

Note this is under testing at Smartsheet. We will report back.

Renrhaf’s picture

Updating patch to avoid a PHP warning :

Warning: Declaration of Drupal\entity_browser\Plugin\Field\FieldWidget\FileBrowserWidget::displayCurrentSelection($details_id, $field_parents, $entities, $langcode = NULL) should be compatible with Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::displayCurrentSelection($details_id, array $field_parents, array $entities, $langcode = NULL)...

Spokje’s picture

Reroll of patch in #50 against latest 8.2-2.x

Spokje’s picture

Issue tags: -Nashville2018

Tags cleanup.

I will be trying to get at least the scenario from #41 in as a start point for (more) testing during the upcoming weeks.

ayalon’s picture

We did a lot of testing and have this patch in place on a lot of production sites.
Test are not failing and not using this patch might lead to a loss af data.
We should go a step ahead.

ayalon’s picture

Status: Needs work » Reviewed & tested by the community
xavier.masson’s picture

Reroll the patch with the 8.x-2.x

pcambra’s picture

Confirming RTBC

Lukas von Blarer’s picture

Does anyone know of an issue regarding this for core's media_library?

Lukas von Blarer’s picture

Works perfectly! Thank you!

ozin’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
13.41 KB
1.71 KB

#55 Patch does not work with layout builder in Drupal 9.3.0

Using LanguageManager service makes it works with Layout Builder.

sokru’s picture

Status: Needs review » Needs work

+1 for adding this. Manually tested patches from #55 and #59, works nicely, but settings "Needs work" since maintainer has required tests for this issue.

Lukas von Blarer’s picture

Lukas von Blarer’s picture

ita08’s picture

Reroll of patch 2721665-55 for version 2.8.

bserem’s picture

Patch #63 has some coding standard issues (spaces). It also seems that it does not address layout builder like patch #59 does.

#59 is the preferred patch here.

dewalt’s picture

Status: Needs work » Needs review
FileSize
13.29 KB

Updated #56 patch to use with latest module version.

dewalt’s picture

Status: Needs review » Needs work
bserem’s picture

@dewalt I assume you meant patch 59 was the source, as there is no patch in 56, and 56 is just a typo mistake.