Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Debugging in Drupal 8
Life After DPM : Creating and Debugging a Module
Allie Jones
Allie Ray Jones | @AllieRays
Review of Resources
Gitbook of Configuration Steps: https://www.gitbook.com/book/zivtech/debug-tools-for-drupal8/details
G...
Plan Ahead:
Configuration to avoid frustration
Reduce technical debt
Why is this Important?
Stop ‘Googling’ all of your pr...
Overview
Debugging is a Personal Choice. Pick Your Tools.
Drupal Console IDE Xdebug
Continuous
Integration
Zivtech Rocks My Socks ( demo site http://zivte.ch/1Ss47k )
But First, Basic Configuration
Easy / Low Hanging Configuration Options
PHP.ini
Set Error Reporting to Strict: error_reporting = E_ALL & ~E_DEPRECATED & ...
Easy / Low Hanging Configuration Options
Configuration Settings: Use your setting.local.php.
In setting.php add an include...
Easy / Low Hanging Configuration Options
Documentation: https://api.drupal.org/api/drupal/8
Community: StackOverflow, Drup...
Change Records:
https://www.drupal.org/list-changes/drupal
Drupal Print Message
The Devel Module provides helper functions to print variables in the browser.
function dpm($input, $n...
Caching
From the Terminal:
● Drush: drush cr (new as of D8)
● Drupal Console: drupal cache:rebuild
Everyone’s favorite lov...
But Seriously,
Did You
Clear Your Cache?
Easy / Low Hanging Configuration Options
"Log. (Huh) What is it good for. Absolutely ..."
Debugging
/admin/reports/dblog
Devel Module
Drupal 7 Drupal 8
Drupal 8
Devel out of the Box
Devel Submodule: Kint
prettify all the things
“It's var_dump() and
debug_backtrace() on steroids”
print data in a human re...
Devel Submodule: Web Profiler
/admin/config/development/devel/webprofiler
Based on Symfony’s toolbar
Summary toolbar
Resou...
Web Profiler in action
Drupal Console
Built on the Symfony console library
Debug Your Code
drupal routes:debug
drupal router:debug [internal rout...
Demo
Zivtech Rocks My Socks (demo site)
Socks Module
Module & Basic Controller:
drupal generate:module
generate:controller
Config Entity : drupal generate:entity:config
Form : drupal generate:form
Let’s Break
Some Things !
Problem:
Class Not Found
PHPStorm
PHP IDE Integrated Development Environment by Jetbrains
Code hinting
Auto completion
Breakpoints
Built in Drupal ...
Controller Goal: Display a basic page
namespace DrupalsocksController;
use DrupalCoreControllerControllerBase;
class SockC...
Controller
namespace DrupalsocksController;
class SockController extends ControllerBase {
public function content() {
$soc...
Problem: Accessing
Private and
Protected Methods
Goal: Create a configurable entity type of Sock
namespace DrupalsocksEntity;
use DrupalCoreConfigEntityConfigEntityBase;
c...
class Sock extends ConfigEntityBase {
/**
* The sock's rating.
* @var string
*/
protected $rating;
}
public function build...
SOLUTION
protected $rating;
/**
* @return string
*/
public function getRating() {
return $this->rating;
}
/**
* @param str...
Problem:
Syntax Error
Xdebug
Allows script execution to be paused at any point to read through variables
Support stack and execution traces in e...
public function submitForm(array &$form, FormStateInterface $form_state) {
$result = $form_state->getValue('fav_sock');
dr...
Form
class FavoriteSockForm extends FormBase {
public function getFormId() {
return 'favorite_sock_form';
}
public functio...
Xdebug
$form_state->getValue is an object,
but the code is trying to access it as if it were an array.
Change $form_state[...
Debugging and QA does not stop at code
completion.
Problem:
Shared Staging Environment
Client Approval Process
Probo CI
Probo CI creates test environments for each new feature
Visual representation of the project
while development is...
Probo CI
assets:
- dev.sql.gz
steps:
- name: Example site setup
plugin: Drupal
database: dev.sql.gz
databaseGzipped: true
...
Problem:
Cherry Picked
Deployments
PROBLEM
Pull Requests with Probo CI
SOLUTION
https://github.com/AllieRays/debugging-drupal-8/pull/1
https://9365d60b-17fa-4834-bb2...
Gitbook of Configuration Steps: https://www.gitbook.com/book/zivtech/debug-tools-for-drupal8/details
Github Repo of ZRMS a...
Gitbook of Configuration Steps:
https://www.gitbook.com/book/zivte
ch/debug-tools-for-drupal8/details
Thank You!
Upcoming SlideShare
Loading in …5
×

Debugging in drupal 8

2,146 views

Published on

Creating and breaking a module in Drupal 8.

Configuration Steps: https://www.gitbook.com/book/zivtech/debug-tools-for-drupal8/details


Debug your PHP code efficiently without Devel module’s dpm(Drupal Print Message). With Drupal 8’s adoption of Symfony and object-oriented architecture, we need to adjust how we debug our code. Up until now, many of us have been using the convenient dpm function for debugging and introspection. However, dpm cannot print protected properties or provide information about methods of an object.

This makes it difficult to debug objects and classes. It’s time to learn a new approach! We will look at alternative methods to solve this problem such as:

Xdebug with PHPStorm configuration - http://xdebug.org
Devel and the WebProfiler (D8+) Drupal module https://www.drupal.org/project/devel
Using the continuous integration tool Probo CI to speed up the testing and approval process - http://probo.ci/
We will create a module, break it and use the above tools to debug, test and fix the code. Whether you are dipping your toes into object-oriented PHP or you're an advanced developer, everyone can benefit from debugging their code more efficiently.

Published in: Technology
  • Be the first to comment

Debugging in drupal 8

  1. 1. Debugging in Drupal 8 Life After DPM : Creating and Debugging a Module Allie Jones
  2. 2. Allie Ray Jones | @AllieRays
  3. 3. Review of Resources Gitbook of Configuration Steps: https://www.gitbook.com/book/zivtech/debug-tools-for-drupal8/details Github Repo of ZRMS and Module: https://github.com/AllieRays/debugging-drupal-8 Demo Site: http://zivte.ch/1Ss473k Slide Deck: http://zivte.ch/1VxWF60 Zivtech Rocks My Socks Demo Site Bear Install Profile Drupal 8 Site
  4. 4. Plan Ahead: Configuration to avoid frustration Reduce technical debt Why is this Important? Stop ‘Googling’ all of your problems away (If this blog post doesn’t solve my problem another one will) ● Fix problems & code faster Stop thinking about debugging after the fact. You should be logically thinking through as you develop.
  5. 5. Overview Debugging is a Personal Choice. Pick Your Tools. Drupal Console IDE Xdebug Continuous Integration
  6. 6. Zivtech Rocks My Socks ( demo site http://zivte.ch/1Ss47k )
  7. 7. But First, Basic Configuration
  8. 8. Easy / Low Hanging Configuration Options PHP.ini Set Error Reporting to Strict: error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT Display Errors as Html: display_errors = On Log your errors to a php_error file: log_errors = On || log_errors_max_len = 1024
  9. 9. Easy / Low Hanging Configuration Options Configuration Settings: Use your setting.local.php. In setting.php add an include if (file_exists(__DIR__ . '/settings.local.php')) { include __DIR__ . '/settings.local.php'; } setting.local.php Show all error messages, with backtrace information. $config['system.logging']['error_level'] = 'verbose'; Disable CSS and JS aggregation. $config['system.performance']['css']['preprocess'] = FALSE; $config['system.performance']['js']['preprocess'] = FALSE;
  10. 10. Easy / Low Hanging Configuration Options Documentation: https://api.drupal.org/api/drupal/8 Community: StackOverflow, Drupal.StackExchange & Drupal IRC Change Records: https://www.drupal.org/list-changes/drupal
  11. 11. Change Records: https://www.drupal.org/list-changes/drupal
  12. 12. Drupal Print Message The Devel Module provides helper functions to print variables in the browser. function dpm($input, $name = NULL, $type = 'status') { if (Drupal::currentUser()->hasPermission('access devel information')) { $export = kprint_r($input, TRUE, $name); drupal_set_message($export, $type, TRUE); } return $input; } What is DPM anyways?
  13. 13. Caching From the Terminal: ● Drush: drush cr (new as of D8) ● Drupal Console: drupal cache:rebuild Everyone’s favorite love / hate relationship From the Browser: ● Admin Ui: /admin/config/development/performance ● Chrome Settings: Disable cache
  14. 14. But Seriously, Did You Clear Your Cache?
  15. 15. Easy / Low Hanging Configuration Options "Log. (Huh) What is it good for. Absolutely ..." Debugging /admin/reports/dblog
  16. 16. Devel Module Drupal 7 Drupal 8
  17. 17. Drupal 8 Devel out of the Box
  18. 18. Devel Submodule: Kint prettify all the things “It's var_dump() and debug_backtrace() on steroids” print data in a human readable way backtracing
  19. 19. Devel Submodule: Web Profiler /admin/config/development/devel/webprofiler Based on Symfony’s toolbar Summary toolbar Resources admin page Review resource utilization Cache effectiveness Database queries Views
  20. 20. Web Profiler in action
  21. 21. Drupal Console Built on the Symfony console library Debug Your Code drupal routes:debug drupal router:debug [internal route name] Clear Cache drupal router:rebuild Generate Code drupal generate:module drupal generate:controller drupal generate:entity:config drupal generate:form
  22. 22. Demo
  23. 23. Zivtech Rocks My Socks (demo site)
  24. 24. Socks Module
  25. 25. Module & Basic Controller: drupal generate:module generate:controller
  26. 26. Config Entity : drupal generate:entity:config
  27. 27. Form : drupal generate:form
  28. 28. Let’s Break Some Things !
  29. 29. Problem: Class Not Found
  30. 30. PHPStorm PHP IDE Integrated Development Environment by Jetbrains Code hinting Auto completion Breakpoints Built in Drupal settings support
  31. 31. Controller Goal: Display a basic page namespace DrupalsocksController; use DrupalCoreControllerControllerBase; class SockController extends ControllerBase { public function content() { $sockContent = Drupal::service('socks.sock_content'); $somethings = $sockContent->displaySomethings(); return [ '#type' => 'markup', '#markup' => t($somethings) ]; } } socks.sock_controller_content: path: 'socks' defaults: _controller: 'DrupalsocksControllerSockController::content' _title: 'All the Socks' requirements: _permission: 'access content' socks/src/Controller/SockController.php socks/socks.routing.yml
  32. 32. Controller namespace DrupalsocksController; class SockController extends ControllerBase { public function content() { $sockContent = Drupal::service('socks.sock_content'); $somethings = $sockContent->displaySomethings(); return [ '#type' => 'markup', '#markup' => t($somethings) ]; } } Problem Solution
  33. 33. Problem: Accessing Private and Protected Methods
  34. 34. Goal: Create a configurable entity type of Sock namespace DrupalsocksEntity; use DrupalCoreConfigEntityConfigEntityBase; class Sock extends ConfigEntityBase { public $id; public $uuid; public $label; public $description; public $fabric; public $rating; } socks/Entity/Sock.php class SockListBuilder extends ConfigEntityListBuilder { public function buildRow(EntityInterface $entity) { $row['id'] = $entity->id(); $row['description'] = $entity->description; $row['fabric'] = $entity->fabric; $row['rating'] = $entity->rating; return $row + parent::buildRow($entity); } } socks/src/Controller/SockListBuilder.php Config Entity
  35. 35. class Sock extends ConfigEntityBase { /** * The sock's rating. * @var string */ protected $rating; } public function buildRow(EntityInterface $entity) { $row['id'] = $entity->id(); $row['description'] = $entity->description; $row['fabric'] = $entity->fabric; $row['rating'] = $entity->rating; return $row + parent::buildRow($entity); } PROBLEM Config Entity
  36. 36. SOLUTION protected $rating; /** * @return string */ public function getRating() { return $this->rating; } /** * @param string $rating */ public function setRating($rating) { $this->rating = $rating; } public function buildRow(EntityInterface $entity) { $row['id'] = $entity->id(); $row['description'] = $entity->description; $row['fabric'] = $entity->fabric; $row['rating'] = $entity->getRating(); return $row + parent::buildRow($entity); } Sock Entity Sock List Builder Accessing a protected property by creating methods
  37. 37. Problem: Syntax Error
  38. 38. Xdebug Allows script execution to be paused at any point to read through variables Support stack and execution traces in error messages Profiling to find performance bottlenecks https://zivtech.gitbooks.io/zrms/content/xdebug.html admin/reports/status/php
  39. 39. public function submitForm(array &$form, FormStateInterface $form_state) { $result = $form_state->getValue('fav_sock'); drupal_set_message($this->t('Your favorite sock is @fav_sock', array('@fav_sock' => $result))); if ($result == 'Ankle Biters') { $form_state->setRedirect('socks.knee_highs_controller_content'); } else { if ($result == 'Old Fashions') { $form_state->setRedirect('socks.old_fashions_controller_content'); } else { $form_state->setRedirect('socks.knee_highs_controller_content'); } } } Form Goal: Let users pick their favorite sock class FavoriteSockForm extends FormBase { public function getFormId() { return 'favorite_sock_form'; } public function buildForm(array $form, FormStateInterface $form_state) { $form['fav_sock'] = array( '#type' => 'radios', '#options' => array( 'Ankle Biters' => $this->t('Ankle Biters'), 'Old Fashions' => $this->t('Old Fashions'), 'Knee Highs' => $this->t('Knee Highs') ), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); return $form; } socks/src/Form/FavoriteSockForm.php
  40. 40. Form class FavoriteSockForm extends FormBase { public function getFormId() { return 'favorite_sock_form'; } public function buildForm(array $form, FormStateInterface $form_state) { $form['fav_sock'] = array( '#type' => 'radios', '#options' => array( 'Ankle Biters' => $this->t('Ankle Biters'), 'Old Fashions' => $this->t('Old Fashions'), 'Knee Highs' => $this->t('Knee Highs') ), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); return $form; } public function submitForm(array &$form, FormStateInterface $form_state) { $result = $form_state['values']['fav_sock']; drupal_set_message($this->t('Your favorite sock is @fav_sock', array('@fav_sock' => $result))); if ($result == 'Ankle Biters') { $form_state->setRedirect('socks.knee_highs_controller_content'); } else { if ($result == 'Old Fashions') { $form_state->setRedirect('socks.old_fashions_controller_content'); } else { $form_state->setRedirect('socks.knee_highs_controller_content'); } } } socks/src/Form/FavoriteSockForm.php
  41. 41. Xdebug $form_state->getValue is an object, but the code is trying to access it as if it were an array. Change $form_state['values']['fav_sock'] to $form_state->getValue('fav_sock')
  42. 42. Debugging and QA does not stop at code completion.
  43. 43. Problem: Shared Staging Environment Client Approval Process
  44. 44. Probo CI Probo CI creates test environments for each new feature Visual representation of the project while development is in progress with Pull Request builds Code more confidently & Speed up approval process Continuous integration
  45. 45. Probo CI assets: - dev.sql.gz steps: - name: Example site setup plugin: Drupal database: dev.sql.gz databaseGzipped: true databaseUpdates: true revertFeatures: true clearCaches: true SOLUTION https://github.com/AllieRays/debugging-drupal-8/blob/probos/.probo.yaml
  46. 46. Problem: Cherry Picked Deployments
  47. 47. PROBLEM
  48. 48. Pull Requests with Probo CI SOLUTION https://github.com/AllieRays/debugging-drupal-8/pull/1 https://9365d60b-17fa-4834-bb2b-3bba252a14c6.probo.build/ || zivte.ch/1Ss473k
  49. 49. Gitbook of Configuration Steps: https://www.gitbook.com/book/zivtech/debug-tools-for-drupal8/details Github Repo of ZRMS and Module: https://github.com/AllieRays/debugging-drupal-8 Demo Site: http://zivte.ch/1Ss473k Slide Deck: http://zivte.ch/1VxWF60 Step 1: Logically think through as you develop. Step 2: Identify the goal/objective of your code. Step 3: Identify problem and use appropriate tools to solve your problem. Step 4: Fix and Test. Step 5: Test Again. Step 6: Success! Review
  50. 50. Gitbook of Configuration Steps: https://www.gitbook.com/book/zivte ch/debug-tools-for-drupal8/details
  51. 51. Thank You!

×