When page is not node entity site breaks How can correct this, my problem is that I'm using node label to set class. And everything works perfectly until I visit contact form or user login form which are not node entities, then site completely breaks, only on that two pages. How can I make some condition if it's not node or its contact form?

  if ($vars['node']-&gt;getType('location')) {
    $node = \Drupal::routeMatch()-&gt;getParameter('node');
    $title = $node-&gt;label();
    $vars['location'] = str_replace(' ', '-', strtolower($title));
  }
getType() takes no arguments.

What you want to do is:

  if (isset($vars['node']) &amp;&amp; $vars['node']-&gt;getType() == 'location') {
    $title = $vars['node']-&gt;label();
    $vars['location'] = str_replace(' ', '-', strtolower($title));
  }


That will check that the node object exists in the variables array, and getType returns location.

You could go a step further and ensure the type on the off chance that $vars['node'] could get set to anything not an instance of NodeInterface, which getType would not be available for and throw another error.

  if (isset($vars['node']) &amp;&amp; ($vars['node'] instanceof NodeInterface) &amp;&amp; $vars['node']-&gt;getType() == 'location') {
    $title = $vars['node']-&gt;label();
    $vars['location'] = str_replace(' ', '-', strtolower($title));
  }


Be sure to include NodeInterface with a use statement at the top of the file.

See more about getType().