This snippet returns TRUE or FALSE if an entity has a filled-in value on a entity reference on field_winner field.

A nice example of the Exists condition on entityQuery

  /**
   * Returns true if there is already a winner of one active contest.
   *
   * @return FALSE|TRUE
   */
  public function contestHasWinner()
  {
    // Find a winner of at least one active contest
    $count = \Drupal::entityQuery('contest')
      ->condition('status', 1)
      ->Exists('field_winner')
      ->count()
      ->execute();
    return ($count > 0) ? TRUE : FALSE;
  }

 


Saved you some valuable time?

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