You are here

protected function FileDownloadLink::addDeltaToTokens in File Download Link 8

A helper function to handle delta in tokens.

Parameters

string $string: The string that might have tokens.

int $delta: The delta to add to certain tokens.

string $entity_token_type: Entity token type, like node or media.

string $field: Field name of this field being rendered.

Return value

string The string with delta value added to certain tokens.

1 call to FileDownloadLink::addDeltaToTokens()
FileDownloadLink::viewElements in src/Plugin/Field/FieldFormatter/FileDownloadLink.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/FileDownloadLink.php, line 420

Class

FileDownloadLink
Plugin implementation of the 'file_download_link' formatter.

Namespace

Drupal\file_download_link\Plugin\Field\FieldFormatter

Code

protected function addDeltaToTokens($string, $delta, $entity_token_type, $field) {

  // We do two str_replace calls to save us from confusing regex.
  // First add delta to middle of a "chain".
  $string = str_replace("[{$entity_token_type}:{$field}:", "[{$entity_token_type}:{$field}:{$delta}:", $string);

  // Then add delta if token ends at this field.
  $string = str_replace("[{$entity_token_type}:{$field}]", "[{$entity_token_type}:{$field}:{$delta}]", $string);
  return $string;
}