Problem/Motivation

Currently, if you render tokens for message, all timestamps are rendered by using current user. With multilingual (and multi-timezoned) this becomes a bit tricky.

In scenario, where there's mail including datetime token being sent to recipient from queue, and the queue is ran in cron, the current user is anonymous, which does not have timezone. This leads in situation where the datetime is default timezone.

The solution for above is to use `account_switcher` service when handling the queue to set current user to be the recipient.

However, if you like to let the user know what timezone was actually used for the timestamps, you would probably add something like
All times are shown as timezone of [current-user:timezone]

And for some reason, this isn't yet possible.
 token for timezone  does not exist

Proposed resolution

Adds a "timezone" user token, which gets either the user's timezone or the default time zone.

 token for timezone exists

Remaining tasks

- Edit the change record for language and code examples following other change records

User interface changes

Adds a new token.
Adds a new translatable string: "The timezone of the user account."

API changes

N/A

Data model changes

N/A

Release notes snippet

Issue fork drupal-2889688

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Dropa created an issue. See original summary.

dropa’s picture

Made timezone available as a token and added test of it to UserTokenReplaceTest

dropa’s picture

Assigned: dropa » Unassigned
Status: Needs work » Needs review

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dropa’s picture

Issue tags: +token

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

socialnicheguru’s picture

Status: Needs review » Needs work

why is this being done in the patch?
+ $account->set('timezone', 'America/Santiago');
If you are using the $user->id, should that user's timezone be used first, then the default of the site be the fallback?

dropa’s picture

Status: Needs work » Needs review

The timezone is set for test, I assume you missed that part? The affecting code is at the bottom of patch.

dropa’s picture

Status: Needs review » Needs work

Didn't notice the patch wont apply anymore.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

olofbokedal’s picture

Re-rolled against 8.7. Not sure if this will work for 8.8 but I figured I still upload the patch since we need it for a project running 8.7.

yogeshmpawar’s picture

Status: Needs work » Needs review

Setting back to Needs Review & Triggering bots. Verified that above patch still applies to 8.8.x branch.

john cook’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Novice
StatusFileSize
new25.45 KB
new25.1 KB
new184.73 KB

Thank you for working on this everyone.

While testing I've used the Token Filter module.

After setting up the text formatter to use tokens, I created a new basic page and the following:

All times are shown as timezone of [current-user:timezone]

Your email address is [current-user:mail]

Before patching:

After patching:

And the timezone is updated when the user changes their locale settings.

Unfortunately the new token does not appear in the token list:

To do this, the user_token_info() function needs to be updated to include the new token.

A change record needs to be drafted as well.

Both these tasks can be novice task, so I've set that tag, and returned the issue to needs work.

fabienly’s picture

Assigned: Unassigned » fabienly

Hi, I will try to finish the patch and write the API change record.

fabienly’s picture

Status: Needs work » Needs review
StatusFileSize
new30.69 KB
new4.38 KB

I take the pacth 12 and I add to user_token_info() the information about the new token as requested in comment #14.
This is a screenshot of the result in the help page (admin/help/token).
screenshot my modification
I will write the API change record draft now.

fabienly’s picture

Assigned: fabienly » Unassigned

I have created the change record draft.
link here

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

darvanen’s picture

Status: Needs review » Needs work

Needs patch (or MR) against 9.3.x

suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new4.38 KB

A re-rolled patch against 9.3.x. Please review.

darvanen’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/user/user.tokens.inc
    @@ -138,6 +142,11 @@ function user_tokens($type, $tokens, array $data, array $options, BubbleableMeta
    +          // Fallback to system default timezone, as timestamps are localised.
    

    Try 'localized'. Drupal's code standards specify "American English" is to be used in the codebase (CSpell custom command error).

  2. The draft change record is not yet fit for purpose. It needs to describe the change, including how it affects site behaviour. Looking at other published change records is a good way to get an idea of the desired output.
akshay_d’s picture

Status: Needs work » Needs review
StatusFileSize
new4.38 KB
new507 bytes

Command failures have been fixed in the updated patch i.e point 1.

Also please elaborate more if point 2 need to have any update to the codebase.

mradcliffe’s picture

Issue summary: View changes
Status: Needs review » Needs work

@akshay_d, a change record doesn't need to have an update the codebase itself. See Write a change record for a Drupal core issue.

I edited the issue summary so next steps are clear.

I briefly reviewed the patch above.

+++ b/core/modules/user/user.tokens.inc
@@ -63,6 +63,10 @@ function user_token_info() {
+    'name' => t("Timezone"),
+    'description' => t("The timezone of the current user account."),

We should use single quotes here.

After that is fixed, I think that you can add the "Needs manual testing" and "Needs issue s

vikashsoni’s picture

StatusFileSize
new64.11 KB
new61.41 KB

Applied #25 patch working fine and looks good for me
After patch user time zone field is added in token

Thanks for the patch
For ref sharing screenshots .....

karishmaamin’s picture

Status: Needs work » Needs review
StatusFileSize
new4.37 KB
new452 bytes

Updated patch #25 as per mentioned in #27

mradcliffe’s picture

Issue summary: View changes
Status: Needs review » Needs work

Thank you for manually testing and uploading the screenshots, @vikashsoni! The final step for completing a manual testing task is to add the before and after screenshots to the issue summary replacing any old screenshots. I modified the issue summary in order to complete that last step.

+++ b/core/modules/user/user.tokens.inc
@@ -64,8 +64,8 @@
-    'name' => t("Timezone"),
-    'description' => t("The timezone of the current user account."),
+    'name' => t('Timezone'),
+    'description' => t('The timezone of the current user account.'),

Thank you for the patch, @karishmaamin! The minor code standard fix looks good to me.

I noticed that this introduces a new translatable string. I looked on localize.drupal.org and found "The timezone of this user.", but I don't think that fits with the other token descriptions which call out "user account" rather than "user". Also, the word "current" isn't used for most of the user token descriptions. I think the we can drop the word "current" as it might be confusing when compared to the other strings. I'm going to bump it back to Needs work for that change. I'm sorry I didn't think about the wording in my previous review.

We still need to modify the draft change record per @darvanen's review in comment #24.

guilhermevp’s picture

Assigned: Unassigned » guilhermevp

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

nikhil_110’s picture

StatusFileSize
new391 bytes
new4.37 KB

Addresses #29 && Attached patch against Drupal 10.1.x

nikhil_110’s picture

Status: Needs work » Needs review
nikhil_110’s picture

Assigned: guilhermevp » Unassigned
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs change record updates

Per #24

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rachel_norfolk’s picture

Issue tags: -Novice

Removing Novice tag until some of the specific reviews are complete.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

bob.hinrichs’s picture

What is the current status of this issue? The patch no longer applies for D11.

darvanen’s picture

@bob.hinrichs you can usually figure that out by working back through the comments. In this case the latest status change is at #37, referencing #24 which hasn't been addressed. We'll need to add rebasing the MR to the list of tasks too.