Problem/Motivation

I'm using Supported Image Field which extends core's Image field type and would love to use image style tokens with supported image fields. Currently that's not possible because Token module defines those tokens only for core's Image field type.

Note that I did verify that this check $field->getType() == 'image' is the only reason for image style tokens not to work with supported image fields. As a quick experiment, I updated the check to include supported_image field type and image tokens worked just fine.

Steps to reproduce

  1. Install Supported Image Field module.
  2. Create and configure field of a type "Supported Image". E.g. field_supported_image.
  3. Define token that is using supported image field and image style. E.g. [node:field_supported_image:large] where "large" is the name of an image style.
  4. Confirm that the output of this token is empty.

Proposed resolution

In addition to checking if the field type is image, also check if the field has capability to support image style tokens. Below are some ideas how to do that.

/**
 * Implementation of hook_tokens() in Token module.
 */

// Current code: Check only for the field type.
if ($field_item->getFieldDefinition()->getType() == 'image') {}

// Option 1: Implicitly assume if the field extends ImageItem it must be
// compatible. No need for other field types to declare anything.
if (($field_item instanceof \Drupal\image\Plugin\Field\FieldType\ImageItem)) {}

// Option 2: Define empty (marker) interface for this purpose and check if the
// field type implements it.
if ($field_item->getFieldDefinition()->getType() == 'image' || $field_item instanceof ImageFieldCompatible) {}

// Option 3: Check for the boolean attribute defined on the field type.
if ($field_item->getFieldDefinition()->getType() == 'image' || !empty($field_item->getPluginDefinition()['add_image_style_tokens'])) {}

Note: Options 2 and 3 still contain the check for image field type in order to avoid having to modify core's image module.

I like the simplicity of Option 1 but I'm not sure if assumption its making is safe enough.

Making this change would enable any other compatible field type to take advantage of image style tokens. I didn't check if there are any other modules currently that could benefit from this change in addition to Supported Image Field.

Remaining tasks

Decide which approach to take and implement the solution.

User interface changes

None.

API changes

TBD

Data model changes

None.

Issue fork token-3595271

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

vaish created an issue. See original summary.

vaish’s picture

Status: Active » Needs review

I created merge request implementing Option 1 from the issue description. Instead of checking for the field type name, this MR checks if the field inherits from the Image field type. Image style tokens now work with the Image field type and any other field type that extends it.

csakiistvan’s picture

Assigned: Unassigned » csakiistvan
csakiistvan’s picture

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

Environment

  • Drupal: 11.4.4
  • PHP: 8.5.5
  • Database: MariaDB 10.11.16
  • DDEV: v1.25.2
  • Token: 8.x-1.17

Prerequisites

  • Create a field type that extends core's Image field type (Drupal\image\Plugin\Field\FieldType\ImageItem) without literally being named image, e.g. via the Supported Image Field module.
  • Attach this field to a content type and populate it with an image on a node.

Steps

  1. Apply the fix from MR !131: check whether the field type extends the Image field type instead of comparing the field type name to the literal string image, both when listing image style tokens and when resolving them.
  2. Rebuild caches: ddev drush cr
  3. Check the token tree (or generate the token programmatically) for a field using the custom image-derived field type, using an image style token, e.g. [node:field_name:large].

Expected results

  • Image style tokens (e.g. large, medium, thumbnail) are listed for any field type that extends the Image field type, not only for fields whose type is literally image.
  • Resolving such a token returns the image style derivative URL.

Actual results

Before the fix, a field using a custom field type that extends the Image field type did not list any image style tokens at all, and resolving [node:field_name:large] manually returned an empty value, since the code only matched fields whose type name was exactly image. After applying the fix, the image style tokens were listed for the field and [node:field_name:large] resolved correctly to the image style derivative URL.


Testing produced with the assistance of an LLM.