[Drupal] How to add an SQL query to the global footer in Views without directly placing the PHP code in view?

| | 1 min read

Drupal 7 Views provides an option to add php to the Views footer in a View page. The option is called Global PHP. It allows you to add PHP code to views, to modify its output without adding code in a separate php file. However this option shoud be used with caution as a php error here can bring down the whole site and it would be difficult to detect. There is an alternative though. Read on to know how to add an SQL query to the global footer in Views without directly placing the PHP code in a view.

The solution is very straightforward. You would need to insert some PHP code in this global PHP but you would not need to place all your PHP code. All you have to do is to create a PHP file inside a Drupal module and then within the Global PHP field, include the file there. Here is a sample below.



That is all there to it. Now how do we access the view variables and objects in this file?

Views provides the following variables that you file can use to modify. You can find out the list of available variables from within the Global PHP option under the sectoin Available Variables

  • $view: The view object.
  • $handler: The handler object.
  • $results: Array containing the view's result.

Hope this short article has been helpful. Use the comment box below to share your views on Drupal Views and doubts regarding this article here