0

My setup is simple:

A content type "film" with a taxonomy field "Type of film".

The content type "film" is translatable, but not the field "type of film" (same type for all languages): only the entity type "Type of film" is translatable.

If I display a film with an English interface, I will get "Comedy" as the type of the film and in French, I will get "Comedie".

If I create a view showing the films teasers, I will have Comedy/Comedie depending if I ask to display French or English films.

Until this point, everything runs fine...

But, as all the films are not translated, I would like to have Comedie if it is a French interface (http://domain/fr) even if the teaser is displaying an English (not translated) film.

In the view, I have used

FILTER CRITERIA: Content: Default translation (= True)

LANGUAGE: Rendering Language:Interface text language selected for page

In the Manage display of the film's teaser, I choose "label" to display my taxonomy field... without any option to display it into the current language (now it is displayed into the content type language)

EDIT TO ADD:

Following the advise from @Berdir, I have created a new formatter: /my_module/src/Plugin/Field/FieldFormatter/CurrentLanguageLabelFormatter.php

<?php

namespace Drupal\artprod\Plugin\Field\FieldFormatter;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;

/**
 * Plugin implementation of the 'current language label' formatter.
 *
 * @FieldFormatter(
 *   id = "current_language_label",
 *   label = @Translation("Current Language Label"),
 *   description = @Translation("Display the label of the referenced entities using the language of the page."),
 *   field_types = {
 *     "entity_reference"
 *   }
 * )
 */
class CurrentLanguageLabelFormatter extends EntityReferenceLabelFormatter {

    protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items,$langcode) {
        $entities = [];

        foreach ($items as $delta => $item) {
            // Ignore items where no entity could be loaded in prepareView().
            if (!empty($item->_loaded)) {
                $entity = $item->entity;

                // Set the entity in the current language for display.
                // HERE IS THE CHANGE
                if ($entity instanceof TranslatableInterface) {
                    $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
                    $entity = \Drupal::entityManager()->getTranslationFromContext($entity, $language);
                }

                 $access = $this->checkAccess($entity);
                // Add the access result's cacheability, ::view() needs it.
                $item->_accessCacheability = CacheableMetadata::createFromObject($access);
                if ($access->isAllowed()) {
                    // Add the referring item, in case the formatter needs it.
                    $entity->_referringItem = $items[$delta];
                    $entities[$delta] = $entity;
                }
            }
        }

        return $entities;
    }
}

In the annotation, I have changed the id and the description

In the class body, I have copied the function getEntitiesToView from the class EntityReferenceFormatterBase except that I changed the chunk of code which is translating the entity.

After restarting Apache and clearing the cache, I could select this overrided formatter for my taxonomy field.... AND the label are now displayed in the current language (vs language of the parent entity)

1

The language of the parent entity is passed to entity formatters and they respect that when displaying referenced entities.

If you want to override that, you can for example provide your own entity formatter plugin that overriddes \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView() and ignores the passed in $langcode. There is no configuration for that.

Since you probably want the entity label to be displayed, you should subclass \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter.

Alternatively, you can also do something in preprocess/twig.

  • Thank you Berdir, I have added the description of your solution in my question... But I think I missed something. – Baud Apr 15 '18 at 21:03
  • @Baud There's one "Field" too much in your namespace, "Core\Field" in this case is the "module" here, in your case Plugin is the top-level namespace below the modulename. – Berdir Apr 16 '18 at 6:08
  • many thank it solve the problem; I can use the formatter... but it is still in the wrong language. I have updated the question (showing also my preprocess function) – Baud Apr 16 '18 at 10:10
  • If you don't explicitly specify a language, it will use the default, which could be anything. So instead of removing the call, it is likely more correct to just remove the $langcode argument from it, should have suggested that – Berdir Apr 16 '18 at 10:32
  • I succeed... just before going in vacation... Thanks a lot... – Baud Apr 16 '18 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.