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 &$variables, $hook) {
if ($node = \Drupal::request()->attributes->get('node')) {
$variables['pageNodeType'] = $node->getType();
}
}
Then I print out the pageNodeType variable into CT-2's node.html.twig template like so...
{% if pageNodeType == 'TYPE' %}
<div>TEST</div>
{% 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()->headers->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