Score:0

EntityQuery condition only if field exists

ru flag

How to apply a condition only if a field exists?

$query = \Drupal::entityQuery('node')
  ->condition('type', 'videos')
  ->condition('field_tags', 39, '=')
  ->condition('status', 1)
  ->condition('field_date_start', $now->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT), '<=')
  ->condition('field_date_end', $now->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT), '>=')
  ->execute();

I want to get nodes that meet the condition about field_date_end but also those, that do not have a value for field_date_end.

Thanks in advance !

Score:1
ph flag

Something like this:

$query = \Drupal::entityQuery('node');
$group = $query
  ->orConditionGroup()
  ->condition('field_date_end', $now->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT), '<=')
  ->notExists('field_date_end');
$results = $query
  ->condition('type', 'videos')
  ->condition('field_tags', 39, '=')
  ->condition('status', 1)
  ->condition($group)
  ->execute();
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.