4

I'm trying to get the value of a field and use it as a class for a wrapper of another field within a Views display. So essentially what I need is to be able to get a string representation of the first field. The field already is a text field but when I try getting the content it doesn't just give me a string, which is what I need to be able to use is as a class for my second field.

I've tried using {{ fields.[field_name].content }} but that renders the twig template recommendation along with the data. I tried using the getValue() function, but that doesn't return anything. I tried passing the data through the raw twig filter but that doesn't return anything. I tried rendering the array using dpm and kint but they both give me way too much information and I can't narrow it down to what array item or object I need to use. I'm new to Drupal so maybe I'm not using Devel and Kint correctly to find what I'm looking for.

Is there a function I have to use to get the content data as a string or just raw, or is there some other way I can accomplish this?

I am using Drupal 8.1.

3 Answers 3

3

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 }}
2
  • I think you are in the wrong template, to change a row you need views-view-fields.html.twig.
    – 4uk4
    Nov 17, 2016 at 14:42
  • You are right, it works on that template, but it prints out what I'm not looking for. If you get the chance, can you take a look at my question? drupal.stackexchange.com/questions/220643/…
    – Chayemor
    Nov 17, 2016 at 15:33
0

I've tried using {{ fields.[field_name].content }} but that renders the twig template recommendation along with the data.

That is because you have twig debug turned on. If you turn off twig debug the recommendation will go away.

3
  • 1
    Yeah, I know that. But since I'm currently developing I don't want to turn it off just yet. I used twig's split function to extract the string, but again once I turn off debug mode it's going to fail. I was hoping there was a function which would let me extract just the string regardless if I'm in debug mode or not. But I guess if it's not an option I'll have to just do with what I can.
    – chavab_1
    Jun 1, 2016 at 22:18
  • someone asked that question a few weeks ago Possible to turn off Twig debug mode for a specific template?
    – No Sssweat
    Jun 1, 2016 at 22:25
  • 1
    Thanx. Looks like he/she had the same problem as me. However, it looks like they just found a workaround for their specific problem rather than finding a way to turn off debug mode for a specific template. I think I'm just going to go with my workaround and then when I'm ready to launch I'll just make sure to remove it. I'm still hoping someone know either how to extract just the string or turn off debug mode for certain templates. Once again, thanx!
    – chavab_1
    Jun 1, 2016 at 22:44
0

{{ fields.[field_name].content|striptags|trim }}
this helped me printing path and image url fields

Your Answer

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

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