How to get delta of a file from within hook_tokens I am trying to create a delta token in my module.
In  hook_tokens, I manage to get the file object, but I have no clue on how to get the delta of my file...

Here is my implementation of hook_token_info

function artprod_token_info() {
  $types = array();
  $tokens = array();

  $types['delta'] = array(
    'name' =&gt; t('Delta'),
    'description' =&gt; t('Provide the delta of the field.')
  );
  $tokens['delta']['delta'] = array(
    'name' =&gt; t('Delta'),
    'description' =&gt; t('Provide the delta of the field.')
  );

  return array(
    'types' =&gt; $types, 
    'tokens' =&gt; $tokens
  );
}


Here is the beginning of my implementation of hook_tokens

function artprod_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  if ($type == 'delta') {
    $file=$data['file'];
    foreach ($tokens as $name =&gt; $original) {
      // Find the desired token by name
      switch ($name) {
        case 'delta':
          $replacements[$original] = $file-&gt;delta();// NOT WORKING
          break;
      }
    }
  }
  return $replacements;
}
It's the relationship between a file entity and another entity via a reference field that has a delta, not the file entity itself. A single file could, theoretically, be referenced by many entities, via multiple separate fields, or even twice in the same entity - meaning it doesn't conceptually have a single delta related to it.

What you'll need is to run an EntityQuery with sufficient contextual conditions to narrow down to one result, load up that parent entity, and loop through the field values until you find your file, giving you the delta. 

If your use case enforces that a file can only be referenced by a single entity, it'll be a lot easier to implement. But it would probably be safer to include at least some kind of limiting.