How do you check if a field is empty in drupal 8?
Asked Answered
K

5

13

I have a field added to the content type of basic page. It is a select box, which is either checked or unchecked depending on whether or not the page is of the 'getting started' kind. I want to add the class 'getting-started' to the body element in the html if the page is a getting started page, i.e. the box is checked. I want to do this in the .theme file. So essentially I want to check too see if the field is empty, and if it isn't add the class to the body element. Any help would be much appreciated.

Kordofan answered 19/1, 2016 at 6:4 Comment(0)
P
25

To check if a field is empty you can use built-in method isEmpty().

E.g.:

if (!$entity->get('category')->isEmpty()) {
  /** @var EntityInterface $category */
  $category = $entity->get('category')->entity;
  $row['category']['data'] = [
    '#type' => 'link',
    '#title' => $category->get('name')->value,
    '#url' => $category->toUrl(),
  ];
}
else {
  $row['category'] = NULL;
}
Plough answered 21/5, 2017 at 9:34 Comment(0)
M
5

This solution worked for me, the field_poster has none or max 1 value.

{% if content.field_poster[0] is not empty %}
Marisolmarissa answered 1/3, 2018 at 14:41 Comment(0)
A
3

The following way you can check the field is empty or not in TWIG template.

{% if node.field_date.value %}
  <div class="my-field-wrapper">
    {{ content.field_date }}
  </div>
{% endif %}

or

{% if node.field_date.value is not empty %}
  {{ content.field_date }}
{% endif %}
Autohypnosis answered 21/2, 2018 at 9:4 Comment(1)
This will only work if this field type has a "value" property. Most don't.Windfall
N
0

You can use the template_preprocess_node (https://api.drupal.org/api/drupal/core%21modules%21node%21node.module/function/template_preprocess_node/8) function to pick off the available variables of the node being loaded.

In this function you can access the node's fields like this:

$variables['content']['field_some_name']

If your check passes, you can add some custom variable to use in the twig template:

$variables['something'] = true;

OR: you check the value of the field directly in the node twig template. The question is if it's really necessary for you to add your desired class to the body element. Maybe adding it to the article tag of the node does the job, that would be at least easier to check.

If you really need to add it to the body element, I guess you'll need to use the template_preprocess_page function, and check first if the page being loaded is a node, and then find out which node etc.

Nord answered 19/1, 2016 at 12:4 Comment(3)
Unfortunately I do have to add the class to the body element, since I need to change the color of the page title depending on the value of the field I'm checking. I'm familiar with the process of adding a class to the body in the 'template_preprocess_page' function, but I'm not familiar with the how's and why's of what you described in the latter part of your response, "check first if the page being loaded is a node and then find out which node etc".Kordofan
Couldn't I just check to see if the field isset with the code you sampled above, and then apply the class all in the preprocess page function?Kordofan
The "problem" is that template_preprocess_page is being called on all pages, not only nodes, e.g. on category pages, the front page or any other custom entity pages. So the $variables variable is not always the same. That's what I meant with "check first if the page being loaded is a node, and then find out which node etc.".Nord
P
0

In Drupal 8^9^10

Really goes without saying that to customize you must enter the templates of the specific place to be able to verify if a field is empty or not. But there are different ways to achieve it, in addition the functionality also varies according to the type of project.

Verify that a field is empty with multiple purposes.

  • To print or not what comes in the value.
  • To act with the value stored in the value of the field.
  • To add behavioral js functionality in your project.
  • As important data to send in a context.
  • To add a specific behavior of the template.
  • There are n-utilities that you can give it... Etc.

However, the community publishes the most used cases here a little more according to what you can build, similar when you program in android and when you create events to an element (image, button, text, etc), you can also customize your contents in drupal.

Expensive (render) check

{% if content.field|render is not empty %}
  {{ content.field }}
{% endif %}

Note that this can be an expensive (resource-wise) check to make.

Basic check

{% if node.field.value %}
  {{ content.field }}
{% endif %}

Entity Reference fields

{% if content.field['#items'].getValue() %}
  {{ content.field }}
{% endif %}

Review: Take the trouble to check out the community, to find more ways to solve a problem. Check Stackoverflow account whit similar issue, update information.

Logical filedset

{% if '1' in content['field_logical]['#items'].getValue()|first.value %}
  {#procedures#}
{% endif %}

Happy code!

Review this links for community

Pilaf answered 8/11, 2022 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.