【Drupal】デバッグモードを有効にする

こちらのサイトではDRUPAL10に関連する記事を掲載しています。

この記事はこちらのサイトから引用させていただきました

Let’s debug in Drupal 8 ! · Blog · Liip
Let's see how to develop and debug Drupal 8 projects (theming, profiling, command line tools etc.).

キャッシュの無効化

まず第一に、何千ものdrupal crヒットを伴うクレイジーな端末を避けるために、開発中にDrupalキャッシングを無効にすることができます。 sites/example.settings.local.phpファイルをコピーしてsites/default/settings.local.phpに名前を変更する必要があります。 次に、いくつかの値のコメントを解除/更新します。

  • uncomment this line to enable the “null cache service”:
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
  • uncomment these lines to disable CSS/JS aggregation:
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
  • uncomment these lines to disable the render cache and the dynamic page cache:
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
  • you can allow test modules and themes to be installed if needed with:
$settings['extension_discovery_scan_tests'] = TRUE;

このファイルをDrupalの設定ファイルの一部として含めるには、sites / default / settings.phpファイルを開き、次の行のコメントを解除します。

if (file_exists(__DIR__ . '/settings.local.php')) {
    include __DIR__ . '/settings.local.php';
}

次に、Twigキャッシュを無効にするには、sites / development.services.ymlファイルを開き、次の設定を追加します:

parameters:
    twig.config:
        debug: true
        auto_reload: true
        cache: false

Finally, rebuild the Drupal cache and it is done !

エラーメッセージの表示

Drupal 7では、さまざまなレベルのエラー表示を設定できます (by visiting this page: /admin/config/development/logging in the administration interface):

  • None
  • Errors and warnings
  • All

Drupal 8には、「バックトレース情報を含むすべてのメッセージ」と呼ばれる第4レベルがあります。 これはDrupalコアにネイティブであり、メッセージ領域にエラーバックトレースを表示できます。

You can also adjust the level of errors in your local setting file:

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

ログの出力

Drupal 7の開発者は、有名なwatchdog()関数を使用してデータベースにメッセージを記録できるデータベースロギングモジュールを知っています。

このモジュールはまだDrupal8に存在しますが、関数にはDrupal8ロガークラスの代わりがあります。

It looks like this:

// Logs an error
\Drupal::logger('my_module')->error($message);

Let’s have a look at the different parts of the code:

  • \Drupal::logger(‘my_module’) is the helper method that quickly creates a copy of the logger service. As a parameter, it takes the module name from where we log the information;
  • ->error:this is the severity-level method (it can be debug, info, notice, warning, error, critical, alert, emergency);
  • $message is the log message. It can be a simple string or it can contain some placeholders. In this case, you can pass the associative array (placeholders as keys) as a second parameter.

All the messages created with the logger service can be viewed in the reports page of the administration interface in /admin/reports/dblog as it used to be in Drupal 7.

Twigテンプレートのデバッグ

Twigテンプレートのデバッグモード

Drupalコアには、ローカル環境に非常に役立つテーマデバッグモードが付属しています。 これを有効にするには、次のコードをコピーしてsites / default / services.ymlファイルに貼り付けます(development.services.ymlを追加している場合は、development.services.ymlファイルでもOK):

parameters:
    twig.config:
        debug: true

このモードのおかげで、HTMLコードのどの部分がどのテンプレートに記述されているかを簡単に見つけることができます。ソースコードでは、Twigテンプレートの各部分(ページ、ノード、ブロック、メニューなど)が囲まれています。 一致する提案テンプレートを含むHTMLコメント。 現在使用中のものがチェックされます。

Make sure to enable comments in your web browser debug tab and also note that this feature has been backported to Drupal 7.

Used templates mentioned in HTML comments

Inside a Twig template, you will also be able to use {{ dump(my_variable) }} syntax to print a variable content.

If you have Devel Kint module installed, you can use {{ kint(my_variable) }} to dump the variable in a nice formatted structure: you can hide/show levels of arrays/objects which is very helpful as Drupal variables can have many levels inside. Kint is indeed the successor of the krumo() function from Drupal 7.

Print content_attributes variable with kint from node.html.twig template

Twigテンプレートの自動リロード

  • ソースコードが変更されるたびに、Twig テンプレートを自動的に再コンパイルします。 twig_auto_reload の値を指定しない場合、twig_debug の値に基づいて決定されます。
  • デバッグではなく auto_reload を特に必要としない限り、この設定を変更する必要はありません。 上記でデバッグを有効にするだけです。
