You are here

public static function WebformAttachmentBase::getFileName in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_attachment/src/Element/WebformAttachmentBase.php \Drupal\webform_attachment\Element\WebformAttachmentBase::getFileName()

Get a webform attachment's file name.

Parameters

array $element: The webform attachment element.

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

Return value

mixed|string The attachment's file name.

Overrides WebformAttachmentInterface::getFileName

5 calls to WebformAttachmentBase::getFileName()
WebformAttachmentBase::getFileLink in modules/webform_attachment/src/Element/WebformAttachmentBase.php
Get a webform attachment's file link.
WebformAttachmentBase::getFileMimeType in modules/webform_attachment/src/Element/WebformAttachmentBase.php
Get a webform attachment's file type.
WebformAttachmentBase::getFileUrl in modules/webform_attachment/src/Element/WebformAttachmentBase.php
Get a webform attachment's download URL.
WebformAttachmentUrl::getFileName in modules/webform_attachment/src/Element/WebformAttachmentUrl.php
Get a webform attachment's file name.
WebformEntityPrintAttachment::getFileName in modules/webform_entity_print_attachment/src/Element/WebformEntityPrintAttachment.php
Get a webform attachment's file name.
2 methods override WebformAttachmentBase::getFileName()
WebformAttachmentUrl::getFileName in modules/webform_attachment/src/Element/WebformAttachmentUrl.php
Get a webform attachment's file name.
WebformEntityPrintAttachment::getFileName in modules/webform_entity_print_attachment/src/Element/WebformEntityPrintAttachment.php
Get a webform attachment's file name.

File

modules/webform_attachment/src/Element/WebformAttachmentBase.php, line 69

Class

WebformAttachmentBase
Provides a base class for 'webform_attachment' elements.

Namespace

Drupal\webform_attachment\Element

Code

public static function getFileName(array $element, WebformSubmissionInterface $webform_submission) {
  if (!empty($element['#filename'])) {

    /** @var \Drupal\webform\WebformTokenManagerInterface $token_manager */
    $token_manager = \Drupal::service('webform.token_manager');
    $filename = $token_manager
      ->replace($element['#filename'], $webform_submission);

    // Remove forward slashes from filename to prevent the below error.
    //
    //   Parameter "filename" for route
    //   "entity.webform.user.submission.attachment" must match "[^/]++".
    $filename = str_replace('/', '', $filename);

    // Sanitize filename.
    // @see http://stackoverflow.com/questions/2021624/string-sanitizer-for-filename
    // @see \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::getFileDestinationUri
    if (!empty($element['#sanitize'])) {

      /** @var \Drupal\Component\Transliteration\TransliterationInterface $transliteration */
      $transliteration = \Drupal::service('transliteration');

      /** @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */
      $language_manager = \Drupal::service('language_manager');
      $langcode = $language_manager
        ->getCurrentLanguage()
        ->getId();
      $extension = pathinfo($filename, PATHINFO_EXTENSION);
      $basename = substr(pathinfo($filename, PATHINFO_BASENAME), 0, -strlen(".{$extension}"));
      $basename = mb_strtolower($basename);
      $basename = $transliteration
        ->transliterate($basename, $langcode, '-');
      $basename = preg_replace('([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\].]|[\\.]{2,})', '', $basename);
      $basename = preg_replace('/\\s+/', '-', $basename);
      $basename = trim($basename, '-');
      $filename = $basename . '.' . $extension;
    }
    return $filename;
  }
  else {
    return $element['#webform_key'] . '.txt';
  }
}