Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Add a token type for the current page request's language; see #975116: Create a 'language' token type (D7) for the D7 equivalent.
Comment | File | Size | Author |
---|---|---|---|
#29 | 2863200-29.patch | 18.97 KB | paulocs |
#27 | interdiff.txt | 2.26 KB | Dom. |
#27 | 2863200-27.patch | 18.93 KB | Dom. |
#23 | interdiff.txt | 7.67 KB | pfrenssen |
#23 | 2863200-23.patch | 16.79 KB | pfrenssen |
|
Comments
Comment #2
DamienMcKennaComment #3
DamienMcKennaSome sample code was posted in #975116-30: Create a 'language' token type (D7) that might help whoever works on this.
Comment #4
DamienMcKennaComment #5
DamienMcKennaThis provides a partial port of the patch from the other issue. Right now it only supports 'langcode', 'name' and 'direction', the language object doesn't provide the other attributes without extra digging.
Comment #6
vakulrai CreditAttribution: vakulrai as a volunteer and at gai Technologies Pvt Ltd for gai Technologies Pvt Ltd commented@DamienMcKenna i have applied the patch it is working fine and is adding language interface token.
Comment #7
BerdirThe native name concept is gone, the domain and prefix are now in a separate config file, see language.negotiation.yml.
I don't quite understand the approach here. This defines separate types for interface and content language but those things are identical, it's just a language, the difference is where it comes from.
What if we'd tie this to the current-page token type, so you'd do something like [current-page:language-interface:name] ?
We only need a single language token type then with its tokens and two new tokens on the current-page type?
Comment #8
DamienMcKenna@Berdir: FYI I simply recreated the D7 patch so it would work on D8, no additional thought was put into how to make it work better.
Comment #9
johnchqueWorking on this.
Comment #10
johnchqueNot working yet, not sure why, investigating.
Comment #11
johnchqueThis works, we also need to work in tests now. :)
Comment #12
BerdirThis looks good but needs tests now.
See \Drupal\token\Tests\TokenMenuTest::testMultilingualMenu() for an example, we need to set up a set with at least two languages, then display those tokens somewhere and assert the displayed text.
\Drupal\Tests\token\Functional\UrlTest::testBlockUrlTokenReplacement() uses a block for that and tokens built-in token replacement functionality in that. So you can place a block that displays the language name, langcode and so on and then assert the strings on the page.
Comment #13
raphaeltbm CreditAttribution: raphaeltbm commentedHi! Not sure if it's the perfect place for that, but. Should be it useful to have more language aware tokens? Something like:
See this Metatag issue #2956982: Port hreflang tokens to D8
Comment #14
marassa CreditAttribution: marassa commented@raphaeltbm: What are those tokens supposed to return? Do you have a use case for that?
The current page language is very clear - it's the language in which the current page is rendered.
What's the "site language" for a site available in three languages? What's the "node language" for a node translated into three languages?
Comment #15
raphaeltbm CreditAttribution: raphaeltbm commented@ndlarge: My bad, i guess $options['langcode'] already allow to get the translated url for a node, entity, the current page or the frontpage, right?
Modules like Metatag have fields specifics to different languages, to manage alternate hreflang for example.
But i guess the issue is specific to Metatag here (and all the modules having this kind of implementation), these fields should be able to manage themself a token as [current-page:url] and set the langcode corresponding to their own language context, rather than set the current language by default. Before the tokenService replacement.
And so you should be able to do something like this:
Better than this:
Well... I will post this problematic in Metatag issues.
Comment #16
Joe HuggansNow working, thanks
Comment #17
pfrenssenI'm assuming that @yongt9412 is not actively working on it any more. I am assigning this to me to provide test coverage.
Comment #18
joevagyok CreditAttribution: joevagyok at Arhs for European Commission and European Union Institutions, Agencies and Bodies commentedThe patch under comment #11 still has the native name.
$replacements[$original] = $language->native;
As @Berdir stated the native name concept has been removed, so it should be removed from the patch as well.
Comment #19
joevagyok CreditAttribution: joevagyok at Arhs for European Commission and European Union Institutions, Agencies and Bodies commentedBy reading the tokens for me it makes more sense to do the following naming for the different contexts:
The common is the language itself, so the first part should refer to the specific element rather than starting with the common.
Like "current-page" in the token, or "current-user".
Comment #20
pfrenssenI added a test. The current implementation is failing on a few points:
$language->prefix
and$language->domain
which do not exist. This data is saved in configuration.[language:name]
and[language:langcode]
don't work on their own. Similar to the other tokens which are localized (such asdate
andurl
) we can fall back to the default language in the absence of any passed in language.langcode
parameter in the$data
array, but this should be passed in the$options
array, as documented inToken::replace()
:In this patch I have only added the test. These problems still need to be addressed.
Comment #21
pfrenssenThis addresses the problems outlined in comment #20.
I will look to fix the comments from #18 and #19 now.
Comment #23
pfrenssenAddressed comments #18 and #19.
Comment #24
BerdirI still have very mixed feelings about data providers on kernel tests.
This doesn't really make it easier to understand for me and it's 10x slower. token.module has some assert helpers to assert a bunch of tokens and their expected value. Doesn't support cacheablity metadata, but asserted here either.
\Drupal\Tests\token\Kernel\NodeTest for example.
Comment #25
joseph.olstad+1 for this, I just spent 20 minutes looking for the language token (a requirement for our implementations)
to fix
after
extra
patches
add this to your composer.json:
alternatively, there is the token_language module. I haven't tried it yet.
Comment #26
Promo-IL CreditAttribution: Promo-IL commentedFor current language prefix
https://www.drupal.org/project/token_custom
current language prefix PHP:
Comment #27
Dom. CreditAttribution: Dom. as a volunteer and at ACINO commentedHi!
I don't want to interfere with your excellent work here and hope the following helps.
I don't know if it is still time to add some more functional in the patch #23 ?
If yes, let me add it here, otherwise I can open a follow-up if better, just let me know.
Because we now have 'language' tokens, a very simple addition let those tokens exists for every entities. That means with the patch attached, it is possible to do new things like :
The follow displays the [node:language:name] version of a content originaly written in [node:source:language:name]
being output as:
The follow displays the English version of a content originaly written in French
I am adding also in the patch a simple change in NodeTest.php that demonstrate it working. Let me know if more test are needed.
Comment #28
Berdir> I still have very mixed feelings about data providers on kernel tests.
Apparently nobody wants to do something about my feelings here, fair enough. ;)
The entity token addition seems useful. Looks like this has quite a few followers and there has been no negative feedback on those patches, I wanted to commit it, but trying to run that language test locally gave me a segfault on PHP 7.4 in pcre2_jit_match_8 o.0. Lets see what Testbot has to say about different PHP versions.
Comment #29
paulocsHere is the patch rerolled so the tests can be triggered.
I tested it locally and didn't have problems with php 7.4 and 7.3
PHP 7.4 passes...
Comment #30
NWOM CreditAttribution: NWOM commented#29 works for me. Thanks!
Comment #32
BerdirFinally getting back to this, thanks for the reroll, committed.
Comment #34
AnybodyJust searched for the language tokens in a project and found out that there's no stable release yet containing this important part (for metatags). Is a release planned containing this commit?
Comment #35
Giuseppe87 CreditAttribution: Giuseppe87 commentedI'm confused: is this going to be ported to Drupal 9\does D9 include this somehow?
I've searched the code but I didn't find anything about language tokens, and the patch obviously doesn't apply for 9.2.x
Comment #36
james.williams CreditAttribution: james.williams at ComputerMinds commentedThis has been committed to the dev version of the Token module (8.x-1.x-dev), which can be used with Drupal 8 or 9. No 'proper' release of the module has been created since then, so you would either need to use that dev release, the patch, or wait for the next alpha/beta/rc/stable version of Token module.
Comment #37
Giuseppe87 CreditAttribution: Giuseppe87 commentedOk I see, thank you.
Comment #38
joseph.olstad@Berdir , if you need assistance with creating a tagged release let us know maybe one of us can become a co-maintainer? It's been 7 months since this new token was committed in dev but it's still not in a tagged release.
feel free to assign me if you wish.
Comment #39
Anybody+1 for #38 - would be very helpful to have a new stable release :)
Comment #40
themodularlabFYI, looks like this was included in the latest release: https://www.drupal.org/project/token/releases/8.x-1.10. Thanks!