Debug Drupal query using query_alter


Add your query probably in your custom hook_init()

function mymodule_hook_init() {
$query = new EntityFieldQuery();
  $result = $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'mybundle')
      ->propertyCondition('status', 1)
      ->propertyOrderBy('created', 'DESC')
      ->fieldCondition('field_news_type', 'tid', '282', '=')
      ->addTag('efq_debug')
      ->range(0, 10)
      ->execute();
}

In the custom module add this


function mymodule_query_alter($query) {
  if ($query->hasTag('efq_debug')) {
    $sql = (string) $query;
    $connection = Database::getConnection();
    foreach ((array) $query->arguments() as $key => $val) {
      $quoted[$key] = $connection->quote($val);
    }
    $sql = strtr($sql, $quoted);
    dpm($sql);
  }
}

Comments

Popular posts from this blog

Programatically create layout builder section in Drupal

Code quality analysis of Drupal. Can I use Sonar?

Set up Drupal7 to Drupal8 migration in simple steps (using Drush)