Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Calling the Drupal API on the command line

Parent Feed: 

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.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web