You are here

class ParagraphBlocksEntityManager in Paragraph blocks 8

Same name and namespace in other branches
  1. 8.2 src/ParagraphBlocksEntityManager.php \Drupal\paragraph_blocks\ParagraphBlocksEntityManager
  2. 3.x src/ParagraphBlocksEntityManager.php \Drupal\paragraph_blocks\ParagraphBlocksEntityManager

Class HcpCoreManager.

Hierarchy

Expanded class hierarchy of ParagraphBlocksEntityManager

1 file declares its use of ParagraphBlocksEntityManager
ParagraphBlocksPanelsIpeManager.php in src/ParagraphBlocksPanelsIpeManager.php
1 string reference to 'ParagraphBlocksEntityManager'
paragraph_blocks.services.yml in ./paragraph_blocks.services.yml
paragraph_blocks.services.yml
1 service uses ParagraphBlocksEntityManager
paragraph_blocks.entity_manager in ./paragraph_blocks.services.yml
Drupal\paragraph_blocks\ParagraphBlocksEntityManager

File

src/ParagraphBlocksEntityManager.php, line 16

Namespace

Drupal\paragraph_blocks
View source
class ParagraphBlocksEntityManager implements ContainerInjectionInterface {

  /**
   * The path alias manager.
   *
   * @var \Drupal\Core\Path\AliasManagerInterface
   */
  protected $aliasManager;

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

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /*
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * HcpCoreManager constructor.
   */
  public function __construct(AliasManagerInterface $alias_manager, EntityTypeManagerInterface $entity_type_manager, RequestStack $request_stack, ConfigFactoryInterface $config_factory) {
    $this->aliasManager = $alias_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->requestStack = $request_stack;
    $this->configFactory = $config_factory;
  }

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

  /**
   * Return the entity of the path.
   *
   * @param string $path
   *   (optional) The path to lookup, defaults to the current path.
   *
   * @return \Drupal\Core\Entity\EntityInterface|null
   *   The entity of the current path, or null if none.
   *
   * @todo: This implements something similar to menu_get_object() which was
   * likely removed intentionally, so is this a bad idea?
   *
   * @see entity_css_get_entity()
   */
  public function getEntity($path = NULL) {

    // Get the current path if none is specified.
    if (!$path) {
      $path = parse_url(\Drupal::requestStack()
        ->getCurrentRequest()
        ->getRequestUri(), PHP_URL_PATH);
    }
    $base_path = base_path();
    if (empty($path) || $path == $base_path || $path == '<front>') {
      $path = \Drupal::config('system.site')
        ->get('page.front');
    }

    // Convert the specified path to an internal path.
    $source_path = \Drupal::service('path.alias_manager')
      ->getPathByAlias($path);
    $source_path = preg_replace(":^{$base_path}:", '', $source_path);

    // Check if this is the entity path.
    // @todo: this makes assumption that are possibly not valid. For example, is
    // the entity path always "/entity_type_id/entity_id"?
    $parts = explode('/', trim($source_path, '/'));
    if (count($parts) >= 2) {
      list($entity_type_id, $entity_id) = $parts;
      $entity_type_id = str_replace('-', '_', $entity_type_id);

      // Check that the path is for an entity.
      $entity_type_manager = \Drupal::entityTypeManager();

      /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
      $entity_types = $entity_type_manager
        ->getDefinitions();
      if (is_numeric($entity_id) && isset($entity_types[$entity_type_id])) {

        /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
        $entity_storage = $entity_type_manager
          ->getStorage($entity_type_id);
        if ($entity_storage) {

          // Load and return the entity.
          return $entity_storage
            ->load($entity_id);
        }
      }
    }
    return NULL;
  }

  /**
   * Return the entity of the referer, useful when called via AJAX.
   *
   * @return \Drupal\Core\Entity\EntityInterface|null
   *   The entity of the referer, or null if none.
   */
  public function getRefererEntity() {
    $referer = $this->requestStack
      ->getCurrentRequest()->server
      ->get('HTTP_REFERER');
    if ($referer) {
      $path = parse_url($referer, PHP_URL_PATH);
      if ($path) {
        return $this
          ->getEntity($path);
      }
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParagraphBlocksEntityManager::$aliasManager protected property The path alias manager.
ParagraphBlocksEntityManager::$configFactory protected property
ParagraphBlocksEntityManager::$entityTypeManager protected property The entity type manager.
ParagraphBlocksEntityManager::$requestStack protected property The request stack.
ParagraphBlocksEntityManager::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ParagraphBlocksEntityManager::getEntity public function Return the entity of the path.
ParagraphBlocksEntityManager::getRefererEntity public function Return the entity of the referer, useful when called via AJAX.
ParagraphBlocksEntityManager::__construct public function HcpCoreManager constructor.