You are here

class QuickNodeCloneNodeFinder in Quick Node Clone 8

Helper class.

Hierarchy

Expanded class hierarchy of QuickNodeCloneNodeFinder

1 file declares its use of QuickNodeCloneNodeFinder
AddressEventSubscriber.php in src/EventSubscriber/AddressEventSubscriber.php
1 string reference to 'QuickNodeCloneNodeFinder'
quick_node_clone.services.yml in ./quick_node_clone.services.yml
quick_node_clone.services.yml
1 service uses QuickNodeCloneNodeFinder
quick_node_clone.node_finder in ./quick_node_clone.services.yml
Drupal\quick_node_clone\QuickNodeCloneNodeFinder

File

src/QuickNodeCloneNodeFinder.php, line 13

Namespace

Drupal\quick_node_clone
View source
class QuickNodeCloneNodeFinder {

  /**
   * Request Stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Path Alias Manager.
   *
   * @var \Drupal\Core\Path\AliasManager
   */
  protected $aliasManager;

  /**
   * The Entity Type Manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * QuickNodeCloneNodeFinder constructor.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   Request stack.
   * @param \Drupal\Core\Path\AliasManager $aliasManager
   *   Alias manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct(RequestStack $requestStack, AliasManager $aliasManager, EntityTypeManagerInterface $entityTypeManager) {
    $this->requestStack = $requestStack;
    $this->aliasManager = $aliasManager;
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Derive node data from the current path.
   *
   * @return \Drupal\Core\Entity\EntityInterface|null
   *   Either returns an entity, or null if none found.
   */
  public function findNodeFromCurrentPath() {
    $path = $this->requestStack
      ->getCurrentRequest()
      ->getRequestUri();
    $path_data = explode('/', $path);
    if ($this
      ->currentPathIsValidClonePath()) {

      // By this point, we should be on a quick node clone path.
      $node_path = '/node/' . $path_data[2];
      return $this
        ->findNodeFromPath($node_path);
    }
    return NULL;
  }

  /**
   * Derive node data from a given path.
   *
   * @param string $path
   *   The drupal path, e.g. /node/2.
   *
   * @return \Drupal\Core\Entity\EntityInterface|null
   *   Either returns an entity, or null if none found.
   */
  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;
  }

  /**
   * Get entity links, given an entity type.
   *
   * @param string $type
   *   The entity type.
   *
   * @return array|null
   *   An array of link templates, or null.
   */
  public function getLinksByType($type) {
    $entity_type = $this->entityTypeManager
      ->getDefinition($type);
    return $entity_type
      ->getLinkTemplates();
  }

  /**
   * Determine if the current page path is a valid quick node clone path.
   *
   * @return bool
   *   TRUE if valid, FALSE if invalid.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QuickNodeCloneNodeFinder::$aliasManager protected property Path Alias Manager.
QuickNodeCloneNodeFinder::$entityTypeManager protected property The Entity Type Manager.
QuickNodeCloneNodeFinder::$requestStack protected property Request Stack.
QuickNodeCloneNodeFinder::create public static function
QuickNodeCloneNodeFinder::currentPathIsValidClonePath public function Determine if the current page path is a valid quick node clone path.
QuickNodeCloneNodeFinder::findNodeFromCurrentPath public function Derive node data from the current path.
QuickNodeCloneNodeFinder::findNodeFromPath public function Derive node data from a given path.
QuickNodeCloneNodeFinder::getLinksByType public function Get entity links, given an entity type.
QuickNodeCloneNodeFinder::__construct public function QuickNodeCloneNodeFinder constructor.