I really feel stupid. I am quite new in drupal (i use drupal 8). I created a new content type with a lot of files. Also i created a new template for the content type.
Now i want to display several fields. That works:
Example: {{ content.field_shops }}
But i also want to add some html, so i wanted to check if there is something in content.field_shops and only let it be displayed if it is like that.
So i tried:
{% if content.field_shops %}{{ content.field_shops }}
{% endif %}
But it does not work ;-(
I also tried with "is empty", "ez_is_field_empty" and so on.
Do you have any idea how to check if a field contains data and only display it when it has any content.
Comments
This also does not work:
This also does not work:
{% if content.field_land is not empty %}Land: {{ content.field_land }}
{% endif %}
It will always be shown ... anyway if it is filled or not ;-(
\,,/
Coule of ways...
Generally I'd say you have to render the field to know if it's empty.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Thanx for your answer. Are
Thanx for your answer. Are you really shure? Isn`t it an object? Don't you think the data is in the object bevore rendering?
Also ... you gave me an example for an image field ... is it the same with a text field?
\"/
Yes, see this issue https://www.drupal.org/node/953034 only 195 comments to read through :)
The bottom line is, yes you can check for emptiness if you know what to check for - I am giving you a generic answer that should always work and does no harm to your site.
All fields are the same, pretty much.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
further reading :)
I was hunting for something else and stumbled on this stack exhange thread, you see Cottser suggests the same approach: http://drupal.stackexchange.com/questions/171092/how-do-i-access-field-d...
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
{% if content.field_name
Any idea why the Foo text would display on another line under the name in this example?
Yes, because something in
Yes, because something in content.field_name is a block level element, it likely has a div wrapper or an inline element like a span that is set to display: block etc.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
<div class="field__label"
So you think it has to do with the CSS?
Is this is a serious question
Is this is a serious question or are you trying to be funny? Sorry I can't tell.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Serious. :\
Twig code:
Page source result:
HTML:
Lable: Value
Foo
OK, sorry, I'm a bit lame
OK, sorry, I'm a bit lame when it comes to humour etc, developer Aspergers ect...
This comes from the field template:
You can do one of two (or three) things here:
display: inline;
or floats, flex etc, display: inline is easiest).Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Thanks for taking the time to
Thanks for taking the time to explain, I appreciate your time, but also slowly learning. With your suggestion of modifying the CSS, the issue isn't with the label or field value, it's the additional text.
What I expect (one line):
Label Value Foo
What I get (two lines):
Label Value
Foo
Try this and see what happens
Try this and see what happens - I can't know for sure without seeing your site if this will work in your theme (I'd HAVE to see the site to know for sure what all the other CSS is doing etc):
I.e. it should create, according to your earlier posts:
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Twig:
Twig:
Page Source:
Display:
Class 5
Foo
You can try:
You can try:
.field--name-field-class1.field--label-inline {display: inline-block}
Like I said before, this is all guesswork because I can't see your site, this is my last post, sorry, I am busy and without seeing your site this is just punching in the dark.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Didn't work, wish I should
Didn't work, all 3 are on separate lines now. Wish I should show you but my sandbox doesn't touch the Internet. Thank you for your time.
Try a field template ...
and move the
<div class="field__item">Foo</div>
in there.I've noticed that <div class=
I've noticed that
<div class="field__item">Foo</div>
is outside of the div too, but that's rendered automatically, I've not idea why. Jeff was able to suggest some additional CSS to get everything on one line, the NewsPlus Lite theme is causing issues.Yes, this helped!
Yes, this helped!
Render|StripTags|Trim
I have had situations where the rendered field contains markup (mostly divs) but no actual content. To avoid this I've used
{% if content.field_name|render|striptags|trim %}
{{ content.field_name }}
{% endif %}
NOTE: This will NOT display a field that contains HTML but no non-tag content (such as a form with only input fields) as the HTML will all be stripped away and there will be nothing left, so it looks blank to the IF statement.
Thanks diamondsea
Thanks diamondsea, this is a great tip. I used it to keep Layout Builder from rendering a section if all the views in that row are empty.
{% if (content.left or content.right %}
wasn't working, but{% set left = content.left|render|striptags|trim %}
{% set right = content.right|render|striptags|trim %}
{% if (left or right) %}
worked perfectly to prevent the section from appearing when there was no content. Thanks!
HI Jeff, this is a slightly
HI Jeff, this is a slightly different question but I couldn't find the answer anywhere...
I have an element on my twig page that I only want to show if the page is an Article,
so for example it would not show on my About page or my Home page, but it would show on all my articles.
I can't figure out what {% if %} to use to only display it on Articles. Do you know how I could do this?
Wildcard possible?
Is there any way to use a wildcard on content fields that differ slightly, for example...
No, you'd have to test them
No, you'd have to test them all individually. In your example (though the syntax is invalid) it would display all fields if any one of them had a value. Simplest version would be something like:
content.field_name
This worked for me
Thanks mariami.ta. That
Thanks mariami.ta. That worked for me too.
ReggieW
That should work as long as
That should work as long as the field contains no markup (wrapper DIVs, etc). If it does, see my above comments.
Hi Diamondsea (is that Sonic
Hi Diamondsea (is that Sonic Youth reference btw?)
how would i add a class to an already existing div.
Example: I am creating subtheme based of Barrio
the page.html.twig in barrio has this code:
You should just be able to
You should just be able to put your updated version of page.html.twig in your subtheme and it should pick that up instead of the parent theme's version.
(Actually my username is from an old diamond (gemstone) finding service web site (diamondsea.com) that I created. Sonic Youth really killed my SEO when that song came out :-( ).
thanks for your reply
thanks for your reply @diamondsesa
the barrio subtheme that I'm using, has a page.html.twig, that extends the main theme's page.html.twig
I use this variant
I use this variant, may be it will help somebody.
Thanks for the tip aaagepard
Thanks for the tip aaagepard
i May use that for future use,
in this scenario what I ended up doing was checking the theme hook suggestions for twig file names for each particular area of my page.
Once I created the new twigs I was able to add styles to the individual areas that way.