ParagraphBlocksEntityManager.php in Paragraph blocks 3.x
File
src/ParagraphBlocksEntityManager.php
View source
<?php
namespace Drupal\paragraph_blocks;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\path_alias\AliasManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class ParagraphBlocksEntityManager implements ContainerInjectionInterface {
protected $aliasManager;
protected $entityTypeManager;
protected $requestStack;
protected $configFactory;
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;
}
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'));
}
public function getEntity($path = NULL) {
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');
}
$source_path = \Drupal::service('path_alias.manager')
->getPathByAlias($path);
$source_path = preg_replace(":^{$base_path}:", '', $source_path);
$parts = explode('/', trim($source_path, '/'));
if (count($parts) >= 2) {
list($entity_type_id, $entity_id) = $parts;
$entity_type_id = str_replace('-', '_', $entity_type_id);
$entity_type_manager = \Drupal::entityTypeManager();
$entity_types = $entity_type_manager
->getDefinitions();
if (is_numeric($entity_id) && isset($entity_types[$entity_type_id])) {
$entity_storage = $entity_type_manager
->getStorage($entity_type_id);
if ($entity_storage) {
return $entity_storage
->load($entity_id);
}
}
}
return NULL;
}
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;
}
}