You are here

class HelperFunctions in Twig Template Suggester 8

Class HelperFunctions.

@package Drupal\twigsuggest\Utils

Hierarchy

Expanded class hierarchy of HelperFunctions

1 string reference to 'HelperFunctions'
twigsuggest.services.yml in ./twigsuggest.services.yml
twigsuggest.services.yml
1 service uses HelperFunctions
twigsuggest.helper_functions in ./twigsuggest.services.yml
Drupal\twigsuggest\Utils\HelperFunctions

File

src/Utils/HelperFunctions.php, line 15

Namespace

Drupal\twigsuggest\Utils
View source
class HelperFunctions {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * HelperFunctions constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match) {
    $this->entityTypeManager = $entity_type_manager;
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('current_route_match'));
  }

  /**
   * Get current node.
   *
   * Helper function to return current node no matter if viewing full node,
   * node preview or revision.
   *
   * @return bool|\Drupal\node\Entity\Node
   *   Returns the current node object or FALSE otherwise.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HelperFunctions::$entityTypeManager protected property The entity type manager.
HelperFunctions::$routeMatch protected property The route match.
HelperFunctions::create public static function
HelperFunctions::getCurrentNode public function Get current node.
HelperFunctions::__construct public function HelperFunctions constructor.