Drupal\Core\Entity\Query\QueryException: Entity queries must explicitly set whether the query should be access checked or not.

Created on 7 May 2024, 8 months ago
Updated 13 September 2024, 3 months ago

Problem/Motivation

Error on /admin/commerce

Message
Drupal\Core\Entity\Query\QueryException: Entity queries must explicitly set whether the query should be access checked or not. See Drupal\Core\Entity\Query\QueryInterface::accessCheck(). in Drupal\Core\Entity\Query\Sql\Query->prepare() (line 141 of /app/web/core/lib/Drupal/Core/Entity/Query/Sql/Query.php).

Operations:
#0 /app/web/core/lib/Drupal/Core/Entity/Query/Sql/Query.php(80): Drupal\Core\Entity\Query\Sql\Query->prepare()
#1 /app/web/modules/contrib/commerce_marketplace/src/MarketplaceStorage.php(49): Drupal\Core\Entity\Query\Sql\Query->execute()
#2 /app/web/modules/contrib/commerce/modules/store/src/Resolver/DefaultStoreResolver.php(33): Drupal\commerce_marketplace\MarketplaceStorage->loadDefault()
#3 /app/web/modules/contrib/commerce/modules/store/src/Resolver/ChainStoreResolver.php(46): Drupal\commerce_store\Resolver\DefaultStoreResolver->resolve()
#4 /app/web/modules/contrib/commerce/modules/store/src/CurrentStore.php(65): Drupal\commerce_store\Resolver\ChainStoreResolver->resolve()
#5 /app/web/modules/contrib/commerce/modules/store/src/Resolver/StoreCountryResolver.php(43): Drupal\commerce_store\CurrentStore->getStore()
#6 /app/web/modules/contrib/commerce/src/Resolver/ChainCountryResolver.php(46): Drupal\commerce_store\Resolver\StoreCountryResolver->resolve()
#7 /app/web/modules/contrib/commerce/src/CurrentCountry.php(62): Drupal\commerce\Resolver\ChainCountryResolver->resolve()
#8 /app/web/modules/contrib/commerce/src/Resolver/DefaultLocaleResolver.php(54): Drupal\commerce\CurrentCountry->getCountry()
#9 /app/web/modules/contrib/commerce/src/Resolver/ChainLocaleResolver.php(46): Drupal\commerce\Resolver\DefaultLocaleResolver->resolve()
#10 /app/web/modules/contrib/commerce/src/CurrentLocale.php(62): Drupal\commerce\Resolver\ChainLocaleResolver->resolve()
#11 /app/web/modules/contrib/commerce/modules/price/src/CurrencyFormatter.php(29): Drupal\commerce\CurrentLocale->getLocale()
#12 /app/web/core/lib/Drupal/Component/DependencyInjection/Container.php(259): Drupal\commerce_price\CurrencyFormatter->__construct(Object(Drupal\commerce_price\Repository\NumberFormatRepository), Object(Drupal\commerce_price\Repository\CurrencyRepository), Object(Drupal\commerce\CurrentLocale))
#13 /app/web/core/lib/Drupal/Component/DependencyInjection/Container.php(177): Drupal\Component\DependencyInjection\Container->createService(Array, 'commerce_price....')
#14 /app/web/modules/contrib/commerce/modules/order/src/Form/DashboardMetricsForm.php(79): Drupal\Component\DependencyInjection\Container->get('commerce_price....')
#15 /app/web/core/lib/Drupal/Core/DependencyInjection/ClassResolver.php(28): Drupal\commerce_order\Form\DashboardMetricsForm::create(Object(Drupal\Core\DependencyInjection\Container))
#16 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(195): Drupal\Core\DependencyInjection\ClassResolver->getInstanceFromDefinition('Drupal\\commerce...')
#17 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(232): Drupal\Core\Form\FormBuilder->getFormId('Drupal\\commerce...', Object(Drupal\Core\Form\FormState))
#18 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(224): Drupal\Core\Form\FormBuilder->buildForm('Drupal\\commerce...', Object(Drupal\Core\Form\FormState))
#19 /app/web/modules/contrib/commerce/modules/order/commerce_order.module(500): Drupal\Core\Form\FormBuilder->getForm('Drupal\\commerce...')
#20 /app/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(545): commerce_order_commerce_dashboard_page_build_alter(Array, NULL, NULL)
#21 /app/web/modules/contrib/hook_event_dispatcher/src/HookEventDispatcherModuleHandler.php(87): Drupal\Core\Extension\ModuleHandler->alter('commerce_dashbo...', Array, NULL, NULL)
#22 /app/web/modules/contrib/commerce/src/Controller/DashboardController.php(71): Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler->alter('commerce_dashbo...', Array)
#23 [internal function]: Drupal\commerce\Controller\DashboardController->dashboardPage()
#24 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#25 /app/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#26 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#27 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#28 /app/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#29 /app/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#30 /app/web/modules/contrib/bat_api/src/StackMiddleware/BatApiMiddleware.php(39): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#31 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Drupal\bat_api\StackMiddleware\BatApiMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#32 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#33 /app/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#34 /app/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#35 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#36 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#37 /app/web/modules/contrib/services/src/StackMiddleware/FormatSetter.php(45): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#38 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\services\StackMiddleware\FormatSetter->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#39 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#40 /app/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#41 /app/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#42 /app/web/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#43 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#44 {main}

