When page is not node entity site breaks
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().