- 🇺🇸United States DamienMcKenna NH, USA
I've updated the issue summary, is that workable?
- Status changed to RTBC
over 1 year ago 11:28pm 9 May 2023 - 🇦🇺Australia darvanen Sydney, Australia
I was about to say we need a change record but then I realised this is for the token module, not core.
Thanks everyone for your hard work, let's see what the maintainers think.
- Status changed to Needs work
over 1 year ago 10:34pm 12 May 2023 - 🇨🇭Switzerland berdir Switzerland
Nice work on caching and the tests, I do have a few comments though, some of which you're not going to like I fear :)
-
+++ b/tests/src/Functional/TokenCurrentPageTest.php @@ -64,4 +72,94 @@ class TokenCurrentPageTest extends TokenTestBase { $this->assertPageTokens("/node/{$node->id()}", $tokens, [], ['url_options' => ['query' => ['foo' => 'bar']]]); } + /* + * Test tokens like [current-page:node:nid]. + */
missing empty line.
-
+++ b/tests/src/Functional/TokenCurrentPageTest.php @@ -64,4 +72,94 @@ class TokenCurrentPageTest extends TokenTestBase { + $assert_session->elementContains('css', '#block-token-url-test-block', $test['node1_new_expected']); + // Change to to original title. + $node1->set('title', 'Node the First')->save();
wrong indendation.
-
+++ b/token.tokens.inc @@ -339,6 +339,20 @@ function token_token_info() { + foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) { + // Do not generate tokens if the entity doesn't define a token type or is + // not a content entity. + if (!$entity_type->get('token_type') || (!$entity_type instanceof ContentEntityTypeInterface)) { + continue; + } + + $info['tokens']['current-page'][$entity_type_id] = [ + 'name' => t('The current %type', ['%type' => $entity_type->getLabel()]), + 'description' => t("The current page object if that's a %type", ['%type' => $entity_type->getLabel()]), + 'type' => \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($entity_type_id), + ];
this will generate a ton of token types and they will all show up in the token UI in most cases, then.
There's also a risk of conflicts if an entity type were to be called tile, query or another existing token of this type. It will also might result in less experienced users picking the wrong kind of tokens for example for pathauto patterns.
There's one option that will address most of that, at the loss of token suggestions and it's also going to be a pain for everyone using the patch already. A dynamic token type, like [current-page:entity:?] like :query: already is.
-
+++ b/token.tokens.inc @@ -875,6 +889,52 @@ function token_tokens($type, array $tokens, array $data, array $options, Bubblea + + // Load entity if it exists. + $entity_id = \Drupal::routeMatch()->getRawParameter($entity_type); + if ($entity_id) { + $entity = $entity_type_manager->getStorage($entity_type)->load($entity_id); + } + if (!isset($entity)) { + break; + }
There are a number of special routes that will cause problems or not work, what I know:
* node previews.
* node revisions. will use the current default revisions and not the revision
* latest routes from content moderation, will also use the default revision.What's the reason for not using the loaded parameter? that would handle the third case.
For the first two cases, we have this in a project as a helper function. for node_preview, we could support looking that $entity_type . '_preview' although that's currently a node specific thing.
entity revision routes are becoming more common, we could support $entity_type . '_revision' as well for that.
-
+++ b/token.tokens.inc @@ -875,6 +889,52 @@ function token_tokens($type, array $tokens, array $data, array $options, Bubblea + if (!empty($langcode) && $entity->hasTranslation($langcode)) { + $options['langcode'] = $langcode; + }
This should use $entity = \Drupal::service('entity.repository')->getTranslationFromContext($entity, $langcode);. But if we use the loaded entity from the route context it won't be needed I think because that's already in the correct language.
Also, it should be further up if not, because the entity label token is not using the correct translation currently.
-
- Status changed to Needs review
over 1 year ago 6:26am 22 June 2023 - last update
over 1 year ago 78 pass - 🇮🇳India Ajeet Tiwari
Resolved comments mentioned 1,2 and 5. and uploading patch for the same.
- 🇦🇺Australia darvanen Sydney, Australia
@Ajeet Tiwari can you provide an interdiff → please?
- Status changed to Needs work
over 1 year ago 11:14pm 5 July 2023