Drupal has a method called isEmpty(). An example:

if($project->get('field_project_status')->isEmpty()) {
   \Drupal::logger('my_module')->notice('Project '. $project->label() .' has no status yet.');
}

This is what it actually does:

public static function isEmpty(array $elements) {
  return empty($elements) || count($elements) === 1 && array_keys($elements) === [
    '#cache',
  ];
}

In twig you can use the following:

{% if content.field_project_status is empty %}
.. do your thing
{% endif %}

{% if content.field_project_status is not empty %}
.. do your other thing
{% endif %}

 

 

 

 

Saved you some valuable time?

 

Donate and get more free time-saving code on this website in the future.