I have a custom entity (contract) created in a custom module (amap) by a bunch of BaseFieldDefinition, one of which being
$fields['file1'] = BaseFieldDefinition::create('file')
->setLabel(t('Source file'))
->setDescription(t('Source file (may be modified)'))
->setSettings([
'target_type' => 'file',
'file_extensions' => 'doc docx odt pages',
'uri_scheme' => 'private',
'file_directory' => 'contracts',
])
I have a View about the custom entity that displays some fields in table mode:
The download action (the yellow arrow) is defined using Custom text with rewrite results
, link path
being {{ file1__target_id }}
:
The link is something like mysite/system/files?file=contracts/Contract-4-20200408.doc
(for example).
And following the link gives different results depending on the browser. None of them being what I want: the default filename proposed is not correct 'files' or 'file.dot' instead of 'Contract-4-20200408.doc' (in my example).
So I tried to implement hook_file_download. My code is
function amap_file_download($uri) {
$sFileName = \Drupal::service('file_system')->basename($uri);
$sFileNameWithPath = 'sites/default/files/_private/contracts/' . $sFileName;
$response = new BinaryFileResponse($sFileNameWithPath);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $sFileName);
return $response;
}
and there I get this error!
Notice: Object of class Symfony\Component\HttpFoundation\BinaryFileResponse could not be converted to int in Drupal\system\FileDownloadController->download() (line 51 of core/modules/system/src/FileDownloadController.php).
Drupal\system\FileDownloadController->download(Object, 'private')
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
What am I doing wrong?