Steps to reproduce

- Installed commerce_marketplace-3341967 fork on a Drupal 10.2.6 clean website
- drupal_commerce version: 8.x-2.38

Proposed resolution

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇷🇴Romania alexberendei

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @alexberendei
  • 🇷🇴Romania alexberendei

    Changed the loadDefault function inside MarketplaceStorage.php to:

      public function loadDefault(AccountInterface $user = NULL) {
        $default = NULL;
        if ($uid = $this->getCurrentUserId($user)) {
            $config = $this->configFactory->get('commerce_marketplace.settings');
            $uuid = $config->get("owners.{$uid}.default_store");
            $query = \Drupal::entityTypeManager()->getStorage('commerce_store')->getQuery();
            $ids = $query->condition('uid', $uid)->accessCheck(TRUE)->execute();
        }
        elseif ($defaultStore = parent::loadDefault()) {
            $uuid = $defaultStore->uuid();
            $query = \Drupal::entityTypeManager()->getStorage('commerce_store')->getQuery();
            $ids = $query->accessCheck(TRUE)->execute();
        }
    
        if (!empty($ids)) {
            $stores = $this->loadMultiple($ids);
            if ($uuid) {
                foreach ($stores as $store) {
                    if ($store->uuid() == $uuid) {
                        $default = $store;
                        break;
                    }
                }
            }
            else {
                $default = end($stores);
            }
        }
        else {
            $stores = $this->loadMultiple();
        }
    
        if (!$default && isset($store)) {
            // This is the case when previously assigned default store was
            // deleted, so we need to return at least the last found store.
            $default = $store;
            $default->enforceIsNew();
            if (count($stores) > 1) {
                $this->messenger()->addWarning($this->t('No one default store is assigned yet. Note that it is recommended to have one explicitly assigned otherwise the last found store will be dimmed as the default. This may lead to unexpected behaviour.'), FALSE);
            }
        }
        elseif (!$default && $stores) {
            // As a last resort let's return the first store in the list.
            $default = reset($stores);
        }
    
        return $default;
    }
    
  • 🇷🇴Romania alexberendei

    On /store/add, the same error appear pointing to loadMultiple
    #2 /app/web/modules/contrib/commerce_marketplace/src/MarketplaceStorage.php(70): Drupal\commerce_marketplace\MarketplaceStorage->loadMultiple()

    I've changed the loadMultiple function, in order to work, to :

    public function loadMultiple(array $ids = NULL, AccountInterface $user = NULL) {
        $stores = [];
    
        // If $ids are provided, load the entities directly.
        if ($ids) {
            $stores = parent::loadMultiple($ids);
        } else {
            // If $ids are not provided, build the entity query.
            $query = \Drupal::entityTypeManager()->getStorage('commerce_store')->getQuery();
    
            // Add condition based on the user, if provided.
            if ($user) {
                $query->condition('uid', $user->id());
            }
    
            // Execute the query and load the entities.
            $ids = $query->accessCheck(TRUE)->execute();
            $stores = parent::loadMultiple($ids);
        }
    
        return $stores;
    }
    
    
  • First commit to issue fork.
  • Status changed to Needs review 8 months ago
  • Hii @farhadhf and all maintainers I have created MR for the above issue, Please review this
    Thanks !!

  • 🇷🇴Romania alexberendei

    Thank you @Harpreet_singh_saluja , I have another place where this error also happens:

    on /admin/commerce/config/store

    Operations: #0 /app/web/core/lib/Drupal/Core/Entity/Query/Sql/Query.php(80): Drupal\Core\Entity\Query\Sql\Query->prepare()
    #1 /app/web/modules/contrib/commerce_marketplace/src/Plugin/Field/FieldFormatter/MarketplaceTypeLabelFormatter.php(94): Drupal\Core\Entity\Query\Sql\Query->execute()

    I've replaced this (94) line:
    $used = count($this->storage->getQuery()->condition('uid', $uid)->condition('type', $store_type)->execute());

    To this:

    $query = $this->storage->getQuery();
    $query->condition('uid', $uid);
    $query->condition('type', $store_type);
    // Explicitly set whether the query should be access checked
    $query->accessCheck(FALSE); // or TRUE if access checking is needed
    $used = count($query->execute());
  • created a child issue for that and created merge request there too. @alexberendei thanks for point it

  • 🇩🇪Germany fsnet

    Access checks are already adressed in 🐛 For D10 version: Update accessCheck Needs review . Since the other issue is older and adresses all occurences we should write a MR for 🐛 For D10 version: Update accessCheck Needs review and close this issue as duplicate.

Production build 0.71.5 2024