Page Content Type: Displaying error in Preview mode Whenever I am creating or editing a node, in the preview mode I see these errors.


  


This is the code I am using.

/**
 * Implements hook_preprocess_html().
 */
function campaigns2019_preprocess_html(&amp;$variables) {
  if (isset($variables['node_type']) &amp;&amp; $variables['node_type'] === 'landing_page') {
    $node = \Drupal::routeMatch()-&gt;getParameter('node');
    if ($node-&gt;field_dark_theme-&gt;value) {
      echo("&lt;script&gt;console.log('here');&lt;/script&gt;");
      $variables['attributes']['data-theme'] = 'dark';
    }
  }
}


The dark theme does not show up in preview mode though. However, if I ignore the errors and create my page, the dark theme works and everything seems to be displayed correctly. When is template_preprocess_html() called? Where in Drupal is it called?
The error is about accessing field_dark_theme from an un-existing node instance. The proper way of loading the node during preview is explained here.

On the other hand, welcome to the wonderful world of PHP for debugging properly (on Drupal), please check this post and this documentation entry check your proper IDE/stack on how to setup debugging tools.