Improve DX of retrieving the label of a list item field

Created on 14 May 2024, 6 months ago
Updated 17 July 2024, 4 months ago

Problem/Motivation

If I want to mingle with a list item field in my twig template, I need to write something like

{{ "(" ~ node.field_state.getSetting('allowed_values')[node.field_state.value] ~")" }}

If I want the same in a preprocess, similarly I have to use:

$node->field_state->getSetting('allowed_values')[$node->field_state->value];

Or even more verbose to be technically correct:

      $provider = $node->field_state->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getOptionsProvider('value', $node);
      // Flatten the possible options, to support opt groups.
      $options = OptGroup::flattenOptions($provider->getPossibleOptions());
      $label = $options[$node->field_state->value];

This is too much implementation detail being exposed for such a simple usecase.

Proposed resolution

Allow to use {{ "(" ~ node.field_state.valueLabel ~")" }} as you would use {{ "(" ~ node.field_state.value ~")" }}.

Remaining tasks

Discuss, tests.

User interface changes

None.

API changes

New method label() on ListItemBase.

Data model changes

None.

Release notes snippet

TBD

✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
OptionsΒ  β†’

Last updated 9 days ago

No maintainer
Created by

πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024