Closed (fixed)
Project:
Token
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
22 May 2015 at 15:11 UTC
Updated:
18 Mar 2019 at 18:56 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
Maouna commentedComment #2
Maouna commentedComment #3
berdirDon't forget to set issues to needs review when posting patches, so that they get tested.
Comment #4
berdirComment #5
Anonymous (not verified) commentedThe function should see if the field is instance of the \Drupal\Core\Field\FieldDefinitionInterface instead. Also by checking the provider of the token it basically completely skips all fields so I don't understand the point of this function at all. It would make sense if:
Comment #6
Anonymous (not verified) commentedThis is my train of thought.
Comment #8
it-cruFix failed testing and switch to renderPlain to avoid fatal errors when rendering for example tokens in metatag fields.
Comment #9
chr.fritschComment #12
Maouna commentedMy ideas to #6 and #8 are here: https://www.drupal.org/node/2560191#comment-10586004
@#5:
Which function and which check are you talking about?
I do think that it makes sense that field_tokens() in the tokens module only replaces tokens provided by this module. If that token was added by another module, that one is also responsable for the replacement. As the field tokens are added in the function field_token_info_alter() like this:
they are not skipped in the replacement.
Which tokens do you think are getting lost with this check?
Comment #13
juampynr commentedComment #14
juampynr commentedRe-rolling.
Comment #15
berdirThanks for the reroll. I think that patch is doing too much. We shouldn't remove any existing tokens here IMHO. Instead, I'd prefer to more or less go back to the original patch above, add the necessary bits from #2560191: Provide tokens for all entities out of the box to define the entity type, make sure to also remove the instanceof check when defining field info and most importantly, tests.
We should be able to have a test similar to the existing one for node except it would be for EntityTest from entity_test.module including testing the output and info of various base fields like ID, label, and so on.
Follow-up: Then we need to review which of the existing tokens can be dropped because they're also covered through this generic implementation. We will also risk duplicate tokens for existing entity types, which is a bit problematic. For example, node defines node:author, this would expose node:uid which would however essentially result in the same.
Comment #16
miiimoooAttached find a new patch based on the previous ones that adds support for all content entity types and base fields against 8.x-1.x (918d0d5)
Comment #17
miiimoooComment #18
berdirThanks.
We already have code that adds token info for configurable fields in field_token_info_alter(). Instead of adding a new block, we should simply update that to remove the FieldStorageConfigInterface check. Its not really at the right place but we can easily move it once it's working. Or maybe even here.
There's also token_token_info_alter() which for example defines a label token for each entity type that exists as a token type. We can update that to define entity types that don't exist yet. We should also support the token_type setting and respect that if set.
Similar in the second part, instead of explicityl looking for both interfaces, just remove that check. We want to support anything.
Comment #19
berdirOne thing to think about, at least for a first version, might also be to only define tokens for base fields for entity types that we define ourself. The reason is that those that already exist likely also have many tokens that overlap/duplicate those that we'd define (e.g. author vs. uid). We could avoid that if we only do it for entity types that have no custom integration.
Comment #20
NetNerdy commentedpatch #16 is not running for me: Drupal 8.1.1 and Token 8.x-1.x-dev and PHP 7.x
some patches using (e.g. in token.tokens.inc around line 1265)
($entity->get($field_name)
(e.g. "Chained tokens for field tokens")
and some others
($entity->get($name)
(e.g. this one and "Replace field tokens by empty strings when there is no field value")
i'am not sure what's right or wrong...
Comment #21
Bambell commentedMade the changes suggested by @Berdir and added a test.
Comment #22
Bambell commentedAdding a check that a
uidtoken isn't generated for thenodeentity type.Comment #23
berdirNice, really close now.
Can we add a comment here why we chose a contact message:
We use contact messages as that entity type does not provide any tokens by default.
$entity is not a node :)
Check token info instead, that is enough. getTokenInfo('node', 'uid')
Lets use 'module' => 'token'. We do the same for field tokens for the same purpose.
now it is only about ensuring that the token type exist.
or is defined in token module.
Comment #24
Bambell commentedThere. However, I'm sure there's a more elegant way to check what tokens are defined for the Token module..
Comment #25
bojanz commentedSmall cleanups, tentative RTBC.
Added a todo to fix _token_field_label() in a followup, cause it's not following core logic anymore.
Comment #26
berdirstrotolower() is hardcoding something that only some languages do, it will look strange in german for example.
also we should at least include plural in the placeholder to make it clear to translators.
Maybe the easiest way out: Do not add a description. Does it actually add something useful?
We have _token_module() to get the module of a token, maybe we could also add a function (or a method on our token service replacement) for this use case?
Can be a follow-up.
The return value of the function is actually out of sync on purpose, the structure that that function returns is very weird and way too complicated for what we need. However, the deterministic sort based on the label might make sense.
Comment #27
bojanz commentedYes, but that is a known core bug. We have an open issue about getLowercaseLabel() and friends.
I'm fine with not having a description.
Comment #28
bojanz commentedHere we go, no description.
Comment #31
bojanz commentedForgot to update origin.
Might make more sense to call this testBaseFieldTokens(), can be changed pre-commit.
Comment #32
bojanz commentedBerdir asked me to test manually, I did, with Commerce and Pathauto (latest -dev), works fine.
Comment #34
berdirPushed, thanks everyone!
Comment #36
jienckebd commentedNot sure if the API in core changed since this patch was committed, but the line referenced in this patch doesn't catch base fields added by hook_base_field_info().
Base fields implement \Drupal\Core\Field\FieldStorageDefinitionInterface and do not implement \Drupal\field\FieldStorageConfigInterface. This 1 line change seems to make it work.
Comment #37
heddnMoved that last patch over to #2877268: uuid support since that is open and this is closed.