parameters:
  http.response.debug_cacheability_headers: true
  twig.config:
    debug: true
    auto_reload: true

Twigテンプレートのキャッシュ機能について

  • ソースコードが変更されるたびに、Twig テンプレートを自動的に再コンパイルします。 twig_auto_reload の値を指定しない場合、twig_debug の値に基づいて決定されます。
  • デバッグではなく auto_reload を特に必要としない限り、この設定を変更する必要はありません。 上記でデバッグを有効にするだけです。
parameters:
  http.response.debug_cacheability_headers: true
  twig.config:
    debug: true
    auto_reload: true

Twigテンプレートのキャッシュ機能

先程の、YAMLファイルのParameters、twig.configのセッションにcache: falseを追加するとキャッシュ機能をオフにできますが、これは不要です。

特定の使用例がない限り、Twig キャッシュを無効にしないでください。 Twig デバッグを有効にすると (または、何らかの理由でデバッグを有効にしたくない場合は auto_reload だけを有効にすると)、Twig キャッシュは邪魔になりません。 Twig キャッシュを無効にすると、編集されているかどうかに関係なく各テンプレートをコンパイルする必要があるため、開発エクスペリエンスが遅くなるだけです。

Debugging Twig templates
The Twig template engine offers a debug tool. The Drupal 8 implementation also adds an additional tool that allows you to locate the template that outputs the m...
Debugging compiled Twig templates
How Twig in Drupal normally works By default, the Twig theming engine compiles templates into PHP code and stores the compiled template code in the filesystem (...

プロファイリングページ

パフォーマンスの問題に対処する場合、Webサイトのプロファイリングは、根本的な原因を見つけるのに役立ちます。

よく知られているデバッグモジュールDevelは、最新バージョンでWebprofilerを提供しています。 これは実際にはSymfonyプロファイラーの(部分的な)ポートであり、次のような便利なデータコレクターを備えたフッターバーをすべてのページに表示します。

  • Drupal current version;
  • PHP configuration (current version, loaded modules);
  • route and controller name;
  • page load timeline and memory use;
  • query time and number of queries;
  • number of blocks loaded and rendered;
  • number of views;
  • number of modules and themes available;
  • cache statistics.

Webprofiler bar in Drupal 8

各セクションをクリックすると、収集されたデータの詳細が記載された特定のページにリダイレクトされます。 たとえば、ページリクエストの詳細を見ると、一致するルート、渡されたパラメータを持つルートオブジェクト、応答ヘッダーなどがわかります。まあ、リクエストのデバッグに必要なすべての情報がわかります。

Details of a page request provided by Webprofiler

コマンドラインツールDRUSH

The Drupal Console project is a powerful command line tool that makes use of the Symfony Console and other third-party components. It is complementary to Drush and allows you to generate code to build modules and themes (code scaffolding), to interact with your Drupal installation and help to debug your code.

Once the Drupal Console installed, you can run the drupal list command to show all available commands.

Here are some useful commands for debug:

  • drupal check: check system requirements;
  • drupal site:status: show current Drupal installation status (Versions of Drupal, PHP, MySQL and libraries, updates status, cron last run, database connection etc.);
  • drupal database:table:debug: show all tables of the database;
  • drupal database:table:debug my_table: show columns of my_table table;
  • drupal config:debug: list all configurations;
  • drupal config:debug image.settings: show configuration for image.settings;
  • drupal config:settings:debug: display current key:value from the settings file;
  • drupal container:debug: display all services ID with the matching class name;
  • drupal router:debug: display all route names with the matching paths;
  • drupal router:debug dblog.overview: display route information about dblog overview page;
  • drupal database:log:debug: display current log events;
  • drupal database:log:debug 107: display one log event in details;
  • drupal site:statistics: show some statistics about the website (number of modules enabled/disabled, number of users and comments etc.).

The Drupal Console is already used by many companies well known in the Drupal community such as Acquia, Amazee Labs, Phase2 and Commerce Guys and is becoming the standard command line tool for Drupal 8.

But note that the current version is not yet fully compatible with Drupal 8.1.x, for instance, there are still some issues with migration commands. The team of the project is currently looking for some financial support and more contributors to get a full stable release.

For more information about the project, check out the official website and the documentation.

このサイトに関するご意見・ご質問はこちらまで

この記事またはDrupalに関するご質問がございましたら、お気軽にお問い合わせください。

タイトルとURLをコピーしました