Skip to content
Snippets Groups Projects

Explicitly pass entity language or use it for translation in processors

4 unresolved threads

Explicitly pass entity language or use it for translation; abstract 'get entity language' into a helper method to abstract away the fallback when that's impossible. Remove buggy/unused $key arguments.

Closes #3482119

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
39 39 /** @var \Drupal\media\MediaInterface $media_entity */
40 40 $media_entity = $field_item->entity;
41 41 if ($this->entityIsAccessible($media_entity, $element)) {
42 $media_element = $this->getCustomElementGenerator()->generate($media_entity, $viewMode, $key);
42 $media_element = $this->getCustomElementGenerator()->generate(
43 $media_entity,
44 $viewMode,
45 $this->getEntityLangcodeFromFieldItem($field_item)
46 );
    • getEntityLangcodeFromFieldItem() now derives 'de' from the entity language
    • media reference field gets translated to 'de' specifically (instead of 'de' just being derived from current language in generate(, , [NULL]))

    -> No changed behavior.

  • Please register or sign in to reply
  • 39 39 /** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
    40 40 $paragraph = $field_item->entity;
    41 41 if ($this->entityIsAccessible($paragraph, $element)) {
    42 $paragraph_element = $this->getCustomElementGenerator()->generate($paragraph, $viewMode, $key);
    42 $paragraph_element = $this->getCustomElementGenerator()->generate(
    43 $paragraph,
    44 $viewMode,
    45 $this->getEntityLangcodeFromFieldItem($field_item)
    46 );
      • getEntityLangcodeFromFieldItem() now derives 'de' from the 'fallback logic', because the entity language is 'en' for this untranslatable field
      • media reference field gets translated to 'de' specifically (instead of 'de' just being derived from current language in generate(, , [NULL]))

      -> No changed behavior.

    • Please register or sign in to reply
  • 49 $nested_element->setTag('a');
    50 $generated_url = $url->toString(TRUE);
    51 $nested_element->addCacheableDependency($generated_url);
    52 $nested_element->setAttribute('href', $generated_url->getGeneratedUrl());
    53 $nested_element->setAttribute('type', $entity->getEntityTypeId());
    54 $nested_element->setSlot('default', $entity->label());
    55 $nested_element->addCacheableDependency($entity);
    56 $element->setSlotFromCustomElement($prefixed_key . $name, $nested_element);
    64 && $this->entityIsAccessible($entity, $element)) {
    65 if ($entity instanceof TranslatableInterface) {
    66 $desired_langcode = $this->getEntityLangcodeFromFieldItem($field_item);
    67 if ($entity->language()->getId() !== $desired_langcode
    68 && $entity->hasTranslation($desired_langcode)) {
    69 $entity = $entity->getTranslation($desired_langcode);
    70 }
    71 }
    • (Given 'en' is original node language and 'de' is translation)

      Note: $property->getTarget()->getValue() never gets get the translated entity, because that's just how getTarget() is implemented. (It's just an entity load, without checking language.)

      • term reference:
        • getEntityLangcodeFromFieldItem() returns 'de'
        • translation available -> we get the 'de' term. That's a behavior change / fix.
      • User reference:
        • getEntityLangcodeFromFieldItem() returns 'de' from the 'fallback logic', because the entity language is 'en' for this untranslatable field (see issue comment for reason)
        • Entity is checked for translation anyway (just like with paragraph, essentially: the reference field being untranslatable doesn't mean that the user entity is untranslatable) but is not available. -> We keep the 'en' user.
      Edited by Roderik Muit
    • Please register or sign in to reply
  • 78 80 }
    79 81
    82 /**
    83 * Given a field, get its (parent) entity's language when possible.
    84 *
    85 * @return string
    86 * The entity's language code, or if impossible, a best guess.
    87 */
    88 protected function getEntityLangcodeFromFieldItem(FieldItemInterface $field_item): string {
    89 if (!$field_item->getFieldDefinition()->isTranslatable()) {
    90 // $translated_entity->get(UNTRANSLATABLE_FIELD_NAME) attaches the
    91 // original untranslated entity to the field; see the recursive call in
    92 // ContentEntityBase::getTranslatedField(). Getting the actual entity's
    93 // language is impossible, the next best thing is the current content
    94 // language (as e.g. set through language prefix).
    95 return \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
    • with that it's not possible to render an entity with paragraphs in language FOO when current language is BAR. I'd consider that a fundamental flaw. Let's solve this properly without reaching out to the unsafe, global state.

    • Please register or sign in to reply
  • Wolfgang Ziegler requested changes

    requested changes

  • Please register or sign in to reply
    Loading