Skip to main content
deleted 20 characters in body
Source Link
Yuckle
  • 136
  • 1
  • 13

Well, here's what I came up with... and it's working as desiredneeded. 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 &$variables, $hook) {

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

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

Well, here's what I came up with... and it's working as desired. 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 &$variables, $hook) {

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

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

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 &$variables, $hook) {

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

// Keeps the type available if an AJAX call happens 
if (\Drupal::routeMatch()->getRouteName() == 'views.ajax') {
  $previousUrl = \Drupal::request()->headers->get('referer');
  $fake_request = Request::create($previousUrl);
  $url_object = \Drupal::service('path.validator')->getUrlIfValid($fake_request->getRequestUri());
  if ($url_object) {
    $route_params = $url_object->getRouteParameters();
    $ent_id = $route_params['entity_id'];
    $node = \Drupal\node\Entity\Node::load($ent_id);
    $variables['pageNodeType'] = $node->getType();
  }
}
Post Undeleted by Yuckle
Post Deleted by Yuckle
Source Link
Yuckle
  • 136
  • 1
  • 13

Well, here's what I came up with... and it's working as desired. 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 &$variables, $hook) {

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

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