Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Category filter field displays all categories from all category groups #23

Open
stanislavprokopov opened this issue Aug 24, 2022 · 0 comments

Comments

@stanislavprokopov
Copy link

So I have added a category field to the "Search Fields" list, now when i open the frontend, the field will appear in the filters section, but it will have all categories i have defined in the admin panel, regardless of the actual category field source.

The issue is with this line:

$find = ElementHelper::findSource($elementType, $source);

It does not provide the third parameter to the findSource call, this results in using the default CONTEXT_INDEX context.

Call stack:

https://github.com/craftcms/cms/blob/2eac9249964ccc553bf841c79b9ee44d58f16b61/src/helpers/ElementHelper.php#L562

https://github.com/craftcms/cms/blob/71aac29fa480dd3b09a72c4fa54a5fd4976b4dd4/src/services/ElementSources.php#L76

https://github.com/craftcms/cms/blob/71aac29fa480dd3b09a72c4fa54a5fd4976b4dd4/src/services/ElementSources.php#L341

https://github.com/craftcms/cms/blob/71aac29fa480dd3b09a72c4fa54a5fd4976b4dd4/src/base/Element.php#L795

https://github.com/craftcms/cms/blob/71aac29fa480dd3b09a72c4fa54a5fd4976b4dd4/src/elements/Category.php#L190

https://github.com/craftcms/cms/blob/71aac29fa480dd3b09a72c4fa54a5fd4976b4dd4/src/services/Categories.php#L160

getEditableGroups has this check:

if (Craft::$app->getRequest()->getIsConsoleRequest()) {
    return $this->getAllGroups();
}

$user = Craft::$app->getUser()->getIdentity();

if (!$user) {
    return [];
}

So if you are not authenticated, then you will get empty sources, this results in $criteria = $find['criteria'] ?? null; being null, so no criteria filtering gets applied to the Category element find query.

Fix: Add third parameter to the findSource call, for example this one: \craft\services\ElementSources::CONTEXT_FIELD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant