Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
twig_tweak
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
twig_tweak
Merge requests
!1
Issue
#3185016
: Translation filter
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Issue
#3185016
: Translation filter
issue/twig_tweak-3185016:3185016-translation-filter
into
8.x-2.x
Overview
0
Commits
1
Pipelines
0
Changes
1
Merged
Ariel Barreiro
requested to merge
issue/twig_tweak-3185016:3185016-translation-filter
into
8.x-2.x
4 years ago
Overview
0
Commits
1
Pipelines
0
Changes
1
Expand
0
0
Merge request reports
Compare
8.x-2.x
version 1
4a650acd
4 years ago
8.x-2.x (base)
and
latest version
latest version
4a650acd
1 commit,
4 years ago
version 1
4a650acd
1 commit,
4 years ago
1 file
+
29
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/TwigExtension.php
+
29
−
0
Options
@@ -393,6 +393,19 @@ class TwigExtension extends AbstractExtension {
// {{ node.field_media|file_url }}
// @endcode
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'
))
{
@@ -1319,4 +1332,20 @@ class TwigExtension extends AbstractExtension {
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