Custom label logic with Twig or plugins (as replacement for 'Use PHP')

Created on 25 July 2019, almost 5 years ago
Updated 23 January 2024, 5 months ago

Since the 'Use PHP for auto entity labels' feature was removed in #2940098: eval() should not be a standard feature. β†’ , it is no longer possible to define custom logic for generating the label. There is no replacement, so this is a regression compared to the 2.x branch. In the same issue, several people say they would like to use inline Twig.

Proposed solution

Building on Drupal's plugin system, add a "label generator" plugin type that takes the entity and some configuration as input and returns a label. An administrator then selects one of the available plugins to generate the label. A plugin can (but does not need to) expose a configuration form (eg. the input to define the pattern). The current pattern-method will be the default plugin. An Inline Twig plugin can also be provided by the module. Developers can write their own plugins to implement custom logic (instead of the current PHP-eval method) and get access to things like dependency injection. When writing a custom label generator plugin, they can choose to start from scratch or extend the Pattern or Inline Twig plugin class.

🌱 Plan
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands marcvangend Amsterdam

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States mlncn Minneapolis, MN, USA

    Expose title and base fields in Manage Display are visible on the horizon! Without much squinting needed, even.

  • πŸ‡¦πŸ‡ΉAustria plepe

    I extended the module by twig templates. There's a switch in the configuration, where you can select between tokens or twig templates. I haven't created a patch (yet), for now it is available on Github.

    Examples, how you can use it:

    • {{ entity.field_name[0].value }}
    • {% for v in entity.field_name %}{{ v.value|upper }} {% endfor %}

    How shall we proceed? Shall this modification be merged into the module? Shall I create a separate issue with the merge request?

  • πŸ‡©πŸ‡ͺGermany diqidoq Berlin | Hamburg | New York | London | Paris

    Sorry @marcvangend #10 🌱 Custom label logic with Twig or plugins (as replacement for 'Use PHP') Active that I changed the title too hasty in that busy days. You added the PHP notice for users missing the PHP option to find this issue regarding the comment above (reverted back with hopefully more detail that it replaces the "use PHP" functionality from before).

    For those who still do not understand why this formerly "use PHP" feature had to be removed, please read the very well layed out comments of @dww regarding security here above for example in #16 and #12. Additionally: Big thanks to @dww for working on it here and elsewhere in Drupal.

    @AdamPS Regarding base fields: is https://www.drupal.org/project/manage_display β†’ still relevant and can it be used side by side with cores slidely implementation?

    Apart from the awesome concepts, ideas and thoughts have been put in here over the time to move forward (many thanks to all the input) can we elaborate on the question I tried to raise in #19 🌱 Custom label logic with Twig or plugins (as replacement for 'Use PHP') Active please? It was meant as a help for those who put a lot of efforts in here while core changes in parts of this projects scope.

    Problem/Motivation for followers: This issue is about a massive work on a new plugin or Twig implementation and it needs to be secured that there are no efforts waisted. There is a big change in Drupal core coming regarding base fields and this can change the scope or way of this project.

Production build 0.69.0 2024