Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I show the content translate programmatically like this:
$service_entities = entity_load_multiple_by_properties('node', array('type' => 'service'));
$services = [];
foreach ($service_entities as $entity) {
$entity = \Drupal::entityManager()->getTranslationFromContext($entity);
$services[] = [
'title' => $entity->title->value,
// more field...
];
}
When I'm logged all translations work well, but when I logout just show me the translations in the default language.
Comment | File | Size | Author |
---|---|---|---|
#3 | Selection_007.png | 18.24 KB | rpayanm |
#3 | Selection_006.png | 17.07 KB | rpayanm |
#3 | Selection_005.png | 16.53 KB | rpayanm |
Comments
Comment #1
larowlanComment #2
Berdir- Do you have language negotation configured correctly? If you display the output of \Drupal::languageManager()->getCurrentLanguage()->getId(), is that the language that you expect?
- Is there some page or render cache involved?
Changing to a support request until we know there is a big. I have no idea how that would be possible.
Comment #3
rpayanmYes it's the language I expect.
In my code I do:
$stuff['langcode'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
and on my template.html.twig:
{{ stuff.langcode }}
For spanish, print me:
es
, for english:en
, so it's right, no? My primary language is spanish and when I am an anonymous user only show me the text on spanish, no matter the selected language, and it have a translation for english.No, actually I run
drush cache-rebuild
anyway, and still the problem come out.I test my site with this:
This:
And this:
Still the same result.
Comment #4
rpayanmSomething curious it's that the main menu is translated, and for both admin and anonymous user, this translates!
Comment #5
larowlanDidn't plach open an issue about entity adapter issues with translations, could be related?
Comment #6
rpayanm@larowlan no, could you explain me better to do?
Comment #7
larowlanI was referring to https://www.drupal.org/node/2414721
Comment #8
BerdirWhat are you doing with $services then exactly? What happens if you just do var_dump($entity->title->value)?
Comment #9
rpayanmWith $services I passed to twig template to print this result:
var_dump($entity->title->value) :
Logged:
es:
string 'Desarrollo Web' (length=14)
en:
string 'Web development' (length=15)
Anonymous:
es:
string 'Desarrollo Web' (length=14)
en:
string 'Desarrollo Web' (length=14)
Comment #10
BerdirAh, now I remember. Try giving the anonymous user the translate any content permission.
Not as a real fix, just to verify that it is the problem I think it is.
On mobile, so can't look up the exact source but it is the problem I was telling @plach earlier, some language alter hook in content translation. And the entity adapter issue is not related.
Sorry for not remembering this earlier, I actually had this myself as well in a test.
Definitely a bug and atleast major.
Comment #11
rpayanmUmm I check "Translate Service content" for anonymous and then work fine !!!
I agree with that.
Comment #12
BerdirOk, if you want to try and fix this before @plach finds time, have a look at content_translation_language_fallback_candidates_entity_view_alter(), something with the is published detection might not be correct.
That said, did you make sure that you translations really are published?
Comment #13
plachIf translations are published this is probably a straight duplicate of #2414721: EntityAdapter should be instantiated per language. I found it while manually testing a very similar use case, i.e. checking whether a view of translated nodes worked correctly for anonymous users.
Comment #14
plachComment #15
rpayanmMy translations are unpublished, then I published these and continue the situation.
Comment #16
plachUnpublished translations are not supposed to be viewed by users not having translation permissions, the fact that the behavior persists after publishing them makes me think this is a duplicate of #2414721: EntityAdapter should be instantiated per language. Let's wait for that one to be fixed and check whether that fixes this one too.
Comment #17
plachComment #18
BerdirAh, if they are unpublished then that is the expected behavior, yes :)
I had this in simplenews, in a test where I'm sending translated newsletters. That required the translate any entity workaround a few weeks ago, and I was trying to test the patch in #2414721: EntityAdapter should be instantiated per language, but I was actually not able to reproduce the problem anymore, the test works without that permission on HEAD, see #2418403: Remove translate any entity permission from SimplenewsI18nTest.
Comment #19
plach@Berdir++
Thanks for being so careful :)
Comment #20
plach#2414721: EntityAdapter should be instantiated per language was committed. Can you still reproduce this?
Comment #21
rpayanmThe problem was that my translated content was unpublished, but already published working as expected.
Thank you so much @plach and @Berdir.
Comment #22
BerdirGreat, then marking as fixed and changing to a support request.