Commit faf79de6 authored by akoe's avatar akoe Committed by Vaibhav Jain
Browse files

Issue #3189181 by akoe: Language path prefix breaks method...

Issue #3189181 by akoe: Language path prefix breaks method ParagraphBlocksEntityManager->getRefererEntity
parent 4cd1223e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ services:
      - { name: event_subscriber }
  paragraph_blocks.entity_manager:
    class: Drupal\paragraph_blocks\ParagraphBlocksEntityManager
    arguments: ['@path_alias.manager', '@entity_type.manager', '@request_stack', '@config.factory']
    arguments: ['@request_stack']
  paragraph_blocks.labeller:
    class: Drupal\paragraph_blocks\ParagraphBlocksLabeller
    arguments: ['@paragraph_blocks.entity_manager', '@entity_field.manager']
+10 −77
Original line number Diff line number Diff line
@@ -2,10 +2,7 @@

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;

@@ -35,21 +32,11 @@ class ParagraphBlocksEntityManager implements ContainerInjectionInterface {
   */
  protected $requestStack;

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

  /**
   * ParagraphBlocksEntityManager 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;
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
    $this->configFactory = $config_factory;
  }

  /**
@@ -57,79 +44,25 @@ class ParagraphBlocksEntityManager implements ContainerInjectionInterface {
   */
  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')
      $container->get('request_stack')
    );
  }

  /**
   * 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 the entity from Layout Builders section storage in request.
   *
   * @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);
      }
    /** @var \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage $section_storage */
    $section_storage = $this->requestStack->getCurrentRequest()->attributes->get('section_storage');

    // If section storage is for a single entity override there will be a entity to return.
    if ('overrides' === $section_storage->getPluginId()) {
      return $section_storage->getContext('entity')->getContextData()->getEntity();
    }
    return NULL;
    return null;
  }

}