You are here

protected function WebformManagedFileBase::formatTextItem in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformManagedFileBase.php \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::formatTextItem()

Format an element's value as text.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

array $options: An array of options.

Return value

string The element's value formatted as text.

Overrides WebformElementBase::formatTextItem

See also

_webform_token_get_submission_value()

1 call to WebformManagedFileBase::formatTextItem()
WebformManagedFileBase::formatHtmlItem in src/Plugin/WebformElement/WebformManagedFileBase.php
Format an element's value as HTML.

File

src/Plugin/WebformElement/WebformManagedFileBase.php, line 365

Class

WebformManagedFileBase
Provides a base class webform 'managed_file' elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $value = $this
    ->getValue($element, $webform_submission, $options);
  $file = $this
    ->getFile($element, $value, $options);
  if (empty($file)) {
    return '';
  }
  $format = $this
    ->getItemFormat($element);
  switch ($format) {
    case 'data':
      return base64_encode(file_get_contents($file
        ->getFileUri()));
    case 'id':
      return $file
        ->id();
    case 'mime':
      return $file
        ->getMimeType();
    case 'name':
      return $file
        ->getFilename();
    case 'basename':
      $filename = $file
        ->getFilename();
      $extension = pathinfo($filename, PATHINFO_EXTENSION);
      return substr(pathinfo($filename, PATHINFO_BASENAME), 0, -strlen(".{$extension}"));
    case 'size':
      return $file
        ->getSize();
    case 'extension':
      return pathinfo($file
        ->getFileUri(), PATHINFO_EXTENSION);
    case 'url':
    case 'value':
    case 'raw':
    default:
      return file_create_url($file
        ->getFileUri());
  }
}