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/

 

Find tables modified since an interval of time or recently modified tables with MySQL

select table_schema as database_name,
       table_name,
       update_time
from information_schema.tables tab
where update_time > (current_timestamp() - interval 30 day)
      and table_type = 'BASE TABLE'
      and table_schema not in ('information_schema', 'sys',
                               'performance_schema','mysql')
      -- and table_schema = 'your database name' 
order by update_time desc;

 

Drupal 8

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

 

Debugging a views query that is working but not crashing.

/**
 * Implements hook_views_post_execute() (for debugging purposes).
 */
function MONMODULE_views_post_execute(ViewExecutable $view) {
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler->moduleExists('devel')) {
    if ($view->id() != 'content') {
      return;
    }
    dpq($view->query->query());
  }
}

 

Debugging a views query that throws an error and crashes, this is how to see what the real query looks liks.

/**
 * Implements hook_views_pre_execute() (for debugging purposes).
 */
function MYMODULE_views_pre_execute(ViewExecutable $view) {
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler->moduleExists('devel')) {
    if ($view->id() != 'content') {
      return;
    }
    echo print_r($view->query->query()->__toString(), TRUE);
//    dpq($view->query->query());
    die;
  }
}

import .po files with drush:

cd _DRUPAL_ROOT_;
drush locale:import fr ../translations/projectname.po