$npx -y skills add edutrul/drupal-ai --skill drupal-debuggingDrupal debugging techniques — Devel module, Drush watchdog, Twig debug, XDebug, error logging, and common troubleshooting patterns.
| 1 | # Drupal Debugging |
| 2 | |
| 3 | ## Drush Log Watching |
| 4 | |
| 5 | ```bash |
| 6 | # Watch all errors |
| 7 | ddev drush ws --severity=error |
| 8 | |
| 9 | # Last 20 entries |
| 10 | ddev drush ws --severity=error --count=20 |
| 11 | |
| 12 | # Watch all log levels |
| 13 | ddev drush ws --count=50 |
| 14 | |
| 15 | # Specific type |
| 16 | ddev drush ws --type=php --count=20 |
| 17 | ``` |
| 18 | |
| 19 | ## PHP Inline Debugging |
| 20 | |
| 21 | ```bash |
| 22 | # Run PHP in Drupal context |
| 23 | ddev drush php:eval "var_dump(\Drupal::config('my_module.settings')->getRawData());" |
| 24 | |
| 25 | # Check if service exists |
| 26 | ddev drush php:eval "var_dump(\Drupal::hasService('my_module.my_service'));" |
| 27 | |
| 28 | # Check class autoloading |
| 29 | ddev drush php:eval "class_exists('Drupal\my_module\Service\MyService') ? print 'Found' : print 'Not found';" |
| 30 | |
| 31 | # Inspect an entity |
| 32 | ddev drush php:eval " |
| 33 | \$node = \Drupal::entityTypeManager()->getStorage('node')->load(1); |
| 34 | print_r(\$node->toArray()); |
| 35 | " |
| 36 | ``` |
| 37 | |
| 38 | ## Twig Debugging |
| 39 | |
| 40 | Enable in `sites/default/services.yml`: |
| 41 | ```yaml |
| 42 | parameters: |
| 43 | twig.config: |
| 44 | debug: true |
| 45 | auto_reload: true |
| 46 | cache: false |
| 47 | ``` |
| 48 | |
| 49 | Then in templates: |
| 50 | ```twig |
| 51 | {{ dump(content) }} |
| 52 | {{ dump(node) }} |
| 53 | {{ dump(_context|keys) }} |
| 54 | ``` |
| 55 | |
| 56 | HTML comments in source will show template suggestions: |
| 57 | ```html |
| 58 | <!-- FILE NAME SUGGESTIONS: |
| 59 | * node--1--full.html.twig |
| 60 | * node--1.html.twig |
| 61 | * node--article--full.html.twig |
| 62 | x node--article.html.twig ← currently used |
| 63 | * node--full.html.twig |
| 64 | * node.html.twig |
| 65 | --> |
| 66 | ``` |
| 67 | |
| 68 | ## Devel Module |
| 69 | |
| 70 | ```bash |
| 71 | # Install devel |
| 72 | composer require drupal/devel |
| 73 | ddev drush en devel -y |
| 74 | ``` |
| 75 | |
| 76 | In PHP: |
| 77 | ```php |
| 78 | // Dump and die |
| 79 | dpm($variable); // Krumo output in message area |
| 80 | ksm($variable); // Krumo output (devel module) |
| 81 | dvm($variable); // Var dump |
| 82 | |
| 83 | // Krumo (in Twig via Devel) |
| 84 | // {{ devel_dump(node) }} |
| 85 | ``` |
| 86 | |
| 87 | ## Cache Debugging |
| 88 | |
| 89 | ```bash |
| 90 | # Clear all caches |
| 91 | ddev drush cr |
| 92 | |
| 93 | # Rebuild theme registry only |
| 94 | ddev drush php:eval "\Drupal::service('theme.registry')->reset();" |
| 95 | |
| 96 | # Check cache hit for a page (Drupal page cache) |
| 97 | # Look for X-Drupal-Cache: HIT in response headers |
| 98 | curl -I https://my-project.ddev.site/ |
| 99 | |
| 100 | # Check Big Pipe and render cache |
| 101 | ddev drush state:get drupal.maintenance_mode |
| 102 | ``` |
| 103 | |
| 104 | ## Service Container Debugging |
| 105 | |
| 106 | ```bash |
| 107 | # List all services matching a pattern |
| 108 | ddev drush devel:services | grep my_module |
| 109 | |
| 110 | # List all services |
| 111 | ddev drush devel:services |
| 112 | |
| 113 | # Check container definition |
| 114 | ddev drush php:eval " |
| 115 | \$def = \Drupal::getContainer()->getDefinition('my_module.my_service'); |
| 116 | var_dump(\$def); |
| 117 | " |
| 118 | ``` |
| 119 | |
| 120 | ## Module/Hook Debugging |
| 121 | |
| 122 | ```bash |
| 123 | # List all enabled modules |
| 124 | ddev drush pm:list --status=enabled |
| 125 | |
| 126 | # Check which modules implement a hook |
| 127 | ddev drush php:eval " |
| 128 | \$modules = \Drupal::moduleHandler()->getImplementations('form_alter'); |
| 129 | print implode(', ', \$modules); |
| 130 | " |
| 131 | |
| 132 | # Check module info |
| 133 | ddev drush pm:info my_module |
| 134 | ``` |
| 135 | |
| 136 | ## Common Problems and Solutions |
| 137 | |
| 138 | | Problem | Solution | |
| 139 | |---|---| |
| 140 | | Class not found | Check namespace, PSR-4 in .info.yml, run `composer dump-autoload` | |
| 141 | | Service not found | Check services.yml syntax, run `ddev drush cr` | |
| 142 | | Template not used | Enable Twig debug, check template suggestions in HTML comments | |
| 143 | | Hook not firing | Check class is registered in services.yml, verify Hook attribute | |
| 144 | | Config not saving | Check config schema exists in `config/schema/` | |
| 145 | | Migration failing | `ddev drush ws` for details, check field mappings | |
| 146 | |
| 147 | ## XDebug with DDEV |
| 148 | |
| 149 | ```bash |
| 150 | # Enable XDebug |
| 151 | ddev xdebug on |
| 152 | |
| 153 | # Disable XDebug (performance) |
| 154 | ddev xdebug off |
| 155 | |
| 156 | # Check XDebug status |
| 157 | ddev xdebug status |
| 158 | ``` |
| 159 | |
| 160 | Configure IDE (PHPStorm) to listen on port 9003. |