I want to display the query that is executed in the drupal view. Currently in the view editor it shows the query however I have a need to use that query in my code to download an excel version of the view.
Is there a way to get the executed query the same way it's shown in the "editor" window of the views menu? I want this at the time the the view is shown.
What I plan to do here is to capture the query in the footer, and have that query posted to a process which will send back an XLS resultset. So i'd like the exact query the view is using to display the results.
Usually we use dpm() for printing strings and queries in Drupal 6 and 7. However if we need to execute the query for debugging, dpm() will print only the object which is not that useful. dpq() is used in such a situation where it will print the query string with actual arguments.
SQL Views are stored as individual objects in the database. Though you can use Drupal's database functions to select data from them, SQL Views are otherwise not known within Drupal unless you integrate them with the Views module or use something like the Data module to expose them to some extent.
Go to the “Views settings” page under /admin/structure/views/settings and check the “Show the SQL query” option of the “Live preview settings.” Then you can go to any view and upon clicking the “Update preview” button you will see the SQL query issued by Drupal.
As in the seventh version, Drupal 8 builds the query based on methods such as fields (), join (), condition (), and so on. You can execute the query by calling 'the execute `() method. $query = \Drupal::database()->select('node_field_data', 'nfd'); $query->fields('nfd', ['uid', 'title']); $query->condition ('nfd.
Or you can use hook_views_pre_execute
along with devel's dpq function:
function MY_MODULE_views_pre_execute(&$view) {
dpq($view->build_info['query']);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With