Media object always created when using debugging twig

Created on 8 July 2024, 4 months ago
Updated 9 July 2024, 4 months ago

Problem/Motivation

When debugging twig, media an empty media object is created even if it doesn't exist -- as long as one of the slides does contain a media element. This is because the test to determine if a media object exists is to see if the URL is an empty string, but when twig debugging is enabled, the string contains HTML comments.

Steps to reproduce

1. Enable twig debugging
2. Create multiple slides without Media element
3. Add a Media element to only one slide
4. Now all slides will have a space for a Media element

Proposed resolution

Do not test for URL string, but rather if media item exists (see current patch)

Remaining tasks

Review from colleagues to improve upon this initial fix

πŸ› Bug report
Status

Needs review

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States in0ni

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @in0ni
  • πŸ‡ΊπŸ‡ΈUnited States in0ni
  • Status changed to Needs review 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States in0ni
  • Status changed to Needs work 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States in0ni

    Excuse the noise, but after further testing -- this solution requires additional work.

  • πŸ‡ΊπŸ‡ΈUnited States dcam

    Even if you're able to fix the problem for media, it will still be an issue for other fields. The Twig debug setting is just problematic for a view like this. That's why I included a note about it in the documentation. Personally, if I have to work with a timeline, then I just shut the debug setting off. You may want to consider implementing a custom template for the media field in your theme that uses Twig filters to strip the comments. Someone else with a similar problem posted a link to this Stack Overflow thread about it: https://drupal.stackexchange.com/questions/201275/possible-to-turn-off-t.... That might be simpler.

  • Status changed to Needs review 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States in0ni
  • πŸ‡ΊπŸ‡ΈUnited States in0ni

    Ok, just finished up and seeing your comment. I did notice that for other fields this will be a similar issue, but particularly with this field it's problematic as it causes a rendering issue. (for example background I just left as-is since it does not cause any problems).

    To be quite honest, I read through the documentation (perhaps not 100%) and did not find a note about this.

    Also, I find the solution of having to turn off twig debug to be problematic and not optimal. You know the module well, but think of a team of developers who always have it on, or someone new that joints a team who always work with it on... it becomes a difficult thing to communicate. I can easily see someone wasting time, not understanding why a box appears for an image when there isn't one.

    The timeline should render properly (visually) as to not confuse, even with twig debug IMO.

Production build 0.71.5 2024