You are here

public static function WebformAttachmentUrl::getFileContent in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_attachment/src/Element/WebformAttachmentUrl.php \Drupal\webform_attachment\Element\WebformAttachmentUrl::getFileContent()

Get a webform attachment's file content.

Parameters

array $element: The webform attachment element.

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

Return value

mixed|string The attachment's file content.

Overrides WebformAttachmentInterface::getFileContent

File

modules/webform_attachment/src/Element/WebformAttachmentUrl.php, line 27

Class

WebformAttachmentUrl
Provides a 'webform_attachment_url' element.

Namespace

Drupal\webform_attachment\Element

Code

public static function getFileContent(array $element, WebformSubmissionInterface $webform_submission) {
  try {
    $url = $element['#url'];

    // URL can contain tokens.

    /** @var \Drupal\webform\WebformTokenManagerInterface $token_manager */
    $token_manager = \Drupal::service('webform.token_manager');
    $url = $token_manager
      ->replace($url, $webform_submission);

    // Url can be a URI.
    $url = file_create_url($url) ?: $url;

    // Prepend scheme and host to root relative path.
    if (strpos($url, '/') === 0) {
      $url = \Drupal::request()
        ->getSchemeAndHttpHost() . $url;
    }
    $content = (string) \Drupal::httpClient()
      ->get($url)
      ->getBody();
  } catch (RequestException $exception) {
    $content = '';
  }
  return !empty($element['#trim']) ? trim($content) : $content;
}