Verified Commit cd713185 authored by Lee Rowlands's avatar Lee Rowlands Committed by Lee Rowlands
Browse files

Issue #3034602 by larowlan, BetoAveiga, moshe weitzman, jonathan_hunt: Fatal...

Issue #3034602 by larowlan, BetoAveiga, moshe weitzman, jonathan_hunt: Fatal error on node/{nid}/media
parent 7e190269
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ services:
    arguments: ['@entity_field.manager', '@entity_type.bundle.info']
  access_check.entity_hierarchy.has_fields:
    class: Drupal\entity_hierarchy\Routing\ReorderChildrenAccess
    arguments: ['@entity_hierarchy.information.parent_candidate', '@current_route_match']
    arguments: ['@entity_hierarchy.information.parent_candidate', '@current_route_match', '@entity_type.manager']
    tags:
      - { name: access_check }
  entity_hierarchy.entity_tree_node_mapper:
+26 −1
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ namespace Drupal\entity_hierarchy\Routing;

use Drupal\Core\Access\AccessCheckInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_hierarchy\Information\ParentCandidateInterface;
@@ -29,6 +31,13 @@ class ReorderChildrenAccess implements AccessCheckInterface {
   */
  protected $parentCandidate;

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

  /**
   * Constructs a new ReorderChildrenAccess object.
   *
@@ -37,9 +46,10 @@ class ReorderChildrenAccess implements AccessCheckInterface {
   * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
   *   Route match.
   */
  public function __construct(ParentCandidateInterface $parentCandidate, RouteMatchInterface $routeMatch) {
  public function __construct(ParentCandidateInterface $parentCandidate, RouteMatchInterface $routeMatch, EntityTypeManagerInterface $entityTypeManager) {
    $this->routeMatch = $routeMatch;
    $this->parentCandidate = $parentCandidate;
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
@@ -65,6 +75,21 @@ class ReorderChildrenAccess implements AccessCheckInterface {
  public function access(Route $route, Request $request = NULL, AccountInterface $account = NULL) {
    $entity_type = $route->getOption(EntityHierarchyRouteProvider::ENTITY_HIERARCHY_ENTITY_TYPE);
    $entity = $this->routeMatch->getParameter($entity_type);

    // If a user has configured a local task with views at e.g.
    // node/{node}/media then the {node} object isn't upcast.
    // @todo revisit this when
    //   https://www.drupal.org/project/drupal/issues/2528166 is resolved
    if (!$entity instanceof EntityInterface) {
      $storage = $this->entityTypeManager->getStorage($entity_type);
      $entity = $storage->load($entity);
      if (!$entity) {
        // At this point we can't proceed so we err on the side of caution and
        // deny access to the tab.
        return AccessResult::forbidden();
      }
    }

    if ($entity && $this->parentCandidate->getCandidateFields($entity)) {
      return AccessResult::allowed()->setCacheMaxAge(0);
    }