You are here

protected static function TokenResolver::parseImageFieldTokens in FillPDF 8.4

Same name and namespace in other branches
  1. 5.0.x src/TokenResolver.php \Drupal\fillpdf\TokenResolver::parseImageFieldTokens()

Scans a potential image field token.

This is only called if image module is installed and the backend supports image stamping.

Parameters

string[] $tokens: List of non-fully qualified tokens for a given entity type. These may be image field tokens such as 'field_image' or 'field_image:thumbnail' or other tokens such as 'field_name'.

\Drupal\Core\Entity\FieldableEntityInterface $entity: Fieldable entity.

Return value

\Drupal\fillpdf\FieldMapping\ImageFieldMapping|null An ImageFieldMapping, or NULL if the tokens were no image field tokens.

Throws

\Drupal\Core\TypedData\Exception\MissingDataException

1 call to TokenResolver::parseImageFieldTokens()
TokenResolver::replace in src/TokenResolver.php
Replaces all tokens in a given string with appropriate values.

File

src/TokenResolver.php, line 205

Class

TokenResolver
Class TokenResolver.

Namespace

Drupal\fillpdf

Code

protected static function parseImageFieldTokens(array $tokens, FieldableEntityInterface $entity) {

  // Loop through the tokens, starting with the last one.
  foreach (array_reverse($tokens) as $token) {

    // Explode token into its field_name and property parts.
    list($field_name, $property) = array_pad(explode(':', $token), 2, '');
    if (!$entity
      ->hasField($field_name)) {
      continue;
    }
    $item = $entity
      ->get($field_name)
      ->first();
    if (!empty($item) && $item instanceof ImageItem) {
      $value = $item
        ->getValue();
      $file = File::load($value['target_id']);
      if ($file) {
        $uri = $file
          ->getFileUri();
        return new ImageFieldMapping(file_get_contents($uri), NULL, $uri);
      }
    }
  }
}