1

Before the Update to the latest DruPal 8, we were able to link to public://foo/bar.pdf to a file which was in /sites/default/files/. No the public:// prefix does not work anymore in the URL input field.

We are still able to enter the full link, but this might have the downside that it needs to be updated when the file name changes.

error

We also tried /media/1337 but it shows a new page with the download link, while we need a direct download.

Can this still be accomplished?

1
  • If this worked before, than it had exactly the same downside, a filename change will always brake this. You need Media Entity Download for stable links to media entities.
    – Hudri
    Commented Jun 1, 2022 at 14:16

1 Answer 1

2

I think that the issue is that public:// is a PHP file stream wrapper defined by Drupal, which is a PHP language feature, and is not a URL, which is a publicly defined standard. The field is specifically requesting a URL.

The url /sites/default/files/<file name> should wok. Relative URLs are considered best practice.

The issue of changing file names is separate from the topic of file stream wrappers and URLs. A common method for mitigating this issue is to use the Pathauto module.

5
  • We tried to use relative links, but a base: prefix is prepend
    – Alex
    Commented Jun 2, 2022 at 5:26
  • The base: prefix is part of Drupal's URI processing scheme. From api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/…: "For paths that are known not to be handled by the Drupal routing system (such as static files), use base: for the scheme to get a link relative to the Drupal base path."
    – Beau
    Commented Jun 3, 2022 at 15:37
  • Ah, so we could maybe use base: .. because we used just a "/" and base was inserted automagically.
    – Alex
    Commented Jun 4, 2022 at 9:18
  • No it is not working. If I enter base://sites/default/files/myfile.pdf it results in <a href="base://sites/default/files/myfile.pdf">test</a> and cannot be opened in the browser.
    – Alex
    Commented Jun 4, 2022 at 9:22
  • 1
    As mentioned before, base: is used as part of Drupal's internal URI scheme. A base: URI is intended to be passed as the first argument to \Drupal\Core\Url::fromUri() in order to return a URL. Adding more detail to your question about how you're retrieving and using the field data would help myself and others offer better solutions.
    – Beau
    Commented Jun 5, 2022 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.