Basics of Debugging in Drupal

Evgeniy Melnikov

www.angarsky.ru

@angarsky

Developer's problems

  • You don't understand how Drupal core works?
  • Your site shows a white screen?
  • You can't fix a bug within a several hours?
  • You have to work on a site with a lot of custom code that was developed by another  developer?
  • Your site crashes after module updates?
  • ...

Calm down and start to analyze

  • Check Drupal logs (admin/reports/dblog)
  • Check server logs
  • Detect a problematic part of code
  • Developer tools will help you to solve a problem!

Devel module

  • A simple tool for Drupal debugging
  • Allows to display variables of functions
  • Allows to display a compiled SQL query 
  • Allows to prints a function call stack
  • Provides an information about theme registry, current menu item, user's session
  • Provides an interface to view and edit Drupal variables
  • Provides a quick links to flush a cache, run a cron, rebuild menus

https://www.drupal.org/project/devel

Devel module

Function dsm()

https://www.drupal.org/project/devel

Devel module

Function dpq()

https://www.drupal.org/project/devel


/**
 * Implements hook_preprocess_HOOK().
 */
function bootstrap_mel_preprocess_page(&$vars) {
    $d = db_select('users', 'u')
      ->fields('u', array('name', 'created'))
      ->condition('u.status', 1);
    dpq($d);
}

Devel module

Function ddebug_backtrace()

https://www.drupal.org/project/devel

Haked! module

  • This module scans the currently installed Drupal, contributed modules and themes, re-downloads them and determines if they have been changed
  • Install the Diff module to see the exact lines that have changed

https://www.drupal.org/project/hacked

Drupal function watchdog()

  • A Drupal function out of box
  • Writes a specified data to Drupal logs

/**
 * Function definition.
 */
function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) {}

// How to use this function.
watchdog('my_module', 'Data to show - @data.', array('@data' => $some_data), WATCHDOG_DEBUG);
  • See the data in Drupal logs (admin/reports/dblog)
  • Note: arrays should be serialized before sending in watchdog()

Preview settings of the Views module

  • Show the SQL query
  • Show performance statistics
  • Show other queries run during render during live preview

GIT

GIT

  • Possibility to go back to a last stable version of code
  • Prevents occasional errors of developer
  • History of code updates
  • Possibility to create an experimental code

Integrated Development Environment (IDE)

Integrated Development Environment (IDE)

  • Loads a whole project with all files, functions, classes  
  • Search and autocomplete for functions
  • Highlighting of variables, functions, code errors
  • Indents and formatting
  • References for functions, classes, methods, variables
  • Integration with Git (or another VCS)

Debugger

XDebug a PHP extension for powerful debugging. It supports stack and function traces, profiling information and memory allocation and script execution analysis.

Questions?

Basics of Debugging in Drupal

Evgeniy Melnikov

www.angarsky.ru

@angarsky

Made with Slides.com