You are here

public function HelperFunctions::getCurrentNode in Twig Template Suggester 8

Get current node.

Helper function to return current node no matter if viewing full node, node preview or revision.

Return value

bool|\Drupal\node\Entity\Node Returns the current node object or FALSE otherwise.

File

src/Utils/HelperFunctions.php, line 63

Class

HelperFunctions
Class HelperFunctions.

Namespace

Drupal\twigsuggest\Utils

Code

public function getCurrentNode() {
  $node = FALSE;
  if ($this->routeMatch
    ->getRouteName() == 'entity.node.canonical') {
    $node = $this->routeMatch
      ->getParameter('node');
  }
  elseif ($this->routeMatch
    ->getRouteName() == 'entity.node.revision') {

    // @todo https://www.drupal.org/i/2730631 will allow to use the upcasted
    //   node revision object.
    $node_revision = $this->routeMatch
      ->getParameter('node_revision');
    if ($node_revision instanceof NodeInterface) {
      $node = $node_revision;
    }
    elseif ($node_revision) {
      $node = $this->entityTypeManager
        ->getStorage('node')
        ->loadRevision($node_revision);
    }
  }
  elseif ($this->routeMatch
    ->getRouteName() == 'entity.node.preview') {
    $node = $this->routeMatch
      ->getParameter('node_preview');
  }
  return $node;
}