Template file_link is difficult to alter

Created on 17 May 2016, about 9 years ago
Updated 3 February 2023, over 2 years ago

Problem/Motivation

In order to add target="_blank" to a file link, I had to replicate most of template_preprocess_file_link. The documentation for $variables['attributes'] ("An associative array of attributes to be placed in the a tag") is wrong after 📌 Use CSS for file icons in file fields Fixed

Proposed resolution

Add URL options variable to the template which includes attributes. See #29

This may be deprecated eventually: #2615660: Allow Link and LinkGenerator::generate() to provide attributes.

Remaining tasks

Review the patch and change record.

User interface changes

API changes

Data model changes

Feature request
Status

Needs work

Version

10.1

Component
File module 

Last updated about 1 hour ago

Created by

🇸🇪Sweden Arla

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

    @imclean I do think this could use a test case. To least show that altering works as expected.

  • Merge request !12139Add link options to apply to the a tag → (Open) created by imclean
  • 🇦🇺Australia imclean Tasmania

    Still needs tests.

    At the very least this should be updated as it's simply incorrect.

     *   - attributes: An associative array of attributes to be placed in the a tag.
     */
    function template_preprocess_file_link(&$variables): void {}
    

    file-link.html.twig:

    <span{{ attributes }}>{{ link }}</span>
    {% if file_size %}
      <span>({{ file_size }})</span>
    {% endif %}

    The attributes are applied to the wrapper element, not the a tag. See file.module for what's done with attributes.

  • Pipeline finished with Success
    15 days ago
    Total: 605s
    #498560
  • First commit to issue fork.
  • Pipeline finished with Failed
    2 days ago
    Total: 648s
    #508586
  • 🇮🇳India mohit_aghera Rajkot

    - Added a test to validate that attribute passed is exist on the render.
    - Variable hint in the twig template.

  • 🇺🇸United States smustgrave

    Looking good!

  • 🇨🇭Switzerland berdir Switzerland

    I'm a bit unclear what the specific issue is now.

    I worked with @arla in 2016 when we did encounter this, but things have changed since then. We did deprecate and remove Drupal::l() and replace it with a Link::toRenderable(), so you should be able to alter this in additional preprocess functions as it is an array and not just a string now.

    So it is possible to *alter* file_link template and its link structure and attributes. The thing I see is that it might be difficult to get enough context, for some cases you might want to alter only specific fields or so, then you'd want to alter the render array before this bit is rendered.

    Additionally to the title, the issue summary also needs to be updated with the proposed solution and use cases why it's useful.

Production build 0.71.5 2024