debugging

DEBUGGING TIPS:

Drupal 7 Enabling php debug mode: (for Drupal you would put this in your settings.php)

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

Drupal 8 Enabling php debug mode: (settings.local.php or settings.php)

$config['system.logging']['error_level'] = 'verbose';

 

watchdog (Drupal 7)

//When you want to debug the $wtf variable, and dpm isn't doing it for you, then you should use this:

watchdog('michael_debug', '<pre>@print_r</pre>',
  array('@print_r' => print_r( $wtf, TRUE))
);

watchdog (Drupal 8/9)

\Drupal::logger('DEBUG')->notice('<pre>' . print_r($variable, TRUE) . '</pre>');

Drupal message (Drupal 8)

\Drupal::messenger()->addMessage('<pre>' . print_r($variable, TRUE) . '</pre>', 'warning', TRUE);

//Or my means of a 'service':

use Drupal\Core\Messenger;

\Drupal::service('messenger')->addMessage('test:' . print_r($form_id, TRUE), Messenger\MessengerInterface::TYPE_WARNING);

 

When you need a backtrace

Here is some backtrace code that might come in handy. The file should be created in the web root.

if ($fp = fopen('backtrace.log', 'w'))
{
   $bt = debug_backtrace();
   fwrite($fp, 'debug='.print_r($bt, true));
   fclose($fp);
}

Another approach if you want it in your browser:

$bt = debug_backtrace();

echo "<pre>" . print_r($bt, true) . "</pre>\n";

Here is an example of some output:

[4] => Array (
   [file] => sites/all/modules/contrib/path_breadcrumbs/path_breadcrumbs.module
   [line] => 135
   [function] => ctools_access
   [args] => Array (
      [0] => Array (
         [plugins] => Array (
            [0] => Array (
                [name] => entity_field_value:node:feed_item:field_news_category

 

Devel module kint() debug; video: 

 

 

Keeping a log

Using a function like this, you can keep appending a log. The log is best viewed using gedit on Linux or Notepad++ on windows where you will be prompted automatically to reload. When you're wanting to see a fresh log, then just select-all, delete and save.

function imgoingtosaythis($what_im_sayin) {
  if ($fp = fopen('saythis.txt', 'a'))
  {
      fwrite($fp, "$what_im_sayin\r\n");
      fclose($fp);
  }
}

 

Theme debugging: (Drupal 7)

$conf['theme_debug'] = TRUE;

this renders theme hook information into html source, do not use this in a production mode.

 

Theme debugging: (Drupal 8)

create a sites/default/services.yml from sites/default/default.services.yml

enable twig debugging (for templating hints in html source comments).

    # Not recommended in production environments
    # @default false
    debug: true

    # Twig auto-reload:
    # Not recommended in production environments
    # @default null
    auto_reload: true

Twig file variable debugging:

Regex:

https://regex101.com/

Drupal 8

\Drupal::messenger()->addMessage('<pre>' . print_r($variable, TRUE) . '</pre>', 'warning', TRUE);