diff --git a/core/lib/Drupal/Core/Config/Entity/Query/Query.php b/core/lib/Drupal/Core/Config/Entity/Query/Query.php index 7332680..720b67e 100644 --- a/core/lib/Drupal/Core/Config/Entity/Query/Query.php +++ b/core/lib/Drupal/Core/Config/Entity/Query/Query.php @@ -77,6 +77,10 @@ public function condition($property, $value = NULL, $operator = NULL, $langcode * Implements \Drupal\Core\Entity\Query\QueryInterface::execute(). */ public function execute() { + $return = parent::execute(); + if (isset($return)) { + return $return; + } // Load the relevant config records. $configs = $this->loadRecords(); diff --git a/core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php b/core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php index d35466f..c564e97 100644 --- a/core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php +++ b/core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php @@ -45,6 +45,10 @@ public function __construct(EntityTypeInterface $entity_type, $conjunction, arra * {@inheritdoc} */ public function execute() { + $return = parent::execute(); + if (isset($return)) { + return $return; + } // Load the relevant records. $records = $this->keyValueFactory->get('entity_storage__' . $this->entityTypeId)->getAll(); diff --git a/core/lib/Drupal/Core/Entity/Query/QueryBase.php b/core/lib/Drupal/Core/Entity/Query/QueryBase.php index f5d36a1..e0b9d4d 100644 --- a/core/lib/Drupal/Core/Entity/Query/QueryBase.php +++ b/core/lib/Drupal/Core/Entity/Query/QueryBase.php @@ -108,6 +108,13 @@ protected $accessCheck = TRUE; /** + * Whether this query should return result or not. Defaults to FALSE. + * + * @var bool + */ + protected $dead = FALSE; + + /** * Flag indicating whether to query the current revision or all revisions. * * Can be either EntityStorageInterface::FIELD_LOAD_CURRENT or @@ -257,6 +264,14 @@ public function accessCheck($access_check = TRUE) { } /** + * {@inheritdoc} + */ + public function kill() { + $this->dead = TRUE; + return $this; + } + + /** * Implements \Drupal\Core\Entity\Query\QueryInterface::age(). */ public function age($age = EntityStorageInterface::FIELD_LOAD_CURRENT) { @@ -265,6 +280,15 @@ public function age($age = EntityStorageInterface::FIELD_LOAD_CURRENT) { } /** + * {@inheritdoc} + */ + public function execute() { + if ($this->dead) { + return $this->count ? 0 : []; + } + } + + /** * Implements \Drupal\Core\Entity\Query\QueryInterface::pager(). */ public function pager($limit = 10, $element = NULL) { diff --git a/core/lib/Drupal/Core/Entity/Query/QueryInterface.php b/core/lib/Drupal/Core/Entity/Query/QueryInterface.php index 0fad28c..9580971 100644 --- a/core/lib/Drupal/Core/Entity/Query/QueryInterface.php +++ b/core/lib/Drupal/Core/Entity/Query/QueryInterface.php @@ -166,6 +166,17 @@ public function tableSort(&$headers); public function accessCheck($access_check = TRUE); /** + * Kills the query + * + * If module want to return an empty result for a query it may use this + * method. + * + * @return \Drupal\Core\Entity\Query\QueryInterface + * The called object. + */ + public function kill(); + + /** * Queries the current or every revision. * * Note that this only affects field conditions. Property conditions always diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php index cf8eb39..9ad0654 100644 --- a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php +++ b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php @@ -82,6 +82,10 @@ public function __construct(EntityTypeInterface $entity_type, $conjunction, Conn * Implements \Drupal\Core\Entity\Query\QueryInterface::execute(). */ public function execute() { + $return = parent::execute(); + if (isset($return)) { + return $return; + } return $this ->prepare() ->compile() diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/QueryAggregate.php b/core/lib/Drupal/Core/Entity/Query/Sql/QueryAggregate.php index c76a773..a704a2e 100644 --- a/core/lib/Drupal/Core/Entity/Query/Sql/QueryAggregate.php +++ b/core/lib/Drupal/Core/Entity/Query/Sql/QueryAggregate.php @@ -26,6 +26,10 @@ class QueryAggregate extends Query implements QueryAggregateInterface { * Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::execute(). */ public function execute() { + $return = parent::execute(); + if (isset($return)) { + return $return; + } return $this ->prepare() ->addAggregate() diff --git a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php index e71d04e..e42afa1 100644 --- a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php +++ b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php @@ -35,6 +35,10 @@ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) { $query->condition('status', NODE_PUBLISHED); } + // If user can't access the content kill the query. + if (!$this->currentUser->hasPermission('access content')) { + $query->kill(); + } return $query; } diff --git a/core/modules/system/src/Tests/Entity/EntityQueryTest.php b/core/modules/system/src/Tests/Entity/EntityQueryTest.php index 2620b42..6bbdeae 100644 --- a/core/modules/system/src/Tests/Entity/EntityQueryTest.php +++ b/core/modules/system/src/Tests/Entity/EntityQueryTest.php @@ -272,6 +272,19 @@ function testEntityQuery() { ->execute(); // Now we get everything. $this->assertIdentical($results, $assert); + // Test kill method. + $results = $this->factory->get('entity_test_mulrev') + ->condition("$greetings.value", 'merhaba') + ->kill() + ->execute(); + $this->assertIdentical($results, []); + // Test kill method. + $results = $this->factory->get('entity_test_mulrev') + ->condition("$greetings.value", 'merhaba') + ->count() + ->kill() + ->execute(); + $this->assertIdentical($results, 0); } /** diff --git a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php index c69fb87..ba4dd61 100644 --- a/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php +++ b/core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php @@ -11,6 +11,7 @@ use Drupal\Core\Language\LanguageInterface; use Drupal\comment\CommentInterface; use Drupal\simpletest\WebTestBase; +use Drupal\user\Entity\Role; /** * Tests for the base handlers provided by Entity Reference. @@ -111,6 +112,11 @@ public function testNodeHandler() { // Test as a user which is not able to access content. $no_access_user = $this->drupalCreateUser(); + $rids = $no_access_user->getRoles(); + $role = Role::load(reset($rids)); + // Make sure user doesn't have access content permission. + $role->revokePermission('access content') + ->save(); \Drupal::currentUser()->setAccount($no_access_user); $referenceable_tests = array( array(