WebformAttachmentUrl.php in Webform 6.x
File
modules/webform_attachment/src/Element/WebformAttachmentUrl.php
View source
<?php
namespace Drupal\webform_attachment\Element;
use Drupal\webform\WebformSubmissionInterface;
use GuzzleHttp\Exception\RequestException;
class WebformAttachmentUrl extends WebformAttachmentBase {
public function getInfo() {
return parent::getInfo() + [
'#url' => '',
];
}
public static function getFileContent(array $element, WebformSubmissionInterface $webform_submission) {
try {
$url = $element['#url'];
$token_manager = \Drupal::service('webform.token_manager');
$url = $token_manager
->replace($url, $webform_submission);
$url = file_create_url($url) ?: $url;
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;
}
public static function getFileName(array $element, WebformSubmissionInterface $webform_submission) {
if (!isset($element['#filename']) && !empty($element['#url'])) {
$token_manager = \Drupal::service('webform.token_manager');
$url = $token_manager
->replace($element['#url'], $webform_submission);
return basename($url);
}
else {
return parent::getFileName($element, $webform_submission);
}
}
}