QuickNodeCloneNodeFinder.php in Quick Node Clone 8
File
src/QuickNodeCloneNodeFinder.php
View source
<?php
namespace Drupal\quick_node_clone;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\path_alias\AliasManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class QuickNodeCloneNodeFinder {
protected $requestStack;
protected $aliasManager;
protected $entityTypeManager;
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'), $container
->get('path_alias.manager'), $container
->get('entity_type.manager'));
}
public function __construct(RequestStack $requestStack, AliasManager $aliasManager, EntityTypeManagerInterface $entityTypeManager) {
$this->requestStack = $requestStack;
$this->aliasManager = $aliasManager;
$this->entityTypeManager = $entityTypeManager;
}
public function findNodeFromCurrentPath() {
$path = $this->requestStack
->getCurrentRequest()
->getRequestUri();
$path_data = explode('/', $path);
if ($this
->currentPathIsValidClonePath()) {
$node_path = '/node/' . $path_data[2];
return $this
->findNodeFromPath($node_path);
}
return NULL;
}
public function findNodeFromPath($path) {
$entity = NULL;
$type = 'node';
$parts = explode('/', $path);
$i = 0;
foreach ($parts as $part) {
if (!empty($part)) {
$i++;
}
if ($part == $type) {
break;
}
}
$i++;
$entity_path = $this->aliasManager
->getPathByAlias($path);
$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;
}
public function getLinksByType($type) {
$entity_type = $this->entityTypeManager
->getDefinition($type);
return $entity_type
->getLinkTemplates();
}
public function currentPathIsValidClonePath() {
$path = $this->requestStack
->getCurrentRequest()
->getRequestUri();
$path_data = explode('/', $path);
if (!isset($path_data[1]) || $path_data[1] != 'clone') {
return FALSE;
}
if (!isset($path_data[2])) {
return FALSE;
}
if (!isset($path_data[3]) || $path_data[3] != 'quick_clone') {
return FALSE;
}
return TRUE;
}
}