You are here

protected static function TokenResolver::parseImageWebformElementTokens in FillPDF 8.4

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

Scans a potential webform image element token.

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

Parameters

string[] $tokens: List of non-fully qualified webform_submission tokens. These may be image element tokens such as 'values:image' or other tokens.

\Drupal\Core\Entity\ContentEntityInterface $entity: Webform submission entity.

Return value

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

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

File

src/TokenResolver.php, line 156

Class

TokenResolver
Class TokenResolver.

Namespace

Drupal\fillpdf

Code

protected static function parseImageWebformElementTokens(array $tokens, ContentEntityInterface $entity) {

  // Get all non-empty elements.

  /** @var \Drupal\webform\WebformSubmissionInterface $entity */
  $elements = $entity
    ->getWebform()
    ->getElementsInitializedFlattenedAndHasValue();

  // Loop through the tokens, starting with the last one.
  foreach (array_reverse($tokens) as $token) {
    $name = strtr($token, [
      'values:' => '',
    ]);
    if (!array_key_exists($name, $elements) || !isset($elements[$name]['#type'])) {
      continue;
    }
    if ($elements[$name]['#type'] === 'webform_image_file') {
      $file = File::load($entity
        ->getElementData($name));
      if ($file) {
        $uri = $file
          ->getFileUri();
        return new ImageFieldMapping(file_get_contents($uri), NULL, $uri);
      }
    }
    elseif ($elements[$name]['#type'] === 'webform_signature') {
      $signature_data = $entity
        ->getElementData($name);
      $signature_image = static::getSignatureImage($signature_data);
      if (!$signature_image) {
        continue;
      }
      return new ImageFieldMapping($signature_image, NULL, 'webform_signature.png');
    }
  }
  return NULL;
}