.../Drupal/Core/Entity/EntityStorageInterface.php | 2 +- .../modules/views/src/Plugin/views/area/Entity.php | 40 +++++++++++++++++++--- .../views/src/Tests/Handler/AreaEntityTest.php | 7 ++++ .../test_views/views.view.test_entity_area.yml | 4 ++- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/EntityStorageInterface.php b/core/lib/Drupal/Core/Entity/EntityStorageInterface.php index 12ae3b8..dc68983 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageInterface.php @@ -104,7 +104,7 @@ public function deleteRevision($revision_id); * An associative array where the keys are the property names and the * values are the values those properties must have. * - * @return array + * @return \Drupal\Core\Entity\EntityInterface[] * An array of entity objects indexed by their ids. */ public function loadByProperties(array $values = array()); diff --git a/core/modules/views/src/Plugin/views/area/Entity.php b/core/modules/views/src/Plugin/views/area/Entity.php index e1e9f75..d1ae19f 100644 --- a/core/modules/views/src/Plugin/views/area/Entity.php +++ b/core/modules/views/src/Plugin/views/area/Entity.php @@ -142,13 +142,11 @@ public function render($empty = FALSE) { if (!$empty || !empty($this->options['empty'])) { $entity_id_uuid = $this->tokenizeValue($this->options['entity_id_uuid']); - $entity_storage = $this->entityManager->getStorage($this->entityType); - $view_builder = $this->entityManager->getViewBuilder($this->entityType); - // Validate whether the ID is a UUID, in that case load the entity as UUID + // Validate whether the ID is a UUID, in that case load the entity by UUID // otherwise use the ID. - $is_uuid = Uuid::isValid($entity_id_uuid); - if ((!$is_uuid && $entity = $entity_storage->load($entity_id_uuid)) || ($entities = $entity_storage->loadByProperties(['uuid' => $entity_id_uuid])) && ($entity = reset($entities))) { + if ($entity = $this->loadEntityByIdOrUuid($entity_id_uuid)) { if (!empty($this->options['bypass_access']) || $entity->access('view')) { + $view_builder = $this->entityManager->getViewBuilder($this->entityType); return $view_builder->view($entity, $this->options['view_mode']); } } @@ -157,4 +155,36 @@ public function render($empty = FALSE) { return []; } + /** + * Loads an entity by ID or UUID. + * + * @param mixed $entity_id_uuid + * An entity ID or UUID. + * + * @return \Drupal\Core\Entity\EntityInterface|null + */ + protected function loadEntityByIdOrUuid($entity_id_uuid) { + $entity_storage = $this->entityManager->getStorage($this->entityType); + $is_uuid = Uuid::isValid($entity_id_uuid); + if ((!$is_uuid && $entity = $entity_storage->load($entity_id_uuid)) || ($entities = $entity_storage->loadByProperties(['uuid' => $entity_id_uuid])) && ($entity = reset($entities))) { + return $entity; + } + } + + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + $dependencies = parent::calculateDependencies(); + + // Ensure that we don't add dependencies for placeholders. + if (strpos($this->options['entity_id_uuid'], '!') === FALSE) { + if ($entity = $this->loadEntityByIdOrUuid($this->options['entity_id_uuid'])) { + $dependencies['content'][] = $this->entityType . ':' . $entity->bundle() . ':' . $entity->uuid(); + } + } + + return $dependencies; + } } diff --git a/core/modules/views/src/Tests/Handler/AreaEntityTest.php b/core/modules/views/src/Tests/Handler/AreaEntityTest.php index f68c8f0..bdd5658 100644 --- a/core/modules/views/src/Tests/Handler/AreaEntityTest.php +++ b/core/modules/views/src/Tests/Handler/AreaEntityTest.php @@ -79,9 +79,16 @@ public function testEntityArea() { $random_label = $this->randomMachineName(); $data = array('bundle' => 'entity_test', 'name' => $random_label); $entity_test = $this->container->get('entity.manager')->getStorage('entity_test')->create($data); + + $uuid_map[0] = 'aa0c61cb-b7bb-4795-972a-493dabcf529c'; + $uuid_map[1] = '62cef0ff-6f30-4f7a-b9d6-a8ed5a3a6bf3'; + $uuid_map[2] = '3161d6e9-3326-4719-b513-8fa68a731ba2'; + $entity_test->uuid->value = $uuid_map[$i]; + $entity_test->save(); $entities[] = $entity_test; \Drupal::state()->set('entity_test_entity_access.view.' . $entity_test->id(), $i != 2); + } $view = Views::getView('test_entity_area'); diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_entity_area.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_entity_area.yml index e46754a..50f23f6 100644 --- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_entity_area.yml +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_entity_area.yml @@ -1,6 +1,8 @@ langcode: und status: true -dependencies: { } +dependencies: + content: + - entity_test:entity_test:aa0c61cb-b7bb-4795-972a-493dabcf529c id: test_entity_area label: '' module: views