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.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.
- If you see this sort of error (in my case long back while installing standard installation)
- doesn't give much information about errorEnable 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 documenationUncomment in settings.php
if (file_exists(__DIR__ . '/settings.local.php')) {
include __DIR__ . '/settings.local.php';
}
# uncomment lines from settings.php
- No error shown by default
- NR would be greatDynamic 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 blockRender 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 TwigTwig 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.ymlHow to enable debugging
parameters:
twig.config:
debug: true
- enable twig debuggingBenefits of enabling debugging in Twig
- Default: false
- dump() output template variables
- template file name suggestions
- Turn off in production
- It enables auto-reloadTemplate 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 renderedTwig 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 itTwig 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 itkint
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();
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 applicationWeb-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 develWebProfiler in browser
- Download devel module
- enable devel and webprofiler module
- clear cache
- Set the permissionPHP Config
- Details about PHP version, enabled extensionsRequest Info
- Info related to page request example - route matched, route object, request header, cookiesTimeline
- Allows to visualize the order in which PHP controllers and events are called and time to compile them.
- Helps debug slow page loadDatabase
- 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 queryDatabase
- Click on "explain" details about type of query (fast or slow)
- display info and swappable keyUser
- 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 cookieViews
- Displays list of views that was rendered on the current pageForms
- Lists forms built on the current pageExtensions
- Displays all the modules and themes that are enabled on the siteCache
- Displays different caches by ID and the number of hits/missAssets
- Provides list of all CSS and JS files that were loaded for the page requestThank You!
Don't forget to rate session
Debugging in Drupal 8
- Hello Everyone! We are going to talk about "Debugging in D8"