Commit 15f4f99d authored by mxh's avatar mxh
Browse files

Issue #3256560 by mxh: Provide a generic "entity" context for the "view" stack

parent 6874a1eb
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -16,8 +16,9 @@ use Drupal\Core\Entity\EntityInterface;
function context_stack_entity_build_defaults_alter(array &$build, EntityInterface $entity, $view_mode) {
  $collection = ContextStackFactory::get()->createCollection();
  $entity_type_id = $entity->getEntityTypeId();
  $context = GenericEntityContext::fromEntity($entity, $entity->getEntityType()->getLabel());
  $collection->addContext($context, $entity_type_id);
  $label = $entity->getEntityType()->getLabel();
  $collection->addContext(GenericEntityContext::fromEntity($entity, $label), $entity_type_id);
  $collection->addContext(GenericEntityContext::fromEntity($entity, $label), 'entity');
  $build['#context_stack']['view'] = $collection;
  // Add cacheability from the context stack to the render array. This is done
  // by temporarily adding the collection to the stack, fetch its calculated
+13 −9
Original line number Diff line number Diff line
@@ -63,18 +63,22 @@ class ContextStackControllerWrapperSubscriber implements EventSubscriberInterfac
   */
  public function onKernelController(ControllerEvent $event) {
    $arguments = $this->argumentResolver->getArguments($event->getRequest(), $event->getController());
    if (!isset($this->contextCollections['view'])) {
      $collection = $this->contextStackFactory->createCollection();
      foreach ($arguments as $argument) {
        if ($argument instanceof EntityInterface) {
        $id = $argument->getEntityTypeId();
          $label = $argument->getEntityType()->getLabel();
          foreach ([$argument->getEntityTypeId(), 'entity'] as $id) {
            if (!$collection->hasContext($id)) {
          $collection->addContext(GenericEntityContext::fromEntity($argument, $argument->getEntityType()->getLabel()), $id);
              $collection->addContext(GenericEntityContext::fromEntity($argument, $label), $id);
            }
          }
        }
    if (!isset($this->contextCollections['view']) && $collection->getScope()) {
      }
      if ($collection->getScope()) {
        $this->contextCollections['view'] = $collection;
      }
    }

    if (!isset($this->contextCollections['account']) && ($user = \Drupal::entityTypeManager()->getStorage('user')->load(\Drupal::currentUser()->id()))) {
      $collection = $this->contextStackFactory->createCollection();