Allow conditional logic in automatic entity labels

Created on 30 March 2021, over 3 years ago
Updated 17 May 2024, 6 months ago

Problem/Motivation

We have a content type whose title should include a summary of its address. It works great with one exception— the first and second line of the address run together if both present Allow conditional logic in automatic entity labels Active , but adding a comma or other separator looks bad when one of the lines is not present.

[node:field_findit_location_name]: [node:field_findit_address:address_line1][node:field_findit_address:address_line2], [node:field_findit_address:locality], [node:field_findit_address:administrative_area] [node:field_findit_address:postal_code]

Steps to reproduce

Build a label out of optional fields, and see how it looks when certain fields are not present.

Proposed resolution

It would be wonderful to be able to use twig in the pattern for the label or otherwise use very simple conditional logic— if this token isn't empty then print this, otherwise print nothing (or something else).

[node:field_findit_location_name]: {if [node:field_findit_address:address_line1]}[node:field_findit_address:address_line1], {endif}[node:field_findit_address:address_line2], [node:field_findit_address:locality], [node:field_findit_address:administrative_area] [node:field_findit_address:postal_code]

Remaining tasks

Has this been considered? Are there reasons to reject this feature?

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

3.0

Component

Code

Created by

🇺🇸United States mlncn Minneapolis, MN, USA

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.

  • 🇦🇹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 %}
  • 🇫🇷France GuillaumePacilly

    I had the same need and solved it by using custom tokens. Then you can apply conditions inside your hook_tokens().

    /**
     * Implements hook_token_info().
     */
    function mymodule_token_info() : array {
      $info = [];
    
      $info['types']['my_custom_tokens'] = [
        'name' => t('Custom Tokens'),
        'description' => t('Tokens available for specific use on this website'),
      ];
    
      $info['tokens']['my_custom_tokens']['custom_token'] = [
        'name' => 'Custom token',
        'description' => t('Custom token with conditions'),
      ];
    
      return $info;
    }
    
    /**
     * Implements hook_tokens().
     */
    function mymodule_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) : array {
      $replacements = [];
    
      if ($type == 'my_custom_tokens') {
        foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'custom_token':
                $node = $data['node'];
                // Add your logic here.
    
                $replacements[$original] = $title;
              }
    
              break;
    
            default:
              break;
          }
        }
      }
    
      return $replacements;
    }
    
    

    Then just use your custom token in the auto_entitylabel configuration

Production build 0.71.5 2024