You are here

public function QuickNodeCloneNodeFinder::findNodeFromPath in Quick Node Clone 8

Derive node data from a given path.

Parameters

string $path: The drupal path, e.g. /node/2.

Return value

\Drupal\Core\Entity\EntityInterface|null Either returns an entity, or null if none found.

1 call to QuickNodeCloneNodeFinder::findNodeFromPath()
QuickNodeCloneNodeFinder::findNodeFromCurrentPath in src/QuickNodeCloneNodeFinder.php
Derive node data from the current path.

File

src/QuickNodeCloneNodeFinder.php, line 90

Class

QuickNodeCloneNodeFinder
Helper class.

Namespace

Drupal\quick_node_clone

Code

public function findNodeFromPath($path) {
  $entity = NULL;
  $type = 'node';

  // Check that the route pattern is an entity template.
  $parts = explode('/', $path);
  $i = 0;
  foreach ($parts as $part) {
    if (!empty($part)) {
      $i++;
    }
    if ($part == $type) {
      break;
    }
  }
  $i++;

  // Get entity path if alias.
  $entity_path = $this->aliasManager
    ->getPathByAlias($path);

  // Look! We're using arg() in Drupal 8 because we have to.
  $args = explode('/', $entity_path);
  if (isset($args[$i])) {
    $entity = $this->entityTypeManager
      ->getStorage($type)
      ->load($args[$i]);
  }
  if (isset($args[$i - 1]) && $args[$i - 1] != 'node') {
    $entity = $this->entityTypeManager
      ->getStorage($type)
      ->load($args[$i - 1]);
  }
  return $entity;
}