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.
When using tokens, all of the address fields are available as tokens except for the country name.
The only token for country is "country_code".
Can support for a country name token be added?
Comment | File | Size | Author |
---|---|---|---|
#31 | interdiff-2915147-29-31-do-not-test.diff | 2.79 KB | lisastreeter |
#31 | 2915147-31-country-name-token.patch | 12.51 KB | lisastreeter |
| |||
#29 | interdiff-2915147-20-29-do-not-test.diff | 2.99 KB | lisastreeter |
#29 | 2915147-29-country-name-token.patch | 14.22 KB | lisastreeter |
| |||
#26 | 2915147-26-country-name-token.patch | 13.64 KB | lisastreeter |
Comments
Comment #2
rmcdahal CreditAttribution: rmcdahal commentedAny Updates ?
Comment #3
rmcdahal CreditAttribution: rmcdahal commentedStill No Solutions For This
I have opened another issue
Comment #4
weseze CreditAttribution: weseze commentedWhy did you open a new ticket for the exact same issue?
Comment #5
lisastreeter CreditAttribution: lisastreeter at Centarro commentedFirst attempt at a country name token patch, with tests.
Comment #6
lisastreeter CreditAttribution: lisastreeter at Centarro commentedUpdate to try to get tests to run.
Comment #7
lisastreeter CreditAttribution: lisastreeter at Centarro commentedContinuing to work on test code.
Comment #8
lisastreeter CreditAttribution: lisastreeter at Centarro commentedmissing use statement fixed.
Comment #9
lisastreeter CreditAttribution: lisastreeter at Centarro commentedexplicitly enable token module
Comment #10
lisastreeter CreditAttribution: lisastreeter at Centarro commentedComment #11
lisastreeter CreditAttribution: lisastreeter at Centarro commentedComment #12
lisastreeter CreditAttribution: lisastreeter at Centarro commentedAdded to kernel test annotation:
* @requires module token
Comment #13
lisastreeter CreditAttribution: lisastreeter at Centarro commentedComment #14
lisastreeter CreditAttribution: lisastreeter at Centarro commentedCountry name has been added as an option for any address field.
In order to get the address implementation of hook_tokens to execute after the hook_tokens function for the field module (in token.tokens.inc), I needed to implement hook_module_implements_alter.
Comment #15
bojanz CreditAttribution: bojanz at Centarro commentedGreat job on the tests!
Could we have used hook_token_info_alter() to avoid needing address_module_implements_alter()?
Why do we need a custom type? Shouldn't we be able to reuse an existing field token type, as defined in https://cgit.drupalcode.org/token/tree/token.tokens.inc#n1391?
Comment #16
lisastreeter CreditAttribution: lisastreeter at Centarro commented@bojanz Thank you. I did try using hook_token_info_alter() instead of hook_token_info(), but it didn't seem to make any difference. I can try again, though, if you think it might. I tried so many different things that it's quite possible I missed some simple combination.
As for the custom type, initially, I just created the country_name tokens as if country_name was just another property of the address field and for the token type, I just used:
\Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($entity_type_id)
That worked fine, but I thought that maybe creating a custom type would somehow differentiate country_name from the actual address field properties, to help with my hook_tokens not getting overridden by logic in the token.tokens.inc field_tokens() method. Also, it seemed like everything I read about, "how to create custom tokens", started by explaining how to create a custom type. So I thought perhaps my code would be somehow incomplete if I didn't. (Even though it worked without it.) I'll happily remove the custom type to simplify the code.
As for getting rid of address_module_implements_alter(), my issues that field_tokens() (in token.tokens.inc) treats 'country_name' as if it's just another address field property and does this to my tokens (line 1695):
$replacements[$original] = $field_item->$property_name;
Since $field_name->country_name doesn't exist, and since field_tokens() executes after address_tokens(), my country_name replacement values are overwritten.
Essentially, I think my issue is that I want country_name tokens to behave as if they're address field properties so that they'll show up nicely in the tokens UI as part of address fields. But then when it comes time to replace the tokens with the actual values, I'd like for field_tokens() to just leave them alone and not try to set a replacement value for a field property that doesn't actually exist.
Comment #17
lisastreeter CreditAttribution: lisastreeter at Centarro commentedI did some experimenting. If I change the field type to an existing type like this:
'type' => $token_type . '-' . $field_name,
Then a weird little drop-down triangle appears next to the country name token (with nothing underneath it when clicked.)
For normal field properties, the 'type' is omitted from the definition:
If I omit type from the token definition for the country_name, everything seems to work fine, and the funny triangle does not appear. So I think I can either 1) get rid of the custom type and the 'type' part of the country_name tokens definition. Or 2) leave it as it is with the custom type.
Comment #18
bojanz CreditAttribution: bojanz at Centarro commentedLess code is always a good idea.
Comment #19
lisastreeter CreditAttribution: lisastreeter at Centarro commentedRemoved the custom token type.
Comment #20
bojanz CreditAttribution: bojanz at Centarro commentedisSubclassOf() is deprecated.
String references to classes/interfaces should be replaced by class references (ContentEntityInterface::class) in this case.
The test is not passing, the build is green here because it was skipped (probably due to the optional dependency.)
There is no "address" entity type.
This crashes without the "text" and "filter" modules listed in $modules.
I get "2x: KernelTestBase::enableModules() should not be called from setUp(). Use the $modules property instead."
The test has no use statement for Node.
I fixed the above, but the test is still crashing, so setting to "needs work". Uploading updated patch.
Comment #21
lisastreeter CreditAttribution: lisastreeter at Centarro commentedFixed various errors in the Kernel test including an incorrect use statement, missing setUp items, and a token name typo.
Comment #22
lisastreeter CreditAttribution: lisastreeter at Centarro commentedKernel test was passing locally but failed here. Trying a changed base class.
Comment #23
lisastreeter CreditAttribution: lisastreeter at Centarro commentedKernel test doesn't seem happy with including any test base code from token module. Removed it in this patch.
Comment #24
lisastreeter CreditAttribution: lisastreeter at Centarro commentedA little coding standards cleanup now that testbot is happy with test.
EDIT: actually...just looked more carefully: the test was skipped.
Comment #25
lisastreeter CreditAttribution: lisastreeter at Centarro commentedComment #26
lisastreeter CreditAttribution: lisastreeter at Centarro commentedRemoved from the annotation: @requires module token
Comment #27
lisastreeter CreditAttribution: lisastreeter at Centarro commentedPatch #24 has in its class annotation:
@requires module token
Patch #26 does not have that in its annotation.
Patch #24 does not run the CountryNameTokenTest kernel tests.
Patch #26 fails because the token module is unavailable (and says the test class needs to be annotated with '@requires module token')
So I'm not sure what I can do at this point.
Comment #28
lisastreeter CreditAttribution: lisastreeter at Centarro commentedI don't think this is the intended usage for the test_dependencies property in the info.yml file, but perhaps it will encourage testbot to include the token dependency.
Comment #29
lisastreeter CreditAttribution: lisastreeter at Centarro commentedI just learned that test_dependencies will work but only if it's committed first.
The alternative is to add token to require-dev in the module's composer.json file, which I've done here.
Documentation for this was just added here: Managing dependencies for a contributed project
Comment #30
lisastreeter CreditAttribution: lisastreeter at Centarro commentedComment #31
lisastreeter CreditAttribution: lisastreeter at Centarro commentedChanged base class to reduce code by reusing token module test code.
Comment #33
bojanz CreditAttribution: bojanz at Centarro commentedThank you, Lisa.
I've removed the require-dev, now that test_dependencies should be recognized post-commit.