Problem/Motivation

When a numeric (integer) subfield is retrieved in Twig via Custom Replacement of the same field in a view, there are 2 problems:

  • the choice of the formatter is not used, always the key is retrieved and never the label
  • the value 0 is returned as an empty string and not as the 0 value

Steps to reproduce

Create a custom field X and a subfield Y that is an integer with permitted values 0 and 1 with corresponding labels.
Create a view, add Y, set the format to Label, and in the custom replacement of Y retrieve the Y via Twig:
-) the key is shown and not the label
-) when Y = 1 => "1" is displayed (ok)
-) when Y = 0 => "" is displayed (wrong, empty string)

Proposed resolution

  • Use the rendered value by default for token replacement
  • Add a new __raw token option to explicitly account for raw value stored in db
  • Account for map|map_string raw token types to json decode the serialized value to string
  • Account for integer value = 0 can be assumed an empty value so remove the !empty check
  • Simplify the field title to account for core bug that jumbles the rendered output due to extra : part in label due to subfield structure

Remaining tasks

n/a

User interface changes

n/a

API changes

Data model changes

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

giuse69 created an issue. See original summary.

apmsooner’s picture

Assigned: Unassigned » apmsooner

Hi, thanks for the report. I'm able to reproduce and realize there are some revisions overall that need to be made to how views tokens work for custom fields. I will work on a patch and request your review when ready. Thank you.

apmsooner’s picture

Title: Incorrect retrieval of integer subfield via Twig » Views token replacement: Incorrect retrieval of integer subfield via Twig

apmsooner’s picture

Assigned: apmsooner » Unassigned
Issue summary: View changes
Status: Active » Needs review

@giuse69,

This is ready to test. See bullets in the Proposed resolution. I've addressed your bug in multiple ways because now the default token you were using will use the rendered value and you'll notice there is a supplemental __raw version of the token if you want to output the stored value in the db.

giuse69’s picture

Hi @apmsooner, seems ok to me: I tested succesfully all bullets of the proposed solution except the third for map.
You're great!

apmsooner’s picture

Version: 4.0.4 » 4.0.x-dev
Status: Needs review » Fixed

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.