Problem/Motivation

We need to be able to alter FieldGroup labels on render (in Displays) on a per-fieldgroup usage basis.

Use case

We use FieldGroups in Paragraphs to provide is with Teacher-only hints and tips within out educational site and we do this via Paragraphs that are only visible to teachers and not to students.

Sometimes these Paragraphs are 'Hints', other time they are 'Recommended homework' etc. so we need to alter the labels accordingly. We have added a field (label_replacement) for this purpose.

It feel to us that if the label could use token-replacement then that would sort everything very nicely because then we could simply reconfigure the FieldGroup to use the token of the associated field.

field_group_dynamic_label_requirement

However, if that is not possible to provide easily as a new feature or it is not considered to be a good idea to try to use tokens for this then could anyone please guide us as the custom code required (a hook_alter of some kind) to help in the meantime as we can't seem to identify the right one to use.

Many thanks

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

sirclickalot created an issue. See original summary.

super_romeo made their first commit to this issue’s fork.

super_romeo’s picture

Version: 8.x-3.6 » 8.x-3.x-dev
Status: Active » Needs review
sirclickalot’s picture

@super_romeo,

Please forgive my ignorance but this mean that the proposed solution will be in 4.x too if and when it passes muster?

I am now on 4.x anyway as we noted that there will be no further commits to 8.x.

Happy to test it out but how should I best go about that given that we have moved on to 4.x ?

Thank you

super_romeo’s picture

@sirclickalot I'd like to upgrade to version 4 too, but the simple_multistep model uses version 3 (https://www.drupal.org/project/simple_multistep/issues/3507839). So I am forced to use version 3.

super_romeo’s picture

Version: 8.x-3.x-dev » 4.x-dev
Component: Miscellaneous » Code
Category: Support request » Feature request
sirclickalot’s picture

Hi @super_romeo,

I can confirm that applying the patch from commit: 78295998 work very nicely indeed, I am now able to use a token instead of having to use the additional module Field Group Label.

sirclickalot’s picture

Hi,

Any mileage in getting this in place on a release so that we can remove the reliance on the Field Group Label module.

The patch in https://git.drupalcode.org/project/field_group/-/merge_requests/115#note... works seamlessly as far as I can see.

Thanks

csakiistvan’s picture

Assigned: Unassigned » csakiistvan
csakiistvan’s picture

Environment

  • Drupal: 11.4.1
  • PHP: 8.5.5
  • Database: MariaDB 10.11.16
  • DDEV: v1.25.2
  • Field Group: 4.x-dev
  • Token: 1.x
  • Browser: Chrome

Prerequisites

  • The Field Group and Token modules enabled.
  • A content type (e.g. Article) whose default view display has a Details field group wrapping a visible field (e.g. the body), with a label containing a token, for example Title token: [node:title].
  • Layout Builder must be disabled for that view display, otherwise Layout Builder takes over rendering and the field group is not processed.
  • A published node of that type to view.

Steps

  1. Apply the fix from MR !115: add a Replace tokens in label (use_tokens) setting to the Details formatter and replace tokens in the group label at render time when it is enabled and the Token module is available.
  2. Rebuild caches: ddev drush cr
  3. On the content type's Manage display, open the Details group settings, enable Replace tokens in label and save.
  4. View the node and observe the field group title.

Expected results

  • With Replace tokens in label enabled, the token in the group label is replaced with the entity value (e.g. the node title).
  • The setting only appears when the Token module is enabled, and defaults to off (existing groups render unchanged).

Actual results

Before the fix there is no way to use tokens in a field group label: the label renders literally, e.g. the Details group title showed Title token: [node:title]. After the fix, with the new Replace tokens in label setting enabled, the same group renders its title as Title token: Tabs repro node (the [node:title] token replaced with the node title). The setting is only offered when the Token module is present and is off by default, so existing displays are unaffected.


Testing produced with the assistance of an LLM.

csakiistvan’s picture

StatusFileSize
new149.36 KB
new171.08 KB
csakiistvan’s picture

Assigned: csakiistvan » Unassigned
Status: Needs review » Reviewed & tested by the community