1

For some reason, my EntityQuery is returning 60 articles. There are actually 63 of them. I am certain about this.

Here are some questions I have:

  1. I don't think revisions are enabled for my content types, but how do I check? And, how do I do this programmatically? I'm not sure if my EntityQuery is returning nids or vids...
  2. Why would EntityQuery be unable to find an article? It seems like it can't find it, even if I just search for nodes containing part of its title. It returns 0 results.

Here's what I've tried:

I did a loop and loaded the nodes from the nids, printed out the titles, and I copy pasted the node titles (filtered on articles) from the content list, did a diff with the alphabetized results to find the three articles which EntityQuery doesn't believe to exist. They are all normal seeming articles, which can be viewed/edited.

I started debugging into the core EntityQuery files, but I'm afraid at this rate it's going to take me a while before I understand what's going on. Does anyone know why an EntityQuery might not find certain nodes, or even have an idea where I should be looking in the core files, or what I might be expecting to look for in the DB tables?

I'm using MySQL 5.7, in case that's relevant at all.

EDIT: Someone asked for the code, I am doing an EntityQuery for all articles, yet I don't get all of them:

$query = \Drupal::entityQuery('node')
  ->condition('type', 'article');
$entity_ids = $query->execute();

foreach($entity_ids as $id){
  echo $id->getTitle() . "\n";
  // Do I do Drupal entity node load, or do I load via revision ID?
  // Here I would like to load the node and then do stuff with it...
}
  • can you show some code pls .. – rémy Mar 1 '17 at 23:30
  • How are you certain there are 63? – Kevin Mar 1 '17 at 23:49
  • What code is there to show? I am literally doing an entity query for all articles. I will put it in the question anyway. – RaisinBranCrunch Mar 2 '17 at 2:03
  • I'm certain there are 63. I counted them in the content list, and I have copy pasted a list of all 63 of the titles, and I have a list of all 60 that are printed out from the entity query loop. – RaisinBranCrunch Mar 2 '17 at 2:04
  • 2
    If you have that list already then it should be easy to find the 3 IDs that are missing. Then check if you can find a difference with them. Also, that code makes no sense, $id is the node ID (the keys are the revisions but that's not relevant unless you explicitly query for revisions), you want something like foreach ($storage->loadMultiple($ids) as $node). entity query by default uses node access is the only thing that comes to mind. – Berdir Mar 2 '17 at 6:43
3

I added metadata to the query, running it as user 1, which allowed it to get all of the nodes:

$query = \Drupal::entityQuery('node')
    ->condition('type', 'article')
    ->addMetaData('account', \Drupal\user\Entity\User::load(1)); // Run the query as user 1.
$nids = $query->execute();

echo "count: " . $query->count()->execute() . "\n";

foreach ($storage->loadMultiple($nids) as $node){
  //do stuff
}

Also, since revisions are allowed for nodes, it was returning $nids as revision_id => entity_id as Berdir said in his comment.

  • 1
    if you don't want to have node access applied, use $entity_query->accessCheck(FALSE); – Berdir Mar 2 '17 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.