Browse Source

Issue #3185016 by hanoii: Translation filter

merge-requests/7/head
hanoii 4 years ago committed by Chi
parent
commit
0f9c68957e
  1. 29
      src/TwigExtension.php

29
src/TwigExtension.php

@ -393,6 +393,19 @@ class TwigExtension extends AbstractExtension {
// {{ node.field_media|file_url }} // {{ node.field_media|file_url }}
// @endcode // @endcode
new TwigFilter('file_url', [$this, 'fileUrl']), new TwigFilter('file_url', [$this, 'fileUrl']),
// - Entity translation -
//
// Gets the translation of the entity for the current context.
// @code
// {{ node|translation }}
// @endcode
//
// An optional language code can be specified.
// @code
// {{ node|translation('es') }}
// @endcode
new TwigFilter('translation', [$this, 'entityTranslation']),
]; ];
if (Settings::get('twig_tweak_enable_php_filter')) { if (Settings::get('twig_tweak_enable_php_filter')) {
@ -1319,4 +1332,20 @@ class TwigExtension extends AbstractExtension {
return $output; return $output;
} }
/**
* Returns the tranlation for the given entity.
*
* @param object $entity
* The entity to get the translation from.
*
* @param string $langcode
* (optional) For which language the trnaslation should be looked for,
* defaults to the current language context.
*
* @return EntityInterface
* The appropriate translation for the given language context.
*/
public function entityTranslation(EntityInterface $entity, $langcode = NULL) {
return \Drupal::service('entity.repository')->getTranslationFromContext($entity, $langcode);
}
} }

Loading…
Cancel
Save