Skip to content

Instantly share code, notes, and snippets.

@e0ipso
Last active December 2, 2015 19:18
Show Gist options
  • Save e0ipso/6592130d3d0e900d3a72 to your computer and use it in GitHub Desktop.
Save e0ipso/6592130d3d0e900d3a72 to your computer and use it in GitHub Desktop.
EntityFieldQuery IS NULL
<?php
$bundle = 'article';
$entity_type = 'node';
$field = 'body';
$column = 'value';
print init_query($entity_type, $bundle)
->count()
->execute() . ' entities.' . PHP_EOL;
$bodyless = init_query($entity_type, $bundle)
->fieldCondition($field, $column, NULL, 'IS NULL')
->count()
->execute();
print $bodyless . ' IS NULL entities.' . PHP_EOL;
$bodied = init_query($entity_type, $bundle)
->fieldCondition($field, $column, TRUE, 'IS NOT NULL')
->count()
->execute();
print $bodied . ' IS NOT NULL entities.' . PHP_EOL;
print sprintf('(%d + %d) = %d', $bodyless, $bodied, $bodyless + $bodied) . PHP_EOL;
/**
* Generate an Entity Field Query.
*/
function init_query($entity_type, $bundle) {
$query = new \Drupal\restful\Util\EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', $bundle);
return $query;
}
--------------------------------------------
➜ drush scr /var/www/custom-scripts/efq.php
--------------------------------------------
48327 entities.
536 IS NULL entities.
47791 IS NOT NULL entities.
(536 + 47791) = 48327
--------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment