When Substitution type "Direct URL to media file entity" is selected we should add the option to add a download attributte so the file can be downloaded.

Make sure to enable the option here: https://www.drupal.org/files/issues/linkit-doc-media-matcher-config.png

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mpp created an issue. See original summary.

mpp’s picture

Status: Active » Needs review
FileSize
3.67 KB
mpp’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 2: 2997546-2.patch, failed testing. View results

mpp’s picture

Status: Needs work » Needs review
FileSize
6.47 KB

Updated tests.

Status: Needs review » Needs work

The last submitted patch, 5: 2997546-5.patch, failed testing. View results

mpp’s picture

FileSize
8.14 KB
mpp’s picture

Status: Needs work » Needs review
Jérôme Dehorter’s picture

Hi mpp,

Your patch works on D8.6.1 / Linkit 5.0-beta7. Thanks for that.

Next step if possible, add the title to file name in download attribute instead of download.

P.S. : Don't forget in the Readme : Check the option in Linkit URL converter option

mpp’s picture

FileSize
8.78 KB

Hi Jérôme Dehorter, welcome & thanks for the feedback.

I added the documentation to the README.

Interesting remark about the download title but if we use the title, we'd need to escape the filename to a valid and secure name. It would probably be better to use the Media's source_field's url?

Jérôme Dehorter’s picture

Hi mpp,

Thanks for this new patch. I test it quickly and I give you a new feedback soon.

a-fro’s picture

FileSize
8.78 KB

The patch was succeeding but the code was throwing an error because $settings['download'] was undefined when attempting to get the #default_value. This patch fixes that issue for me, in 8.6.2.

anon’s picture

Looks good. I would like some more information about the "download" attribute but I can't find any.

Where can I read or see this?

guedressel’s picture

Please take the module media_entity_download into account. It handles download of media more robust by creating a proxy URL for files. Major benefit: Media download links become predictable and don't change if a media source file gets replaced and therefore changes the file URL. Also access checks are in the make to honor publishing workflow for media entities (as well as private files permission checks).

mpp’s picture

@guedressel, thank you for highlighting media_entity_download.
Using the proxy URL makes sense to facilitate the download and to work around any caching issues that might arise from changing urls but it's not a good SEO practice.

Before I close this issue I want to make sure that media_entity_download is using the media entity alias instead of the proxy url.

mpp’s picture

@anon,

When used on an anchor, this attribute signifies that the browser should download the resource the anchor points to rather than navigate to it.

See documentation on the download attribute.

+                $element->setAttribute('download', 'download');

Perhaps we can make the value for download attribute configurable in a follow up issue so a webmaster can configure a media field to use for the download filename?

mpp’s picture

FileSize
8.79 KB

Added the entity label instead of 'download' so the downloaded document is named as such.

ducktape’s picture

Status: Needs review » Reviewed & tested by the community

Nice, thanks for the patch @mpp. Works well on our test environment.

anon’s picture

Status: Reviewed & tested by the community » Fixed

Great work! Thanks for the patch.

  • anon committed 765b197 on 8.x-5.x authored by mpp
    Issue #2997546 by mpp, a-fro, anon: Add download attribute for Media...
joewhitsitt’s picture

Thanks for working on this. I thought I had everything correctly configured, however the download attribute seems to be populated with the file's media_field_data "name" value.

<a data-entity-substitution="media" data-entity-type="media" data-entity-uuid="fb415703-ba62-4256-aecb-61d64c8c4151" href="/media/17" download="demo txt" title="demo txt">/media/17</a>

The URI is: public://2019-06/demo.txt

We are requiring users to provide a name on upload and this doesn't always match the filename.

Is this working by design and we need to auto-populate/disable the name field with the filename for this to work?

Berdir’s picture

I'd say that's a bug. The problem is that the logic for this is outside of the media plugin, because the substitution plugin doesn't have the ability to return that information, so it is hardcoded outside.

Two ideas:

1) Add a method specifically for Media that we can call there, so at least the logic is where it belongs, would still be hardcoded.

2) Introduce an Interface SubstitutionUrlAlter or something, with a method like $substitution->alterUrl($element) (a bit strange naming..).

Then alternative implementations could do something like that, we could even have the configuration on the substitution plugin where I'd say it belongs instead of the filter format (where you might need to configure it 3x for different text formats), it would even allow to have different profiles that add the attribute and others that don't.

anon’s picture

we could even have the configuration on the substitution plugin where I'd say it belongs instead of the filter format

Yes, this sounds like the way to go..

Berdir’s picture

@anon: Do you want to revert this or create a follow-up? Might be a bit weird if we move the settings around in a follow-up?

anon’s picture

@Berdir: I can revert this later today. This fix is only in the dev branch anyway.

  • anon committed 9b54704 on 8.x-5.x
    Revert "Issue #2997546 by mpp, a-fro, anon: Add download attribute for...
anon’s picture

Status: Fixed » Needs work

Reverted now.

tarik.cipix’s picture

FileSize
978 bytes

Please add this patch to fix a notice.. it's filling up the recent log messages.

alison’s picture

Jelle_S’s picture

FileSize
0 bytes

Rerolled #17 since it worked for our use case, but I'll keep the issue on "Needs work" since it obviously still has some issues.

Jelle_S’s picture

FileSize
8.89 KB

If only I had made the patch correctly... sorry for the spam

Fernly’s picture

Status: Needs work » Reviewed & tested by the community

I am under the impression that the latest patch #31 to fix the notice is already included in 6.x. Setting this issue to RTBC. Perhaps others can confirm this before closing this issue.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 31: 2997546_31.patch, failed testing. View results

Fernly’s picture

Assigned: mpp » Unassigned

I would recommend the maintainer to close this issue if confirmed that the fix is included in 6.x. The failing patch does not need to be merged anymore.