Problem/Motivation
It's a great contrib module. Thanks @clivesj
We have an external host (internet) and an admin host (intranet) linking to a single Drupal site.
The client requests a special role to upload the files into the particular public folder in the admin site through FileBrowser module.
After the files are uploaded into the Drupal site, the client requires to copy the file URL as an external host URL.
the current url ("/filebrowser/download/nodeid") is very difficult for the non-programmer user to construct the file's external url.
Please see the screenshots attached


Steps to reproduce
Proposed resolution
To meet this custom requirement, we can modify the logic of the makeLink function in the DisplayFileList.php
if ($file->fileData->type != 'file') {
return Link::createFromRoute($name, 'entity.node.canonical', ['node' => $this->node->id()], $options);
}
else {
- return Link::createFromRoute($name, 'filebrowser.page_download',['fid' => $fid]);
+ $externalhost = Settings::get('external_host');
+ $directoryuri = $this->filebrowser->folderPath;
+ if (isset($externalhost) && (strpos ($directoryuri, 'public://') !== false)) {
+ //create an external link if a public file link exists in local setting
+ $publicfolderpath = Settings::get('file_public_path');
+ if (!isset($publicfolderpath)) {
+ $publicfolderpath = PublicStream::basePath();
+ }
+ $publicfolderpath = '/' . $publicfolderpath. '/';
+ $publicfolderpath = str_replace('public://', $publicfolderpath, $directoryuri);
+ $fexternal_url = $externalhost . $publicfolderpath . $file->relativePath;
+ $fexternal_link = Link::fromTextAndUrl($name, Url::fromUri($fexternal_url));
+ if (isset($fexternal_link)) {
+ return $fexternal_link;
+ }
+ else {
+ return Link::createFromRoute($name, 'filebrowser.page_download',['fid' => $fid]);
+ }
+ }
+ else {
+ return Link::createFromRoute($name, 'filebrowser.page_download',['fid' => $fid]);
+ }
}
}
Remaining tasks
User interface changes
Added additional download manager and new configuration field `external_host`to main confiuration an to node's widget
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | 3254557-21.patch | 21.56 KB | clivesj |
| #18 | 3227724-15-18.interdiff.txt | 19.39 KB | mmbk |
| #18 | 3227724-18.patch | 22.33 KB | mmbk |
| #16 | Bildschirmfoto vom 2022-03-04 08-45-57.png | 30.62 KB | mmbk |
| #15 | Display-external-URLs-3254557-15.patch | 5.88 KB | jamesyao |
Comments
Comment #2
jamesyao commentedComment #3
jamesyao commentedComment #4
jamesyao commentedI moved the externalhost from settings.php (settings.local.php) to the configuration UI of the FileBrowser, which allows admin to dynamically set the external host in Dev, QA, UAT, and PROD environments.
Comment #5
clivesj commentedThanks.
This is a nice enhancement. Any chance you can provide a patch for the 3.x branch?
There not much diff with 8.x but 8.x is strictly for D8 and 3.x only for D9.
Comment #6
jamesyao commentedThanks @clivesj.
The patch (Display-external-URLs-3254557-04.patch) was tested against Drupal Core D9.3.x in my project. It should work for both D8 and D9.
Comment #7
jamesyao commentedComment #8
jamesyao commentedComment #9
jamesyao commentedI fixed the a bug in the previous patch to check if the external host is not empty.
Comment #10
clivesj commentedI am a bit busy and cannot produce a patch.
Can you please look at the following:
1. Filebrowser.settings.yml format for the new setting:
adhoc_settings:
external_host: ' '
2. We also need HOOK_N in the install file to update the module configuration for adhoc_settings.
3. Please correct typo exnternal.
4. On the config form we need a better description than “leave open, it is for a special feature”. It should be descriptive for other users.
5. We need description documenting what the new feature does to include in the readme.
Thanks for your time.
Comment #11
jamesyao commentedThanks @clivesj for your time and feedback. Much appreciated!
I updated the patch and will attach the readme of the patch later.
Comment #12
jamesyao commentedComment #13
joseph.olstad@jamesyao,
please replace deprecated db_add_field as follows:
https://api.drupal.org/api/drupal/core%21includes%21database.inc/functio...
also, please replace filebrowser_update_7100 with filebrowser_update_9001 as 3.0.x is D9+
and then change
to
also, please recall that the php >=5.4 array syntax must be used
so rather than
please use the expected Drupal 9 standard array style introduced in php 5.4:
Comment #14
jamesyao commentedThanks @joseph.olstad for your feedback.
Comment #15
jamesyao commentedFixed a syntax error (Class 'Database' not found) in Display-external-URLs-3254557-14.patch.
Comment #16
mmbkThe patch is working as described, and solves 200% of my customer's request.
I tend to split this patch into two task

1. display the file's urls as absolute Urls into the filessystem so that the webserver can deliver it directly (This is what my customer wants) This has the effect that the urls will remain persistent if a file is replaced, so the files can be referred externally. This could be archived by adding a new `Download manager`
2. Replace the host-part of the url with the configured `external_host`,
Some reviews on the patch:
Hmm, this is uneccessary code, the variable is not defined and the message is not helpful, I guess it's some generated code
This part should use the Streamwrappers, so that the generation is not restricted to the public wrapper.
Comment #17
mmbkAs outlined in #16 I expanded the download_manager (I'm not totally convinced, that the naming is still valid, but it's good enough) with a `server` manager. Whenever this is selected a the link points to a file not to the file-entity.
When an `external host` is configured the `scheme&host` part of the url is replaced with the configured value.
The url is generated with the help of the used streamwrapper, this allows to use custom `flysystems` wrappers.
The previous patch got the `external host`from filebrowser's configuration, not from the displayed node, I changed this, it seems to make more sense.
The file `DisplayFileList.php` is reformatted with `phpcbf`there are still code-style-issues that have to be fixed manually.
Comment #18
mmbkArghh path #17 is an interdiff, don't use it, correct path and the interdiff attached here.
Comment #19
onfire84 commentedpacht #18 does not apply with latest dev Version
Comment #20
clivesj commentedComment #22
clivesj commentedThe patch is against latest 3.1.x-dev
I have included code to update current configuration for the new setting.
dev is updated and if commit holds, I can make a new release 3.1.0 which has this new feature and is also D10 compatible.