3

In a Drupal 8 site I have a node content type "page" that references a custom entity type using inline entity form. "Page" also has a paragraphs field usig the Paragraphs module.

The site have multiple languages and therefore the following scenario appears:

  1. I have an existing "page" node in both english and german containing entity references and paragraphs.
  2. On the english node I add a new paragraph and a new entity reference and save the node.
  3. When viewing the node in english I see the added content.
  4. When viewing the node in german I also see the new content, which is in english.

I don't want number 4 to happen. That is, I don't want the new untranslated entity and paragraph to be visible on the german translation of the node. The entity reference and paragraph fields are not set as translatable but the target entities are translatable. From what I understand, this means that the newly created entity will be in english, the same is true for the paragraph.

To solve this I'm thinking I should be able to filter out the untranslated entities and paragraph items when viewing the node so that any item that is not in the current language is not shown. The they won't be visible until I go and edit the german node and resave it, which will create translated versions of the entity and paragraph item.

But I am unsure how to go about filtering the fields. Is there any suitable hook that can be used? Or am I going about this in the wrong way?

  • Paragraph does currently not support translatable fields. If not yet, then possibly in the future, things might break. You can still translate the paragraph itself, it's only the reference to the paragraph that you make translatable or not. Editing and resaving and things like that won't work as you think, your paragraphs will get out of sync and you will have completely different paragraphs in each. – Berdir Mar 15 '16 at 19:00
  • Sorry, I'm not actually having the paragraph reference field translatable, only the paragraph itself is set as translatable along with some of its fields. This seems to work fine, except when I add a new paragraph to a node which already has translations. From what I understamd the new paragraph will not yet have a translated version and should therefore by "filterable" like I'm describing above. Would that be possible? – reekris Mar 15 '16 at 19:06
  • I have edited the question to clarify that the fields are not set as translatable but te target entities are. – reekris Mar 15 '16 at 19:20
  • Ah, I see. Good :) Not easily, but I'll see about adding a quick answer. – Berdir Mar 15 '16 at 20:54
4

The logic that defines if entities should be shown is in \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView()

As you can see, translation is checked there, but the API doesn't directly support skipping something, the hooks behind that only allow to control applicable fallback languages and their order.

It's possible that you can use entity access, which is checked after that. Implement hook_paragraph_access(), for the view operation, check if the paragraph has a translation in the current content language and if not, forbid access. The same should work for your other entity reference, it's the same could, just implement hook_ENTITY_TYPE_access() for that too.

If that doesn't work, you can write your own field formatter plugin and override that method.

  • Brilliant solution! Using an access hook is working great. It's also very easy to extend it to include additional entity types to restrict. It actually solved another question I had about hiding untranslated menu items by simply adding a language check on nodes, drupal.stackexchange.com/questions/190213/…. Here is a gist of my current solution for filtering based on language gist.github.com/reekris/e6f64de799df75d724c2. Thank you so much Berdir! – reekris Mar 16 '16 at 8:09

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.