debugging-in-d8



debugging-in-d8

2 0


debugging-in-d8

This is the PHPWorld 2015 presentation

On Github Kalpana-Goel / debugging-in-d8

Debugging in Drupal 8

- Hello Everyone! We are going to talk about "Debugging in D8"

Slides

bit.ly/debugging-d8

- I have posted slides with speaker notes online. You can follow along the slides.

Kalpana Goel

kgoel

kalpanagoel

- Developer at Forum One - Forum One is full service digital agency and does lot of work in Drupal for Government and non-profit organizations.

William Hurley

wwhurley

wwhurley

- Technical Manager at Forum One

First mention of bug and debugging

- First reported bug in 1947 - Bug and debugging terms are attributed to Grace Hopper when she was working on computer at Harward University. - Someone discovered moth stuck and stop the operation, she made a remark that they are debugging the system and since then we are using that term.

Debugging steps

- Write tests so there are minimal chances of bug in your code - if a bug does exist, reproduce a problem - use debug in your code. In D7 - var_dump(), dpm - find the source of the problem and fix it.

Debugging in Drupal 8

- If you see this sort of error (in my case long back while installing standard installation) - doesn't give much information about error

Enable Error Messages

$config['system.logging']['error_level'] = 'verbose';
- cp examples.settings.local.php to settings.local.php, Uncomment lines in settings.php referring to settings.local.php and uncomment the following the bottom of the file - this turns CSS/JS aggregation off if you enable it - Take a look in settings.local.php for in-depth documenation

Uncomment in settings.php

if (file_exists(__DIR__ . '/settings.local.php')) {
  include __DIR__ . '/settings.local.php';
}
# uncomment lines from settings.php

drupal.org/node/2313059

- No error shown by default - NR would be great

Dynamic Page Cache

$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
- uncomment from settings.local.php - drush cr - enabled by default - Cache only bit of the page for example user block or menu block which - displays dynamic info about user name and menu based on user permission - won't be cached but everything else like static block

Render Cache

$settings['cache']['bins']['render'] = 'cache.backend.null';
- uncomment from settings.local.php - drush cr - If you have a piece of content and if you want to make a change in the node or block twig template, to display the changes then you would need to clear the cache all the time if you don't want to do that then disable render cache. - Render caching enable by default to speed up page load - Since we are talking about twig templates, lets talk about debugging in Twig

Twig debugging

- Drupal8 uses Twig as its templating engine - services.yml (turn on settings for enabling twig debugging is in services.yml) - if not cp sites/default/default.services.yml to sites/default/services.yml

How to enable debugging

parameters:
  twig.config:
    debug: true
- enable twig debugging

Benefits of enabling debugging in Twig

- Default: false - dump() output template variables - template file name suggestions - Turn off in production - It enables auto-reload

Template file name

- You can find this information by inspecting element in browser - Twig template file name suggestions are in order from most specific to least specific - current file name suggestion in use has an "x" beside it - Along with the BEGIN OUTPUT and END OUTPUT you will find the full path to the template being rendered

Twig auto-reload

- This is another benefit of enabling Twig debug option - automatically recompile Twig templates if source code changes (old templates) - Turn off in production - look in services.yml file to read more about it

Twig cache

- It will make things slower - If you disable Twig cache then if you wont be able to access compiled templates for debugging because its compiled into PHP class and it is stored in the disk with no read permission. - Its bad to disable twig cache - don't touch it

xdebug

kint

  kint($vars);
- may be already familiar with knit - It's way better than dump() - kint module is sub-module of devel module which you can use as a debugging tool. you can use it from php or twig template.

kint - backtrace

  kint_trace();

kint - different themes

Console

drupalconsole.com | bit.ly/console-doc

- Drupal console is a suite of tools run from CLI - It helps with debug, clear cache and run other useful commands from command line - Run drupal-list and it will give you list of commands that can be run from CLI - Container:debug - displays current services for the application

XHProf

- XHProf in D7

Web-profiler

- useful for debugging, performance testing - learn how your site handles request - Symfony 2 comes with WebProfiler bundle. It collects information about each request made to your site - Allows to visualize data in the browser - WebProfiler is now a sub-module of devel

WebProfiler in browser

- Download devel module - enable devel and webprofiler module - clear cache - Set the permission

PHP Config

- Details about PHP version, enabled extensions

Request Info

- Info related to page request example - route matched, route object, request header, cookies

Timeline

- Allows to visualize the order in which PHP controllers and events are called and time to compile them. - Helps debug slow page load

Performace Timing

Database

- queries are listed in execution order with ability to search by filter (select, insert) - or filter by slow query - displays execution time, PHP file that made query

Database

- Click on "explain" details about type of query (fast or slow) - display info and swappable key

User

- Name of current user - roles etc. - Provider - If basic oauth module is enabled and basic configuration is setup then it will display basic auth. In thos case its cookie

Views

- Displays list of views that was rendered on the current page

Forms

- Lists forms built on the current page

Extensions

- Displays all the modules and themes that are enabled on the site

Cache

- Displays different caches by ID and the number of hits/miss

Assets

- Provides list of all CSS and JS files that were loaded for the page request

Thank You!

Don't forget to rate session

Debugging in Drupal 8 - Hello Everyone! We are going to talk about "Debugging in D8"