Calling the Drupal API on the command line

Post by Mike C Picture of Mike C
Reading time 1 mins clock

If you're trying to trouble shoot problems on a Drupal site and need to get some information out of Drupal then using the command line can get you answers quick.

Traditionally calling any of the Drupal API functions to diagnose something would require peppering your modules with temporary debug code, or writing a whole new module to call API functions and display results.

The command line tool drush is a swiss army knife of useful Drupal functionality, and to make our life easier in calling Drupal API functions for a specific site we can use the 'php-ev' command, often abbreviated to 'ev'. With this command we remove the need to create new modules or adding temporary code to existing modules.

Because the PHP code being run is executed in the context of the Drupal site who's DocumentRoot you are in, you can pull information from the database about entities, users and the inner workings of Drupal.

Examples of useful things you can do:

  • Print a list of all modules which are reacting to a hook being called. Here we're finding all modules that hook in to the cron system to find one which could be executing a drupal_goto() or something equally naughty.

drush ev "print_r(module_implements('cron'))"

  • determine the alias of a system path.

drush ev "print_r(drupal_get_path_alias('node/3614'))"

If you come up with any handy clever ones share them on DropBucket.


Mike C

Managing Director

17 years of Drupal development wrangling and a background in digital project architecture.

Add new comment

Share this article

Sign up to our newsletter!

Our thoughts

Let's work together

Get in touch and find out how we can empower your organisation.
Back to top