1

Using Drupal 8

I want to print out a field's content into the src attribute. I have the following template for my view:

<div class="videoWrapperHD">
     <iframe width="560" height="315" src="{{ rows[0].content | raw }}" 
      frameborder="0" allowfullscreen>
     </iframe>
</div>

But the iframe gets filled with my own site's "Page Not Found" page instead of the Youtube Video because Twig prints out a whole lot of debug comments before and after printing the variable rows[0].content.

Is it possible to disable the debug comments for a specific field? I don't want to have to be disabling/enabling debug to make sure it works as expected. Because it works if I disable Twig debugging (the comments aren't printed so the URL prints out fine) but it's tiresome to be enabling/disabling to check work.

I also tried using {{ attributes.setAttribute('src', {{ rows[0].content }} ) }} , but no dice.

Another failed attempt was:

{% set iframe_src = rows[0].content %}
<div class="videoWrapperHD">
    <iframe width="560" height="315" {{ attributes.setAttribute('src', iframe_src) }}
     frameborder="0" allowfullscreen></iframe>
</div>

My last idea was this:

{% set url = rows[0].content | raw %}
{% set iframe_src = 'src=' ~ url %}

<div class="videoWrapperHD">
   <iframe {{ iframe_src }} ></iframe>
</div> 

But it prints out src=Array

SOLUTION

The answer is in another question, I'm pasting it here in case that one gets deleted. The author of the following answer is @4k4

field.content is the rendered field. And in views that means it is no longer a render array, but the finally rendered markup. So this is very problematic to use it as a class name, not only because of twig debug.

Better use the row data, where you find the entity object with the field data from the database. Use clean_class to escape it for using it as a class name:

{{ row._entity.field_myfield.value|clean_class }}
4
  • Is the content property a valid URL?
    – mradcliffe
    Commented Nov 8, 2016 at 19:30
  • The problem is, this is rendered output, which is supposed to be rendered by a browser, not put into a src attribute or a class name, like in this question drupal.stackexchange.com/questions/202885/…
    – 4uk4
    Commented Nov 8, 2016 at 20:20
  • If the content property is not a valid URL than this is not possible. iframe.src property must be a URL. The iframe.srcdoc can be content, but this is not supported in all browsers (2016.11).
    – mradcliffe
    Commented Nov 8, 2016 at 22:22
  • Yes it's a valid URL, it's a Youtube Link to a a video.
    – Chayemor
    Commented Nov 9, 2016 at 0:15

1 Answer 1

1

I'm pretty sure the nested braces here are an error - inside a {{ }} expression, variables don't need to be enclosed again:

 {{ attributes.setAttribute('src', {{ rows[0].content }} ) }}

But for the real answer: This template isn't the culprit - the content is already rendered separately, passing through its own template which adds the comments.

See Get Views field value in Twig template, where the recommended way of getting at the unrendered field value is via the _entity object.

0

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.