Problem/Motivation

The [file:delta] token, added in Field Tokens 2.0.x (#2386655: File field delta token), returns the zero-based position of a file within a multi-value field. This is useful for producing unique filenames and preventing collisions on multi-value file fields:

[node:title]-[file:delta].[file:ffp-extension-original]

However, File (Field) Paths iterates over files using $field->referencedEntities() in FileFieldPathsProcessFileLegacy, which yields only the File entity and discards position information. The delta key is never present in the token data, so [file:delta] always resolves to an empty string.

Proposed resolution

Replace the referencedEntities() loop with a loop over the field items directly, giving access to each item's delta index, and pass it through in the token data:

foreach ($field as $delta => $field_item) {
    $file = $field_item->entity;
    if (!$file instanceof \Drupal\file\Entity\File) {
        continue;
    }
    $token_data = [
        'file'                      => $file,
        $entity->getEntityTypeId() => $entity,
        'delta'                     => $delta,
    ];

No changes to public API, configuration schema, or other modules.

Remaining tasks

  • Implement the loop refactor in FileFieldPathsProcessFileLegacy.
  • Add a test asserting a two-file node with the pattern above produces filenames containing -0. and -1..

Related issues

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

deciphered created an issue. See original summary.

deciphered’s picture

Status: Active » Needs review