public function WebformAttachmentController::download in Webform 8.5
Same name and namespace in other branches
- 6.x modules/webform_attachment/src/Controller/WebformAttachmentController.php \Drupal\webform_attachment\Controller\WebformAttachmentController::download()
Response callback to download an attachment.
Parameters
\Drupal\webform\WebformInterface $webform: A webform.
\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.
string $element: The attachment element webform key.
string $filename: The attachment filename.
Return value
\Symfony\Component\HttpFoundation\Response A response containing the attachment's file.
1 string reference to 'WebformAttachmentController::download'
- webform_attachment.routing.yml in modules/
webform_attachment/ webform_attachment.routing.yml - modules/webform_attachment/webform_attachment.routing.yml
File
- modules/
webform_attachment/ src/ Controller/ WebformAttachmentController.php, line 75
Class
- WebformAttachmentController
- Defines a controller to return a webform attachment.
Namespace
Drupal\webform_attachment\ControllerCode
public function download(WebformInterface $webform, WebformSubmissionInterface $webform_submission, $element, $filename) {
// Make sure the webform id and submission webform id match.
if ($webform
->id() !== $webform_submission
->getWebform()
->id()) {
throw new NotFoundHttpException();
}
// Get the webform element and plugin.
$element = $webform_submission
->getWebform()
->getElement($element) ?: [];
$element_plugin = $this->elementManager
->getElementInstance($element, $webform_submission);
// Make sure the element is a webform attachment.
if (!$element_plugin instanceof WebformAttachmentBase) {
throw new NotFoundHttpException();
}
// Make sure element #access is not FALSE.
// The #private property is used to to set #access to FALSE.
// @see \Drupal\webform\Entity\Webform::initElementsRecursive
if (!Element::isVisibleElement($element)) {
throw new AccessDeniedHttpException();
}
// Make sure the current user can view the element.
if (!$element_plugin
->checkAccessRules('view', $element)) {
throw new AccessDeniedHttpException();
}
/** @var \Drupal\webform_attachment\Element\WebformAttachmentInterface $element_info */
// Get base form element for webform element derivatives.
// @see \Drupal\webform_entity_print\Plugin\Derivative\WebformEntityPrintWebformElementDeriver
list($type) = explode(':', $element['#type']);
$element_info = $this->elementInfo
->createInstance($type);
// Get attachment information.
$attachment_name = $element_info::getFileName($element, $webform_submission);
$attachment_mime = $element_info::getFileMimeType($element, $webform_submission);
$attachment_content = $element_info::getFileContent($element, $webform_submission);
$attachment_size = strlen($attachment_content);
$attachment_download = !empty($element['#download']) ? 'attachment;' : '';
// Make sure the attachment can be downloaded.
if (empty($attachment_name) || empty($attachment_content) || empty($attachment_mime)) {
throw new NotFoundHttpException();
}
// Return the file.
$headers = [
'Content-Length' => $attachment_size,
'Content-Type' => $attachment_mime,
'Content-Disposition' => $attachment_download . 'filename="' . $filename . '"',
];
return new Response($attachment_content, 200, $headers);
}