1 of 31

BadCamp 2015

Debug Drupal with Devel, XDEBUG + More

2 of 31

About Us

Kalamuna makes the Internet for for mission-driven organizations driven to tinker, critique, and change the way things are. We specialize in design, strategy, user experience, and development.

3 of 31

John Ouellet

Sr. Developer & Support Manager

I dejank the jankness in your janky websites.

labboy0276

4 of 31

I am totes qualified, for realz

  • Built First Professional Website in 1995.
  • Began using PHP ~2002.
  • CMS Domination happened soon thereafter.
  • Drupal since about 2009.

5 of 31

I have 99 DEBUG commands, and Drupal uses all of them.

6 of 31

error_reporting(E_ALL);

ini_set('display_errors', TRUE);

display_errors = On

log_errors = On

ddebug_backtrace();

dpm();

drupal_set_message();

dd();

db_queryd();

TOO MANY CHOICES CAN MAKE YOU GO CRAZY!!!!

ini_set('display_startup_errors', TRUE);

views_trace();

dvm();

kpr();

dargs();

7 of 31

BUT John, which ones do I use??

8 of 31

Good Question

9 of 31

Quick Stuff on Debugging

Front End (HTML / CSS)

Use Google Chrome DevTools (f12 to open)

Front End (JS)

console.log() or alert() in code

Chrome DevTools - Sources Panel w/ Breakpoints

10 of 31

Quick Stuff on Debugging

WSOD

Put this in at the top of your index.php�

error_reporting(E_ALL);

ini_set('display_errors', TRUE);

ini_set('display_startup_errors', TRUE);

11 of 31

Quick Stuff on Debugging

Google your errors:

Copy and paste the error and edit it to just have the file name with line errors.

Notice: Trying to get property of non-object in metatag_views_post_render() (line 2116 of metatag.module).

Other great places to search are:

Drupal Answers - http://drupal.stackexchange.com/

Drupal.org - http://www.drupal.org

Drupal API - http://api.drupal.org/api/drupal

RTFM

12 of 31

Debug with DEVEL

Devel Module - https://www.drupal.org/project/devel

Devel Kint Module - https://www.drupal.org/project/devel

- Need Kint for Devel to work in Drupal 8.

Search Krumo - https://www.drupal.org/project/search_krumo

- Only for Drupal 7 right now.

13 of 31

All you really need is dpm();

14 of 31

Debug with DEVEL

If you don’t care what I have to say, then here is the list

of all the devel functions:

https://api.drupal.org/api/devel/functions

15 of 31

Show the people!

16 of 31

XDEBUG

BENEFITS

  • Platform Agnostic
  • Works on any version of PHP up to 5.6
  • Fairly easy to use

17 of 31

XDEBUG - INSTALL

  • IDE of my choice = Sublime
  • Does work with other IDE’s (Eclipse, NetBeans, etc)
  • We will be using Sublime
    • http://www.sublimetext.com/

18 of 31

XDEBUG - INSTALL

VAGRANT / LAMP / LEMP

sudo apt-get install php5-xdebug

19 of 31

XDEBUG - INSTALL

Add to your php.ini / xdebug.ini

zend_extension="/wherever/you/put/it/xdebug.so"

xdebug.remote_enable = 1

xdebug.remote_host = "127.0.0.1"

xdebug.remote_port = 9000

xdebug.remote_handler = "dbgp"

xdebug.remote_mode = req

xdebug.remote_connect_back = 1

20 of 31

XDEBUG - INSTALL

Drupal 8 - add to your php.ini

xdebug.max_nesting_level = 256

xdebug.show_exception_trace = 0

xdebug.collect_params = 0

21 of 31

XDEBUG - INSTALL

Or Just Use Kalabox 2

Currently in alpha4 CLI.

GUI coming in a few months.

Comes with everything installed!

https://github.com/kalabox/kalabox

22 of 31

XDEBUG - SETUP

SublimeTextXdebug

https://github.com/martomo/SublimeTextXdebug

23 of 31

XDEBUG - SETUP (SUBLIME PROJECT - VAGRANT)

Create a folder called ~/sublime-projects

Create a file called yoursite.project

Next slide has config

24 of 31

25 of 31

XDEBUG - SETUP (SUBLIME PROJECT - KALABOX 2)

Setup instructions are here:

https://github.com/kalabox/kalabox-app-pantheon#xdebug

26 of 31

XDEBUG - SUBLIME COMMANDS + ADVICE

  • Use a similar variable for testing breakpoint.
    • $test = 1;
  • Breakpoints don’t move when you add or remove code.
    • Always clear breakpoints when setting new ones.
  • Add this query to your path to start XDEBUG
    • ?XDEBUG_SESSION_START=sublime.xdebug

27 of 31

XDEBUG - SUBLIME COMMANDS + ADVICE

  • Start Debugging - Ctrl+Shift+F9 or ⌘+Shift+F9
  • Stop Debugging - Ctrl+Shift+F10 or ⌘+Shift+F10
  • Step Into - Ctrl+Shift+F7 or ⌘+Shift+F7
  • Step Out - Ctrl+Shift+F8 or ⌘+Shift+F8

28 of 31

YOU ARE READY TO XDEBUG

29 of 31

Show the people!

30 of 31

Misc Debugging Tips with Watchdog

Watchdog

watchdog($variable);

watchdog('MY_DEBUG', 'message: %msg', array('%msg' => $variable), WATCHDOG_NOTICE);

watchdog(‘MY_DEBUG', '<pre>' . print_r($variable, TRUE). '</pre>');

D8 Version

\Drupal::logger('MY_DEBUG')->notice($variable);

31 of 31

www.kalamuna.com

john@kalamuna.com

Thank You!