5

I am trying to check if the user_picture field is empty using the twig template, but I am not able to do this.

I tried lots of ways:-

{% if node.user_picture.value %}
{% if node.user_picture.entity %}
{% if content.user_picture.value %}
{% if content.user_picture|render.entity %}
{% if content.user_picture|render %}

Sample of code:

  {% if node.user_picture.value %}
    <p>User-picture 02</p>
    {{ user_picture }}
  {% else %}
    <p>Anymonus-picture 02</p>
    <div>
      <a href="#"><img src="{{base_path}}/WEBSITE/themes/THEME/images/anonymous.jpg" alt=""></a>
    </div>
  {% endif %}

4 Answers 4

10

Use the method isEmpty() on the image field:

  {% if node.field_image.isempty %}
    <p>Field "field_image" is empty.</p>
  {% endif %}

Although in this case you might need to check the user entity of the node author:

  {% if node.uid.entity.user_picture.isempty %}
    <p>Picture of the node author is empty.</p>
  {% endif %}
7
  • 2
    I tried it, but it's not working, Its always outputting FALSE Sep 28, 2017 at 13:29
  • I also tried {{ node.user_picture.isempty }} and it is not rendering anything Sep 28, 2017 at 13:39
  • Are you sure you have this field in the node? This is a field name used in the user entity, see the second example.
    – 4uk4
    Sep 28, 2017 at 13:41
  • Yes, I am sure, I am trying to overwrite the comment.html.twig file, to be able to give the comment a user image even if the user has not added any image yet (Default image). and i did try the second example and still not working. Sep 28, 2017 at 14:28
  • 4
    Remember: use node.field_image not content.field_image. I may have lost some hair over this ;) Sep 24, 2018 at 23:55
3

I was able to use either one of the following in a template. The key to all of this is that you use node.user_picture.

{% if node.user_picture is empty %}
{% if node.user_picture.isEmpty() %}
{% if node.user_picture.isEmpty %}

That can be used to verify the field is empty. If you have set a default image on the content type field's setting, then you can use content.user_picture which should display either the image that was selected on the entity or the default image from the field's image setting.

1

I believe it best to use Drupal to set a default image for users. Edit the User Picture field and upload a default image.

If you're in comment.html.twig, "user_picture" is available just as "node" or "content" is:

{{ user_picture }}

And the way to the image value is:

{{ user_picture['#user'].user_picture.entity.uri.0.value }}

That's if you did a full install of Drupal and not a minimal install. In a minimal install you will need to add your own field to the User Account fields. Then you just access your field name:

{{ user_picture['#user'].field_machine_name.entity.uri.0.value }}

What I've done before in the comment.html.twig file:

{% if user_picture['#user'].user_picture.entity.uri.0.value != "" %}
    <a href="/user/{{ user_picture['#user'].uid.0.value }}">
         <img src="{{ file_url(user_picture['#user'].user_picture.entity.uri.0.value) }}" />
    </a>
{% else %}
    <img src="/path/to/image" />
{% endif %}

Also you can check for a user id:

{% if user_picture['#user'].uid.0.value > 0 %}

A good way to set a picture for anonymous users.

0

It is possible to negate the twig condition for others purposes :

{% if not node.field_image.isempty %}
  <p>Field "field_image" is not empty.</p>
{% endif %}

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.