Score:-2

get node id from entityQuery('node')

np flag

I want to get the node id from entityQuery('node') the below query give me array vid = nid

$nids = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'account_content')
->condition('field_account_id', $accountid)
->execute();

When using array_keys($nids) // getting vid which is revision id

When using array_values($nids) // getting nid which is node id

I want the node id i.e. nid rather to vid

$nids['nid'] = numeric value nid

because am not sure whether i am getting $nids = array([1] => [1]) array key is nid or vid same for value

Score:1
sa flag

You should check the method comments about the return value in the Drupal/Core/Entity/Query/QueryInterface::execute() https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Query%21QueryInterface.php/function/QueryInterface%3A%3Aexecute/9

 /**
   * Execute the query.
   *
   * @return int|array
   *   Returns an integer for count queries or an array of ids. The values of
   *   the array are always entity ids. The keys will be revision ids if the
   *   entity supports revision and entity ids if not.
   */

So, it give you an array, get the keys or values depends on your need.

Score:0
de flag

You can create an array keyed by NID, with the value being the NID as follows:

$nids = \Drupal::entityQuery('node')
  ->condition('status', 1)
  ->condition('type', 'account_content')
  ->condition('field_account_id', $accountid)
  ->execute();

// Create an array, each element of which
// is keyed by the NID, with the value being
// the NID.
$nids = array_combine($nids, $nids);

If the values explicitly need to be integers you can run through the resultant array:

// Cast the value of each element of the array
// as an integer, by walking through each element
// of the array.
array_walk($nids, function(&$value) {
  // Cast the value as an integer.
  $value = (int) $value;
});
New Dev avatar
np flag
@japan It look like same as array_values($nids)
Jaypan avatar
de flag
No, `$nids = array_combine($nids, $nids);` will result in an array keyed by NID, with the value by NID. It is not the same as your original array. And the call to `array_walk` will set the values to be of type Integer.
I sit in a Tesla and translated this thread with Ai:

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.