Need a current page's "Content Type" passed to a node.html.twig template (for a different content type) on page load, AND after ajax call I'm trying to get the Content Type value of a node page (that the user is currently visiting - let's call it CT-1)... and feed that value as a variable into a DIFFERENT content type's node.html.twig template (CT-2). This CT-2 template is being used in a View output, displaying on a CT-1 page. Here's my code...

function custom_mods_preprocess_node(array &amp;$variables, $hook) {
   if ($node = \Drupal::request()-&gt;attributes-&gt;get('node')) {
     $variables['pageNodeType'] = $node-&gt;getType();
   }
 }


Then I print out the pageNodeType variable into CT-2's node.html.twig template like so...

{% if pageNodeType == 'TYPE' %}
&lt;div&gt;TEST&lt;/div&gt;
{% endif %}


This all works perfectly on page load. But when the view output is filtered with AJAX (via an exposed filter), pageNodeType becomes NULL, and therefore the "if statement" breaks with the CT-2 template. 

What can I do to keep that variable active after an ajax update?

I tried leveraging..

\Drupal::request()-&gt;headers-&gt;get('referer');


..to pull the previous page, but it seemed like quite a task to get the Content Type via this approach. Is there a better way. 

Thanks!!

Edit: May be related to this post as well... but no answers? Views with ajax get current node in hook_views_query_alter
Well, here's what I came up with... and it's working as needed. If there are issues with this approach, please feel free to critique. My backend skills leave much to be desired. Thanks!

function custom_mods_preprocess_node(array &amp;$variables, $hook) {

// Get the current page's content type on page load
$node = \Drupal::routeMatch()-&gt;getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
  $variables['pageNodeType'] = $node-&gt;getType();
}

// Keeps the type available if an AJAX call happens 
if (\Drupal::routeMatch()-&gt;getRouteName() == 'views.ajax') {
  $previousUrl = \Drupal::request()-&gt;headers-&gt;get('referer');
  $fake_request = Request::create($previousUrl);
  $url_object = \Drupal::service('path.validator')-&gt;getUrlIfValid($fake_request-&gt;getRequestUri());
  if ($url_object) {
    $route_params = $url_object-&gt;getRouteParameters();
    $ent_id = $route_params['entity_id'];
    $node = \Drupal\node\Entity\Node::load($ent_id);
    $variables['pageNodeType'] = $node-&gt;getType();
  }
}