Problem/Motivation

When there are multiple date values (either repeating or just multiple, single dates), formatted tokens that are defined in smart_date.tokens.inc all return a value based on delta 0 rather than the delta that is being referenced.

Steps to reproduce

Print out token values in the node template for a content type with a smart date field. For example, this template code:

{{ drupal_token('node:field_when:0:value', {node}) }}
{{ drupal_token('node:field_when:0:format:default', {node}) }}
{{ drupal_token('node:field_when:1:value', {node}) }}
{{ drupal_token('node:field_when:1:format:default', {node}) }}
{{ drupal_token('node:field_when:2:value', {node}) }}
{{ drupal_token('node:field_when:2:format:default', {node}) }}

Produces this output:

1725638400
Fri, Sep 6 2024, 11am - 12pm
1726243200
Fri, Sep 6 2024, 11am - 12pm
1726848000
Fri, Sep 6 2024, 11am - 12pm
1727452800
Fri, Sep 6 2024, 11am - 12pm

The 'value' tokens are correct, but the 'format:default' tokens are all returning delta 0 values.

Issue fork smart_date-3492003

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

brian.barry@utexas.edu’s picture

Following up on this, I boiled it down to a minimal install.

Steps to reproduce

Install Drupal 10.3.10
Install module Smart Date 4.2.1
Install module Token 8.x-1.15
Install module Twig Tweak 3.4.0
Create content type with Smart Date field (field_when)
Insert code into node.html.twig template:

value - {{ drupal_token('node:field_when:0:value', {node}) }}<br>
format:default - {{ drupal_token('node:field_when:0:format:default', {node}) }}<br>
value - {{ drupal_token('node:field_when:1:value', {node}) }}<br>
format:default - {{ drupal_token('node:field_when:1:format:default', {node}) }}<br>
value - {{ drupal_token('node:field_when:2:value', {node}) }}<br>
format:default - {{ drupal_token('node:field_when:2:format:default', {node}) }}<br>

In this case I created a node with only two date values, but invoking a formatted token for delta 2, which is empty, still returns the value of delta 0.

value - 1733572800
format:default - Sat, Dec 7 2024, 6 - 7am
value - 1733659200
format:default - Sat, Dec 7 2024, 6 - 7am
value - [node:field_when:2:value]
format:default - Sat, Dec 7 2024, 6 - 7am

natts’s picture

The delta part preceding any of smart_date's tokens is checked for, but then discarded, hence always defaulting to zero (the first item).

So I've updated this to either use the delta (if given), or default to zero.

Here's the patch for temporary use until this gets released.
---
natts made their first commit to this issue’s fork.

natts’s picture

Status: Active » Needs review
Issue tags: +delta
mandclu’s picture

Version: 4.2.1 » 4.3.x-dev

  • mandclu committed 28d52db4 on 4.3.x authored by natts
    fix: #3492003 Formatted tokens all return values for delta 0
    
    By: brian....
mandclu’s picture

Status: Needs review » Fixed

Thanks for the work here. Merged into the 4.3.x branch.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.