Debug entity query in Drupal 10 before the execution
When dealing with database custom queries, learn how to debug entity query in Drupal 10 and become more productive
You might need to create custom queries as a developer in order to retrieve data from Drupal entities such as Nodes, Users, Taxonomies, etc. Sometimes it can be challenging to retrieve the intended result because of small errors in the query. However, you may easily troubleshoot your queries without needing to reload the page with every modification.
Debug entity query in Drupal 10
Let's examine the question with an example. The data must be retrieved from the node entity. All nodes must be published, and entities must belong to the article content type. The sample code to get is shown below.
Work like a experienced developer and increase productivity when working with database custom queries, learn how to debug entity query.
try {
$entity_query = $this->entityTypeManager()->getStorage('node')->getQuery();
$entity_query->accessCheck()
->condition('type', 'articel')
->condition('status', Drupal\node\Entity\Node::PUBLISHED);
$result = $entity_query->execute();
}
catch (InvalidPluginDefinitionException $pde) {
$this->getLogger('Bhimmu Plugin Exec')->error($pde->getMessage());
}
catch(\Throwable $th) {
$this->getLogger('Bhimmu Code general')->error($th->getMessage());
}
This query will not provide any results if it is executed! nevertheless, why?
Following the verification, I discovered that the condition stated above is incorrect. A content type spelling error exists, with the word "article" being written as "articel." After executing this query ten times, I realized the issue, which is a typical coding error. This sample query is fairly simple, so I was able to read it quickly. But, there may be circumstances in which you need to write a longer query. How then would you debug it?
Print the query in plain text ad verify before execution
try {
/** @var \Drupal\Core\Entity\Query\QueryInterface $entity_query */
$entity_query = $this->entityTypeManager()->getStorage('node')->getQuery();
$entity_query->accessCheck()
->condition('type', 'articel')
->condition('status', Node::PUBLISHED);
$this->getLogger('Bhimmu Query')->info($entity_query->__toString());
// $result = $entity_query->execute();
}
catch (InvalidPluginDefinitionException $pde) {
$this->getLogger('Bhimmu Plugin Exec')->error($pde->getMessage());
}
catch(\Throwable $th) {
$this->getLogger('Bhimmu Code general')->error($th->getMessage());
}
We have added one line here to see the query in plain text so that we can see the query before executing it.
$entity_query->__toString()
__toString is one of Magic Methods which override PHP's default's action when certain actions are performed on an object. In our example it give us raw output of the query that eventually going to be executed. We will get the output as given below
Now you have a chance to verify this query without hitting refresh button so hard. Yun can easily read the conditions and fix the mistake. After that remove `$entity_query->__toString()` from your code and put `$entity_query->execute()` to run your query.
Fix the condition and execute to see the results
Here is the code after fixing the condition.
Above code will give the correct output if you have published Articles in your database.