-
Notifications
You must be signed in to change notification settings - Fork 19
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
When limiting search to parent IDs, search is limited to current context #69
Comments
Here is a possible solution for this that uses the values from the protected function processIds(string $ids = '', string $type = 'parents', int $depth = 10): array
{
if ($ids === '') {
return [];
}
$ctx = !empty($this->config['contexts']) ? $this->config['contexts'] : '';
$ctx = $this->cleanIds($ctx);
$ctxArray = !empty($ctx) ? explode(',', $ctx) : [];
$ids = $this->cleanIds($ids);
$idArray = explode(',', $ids);
if ($type === 'parents') {
foreach ($idArray as $id) {
if (count($ctxArray) > 0){
foreach ($ctxArray as $ctx) {
$child_ids = $this->modx->getChildIds($id, $depth, ['context' => $ctx]);
if (count($child_ids) > 0){
$idArray = array_merge($idArray, $child_ids);
break;
}
}
} else {
$idArray = array_merge($idArray, $this->modx->getChildIds($id, $depth));
}
}
$idArray = array_unique($idArray);
sort($idArray);
}
$this->ids = $idArray;
return $this->ids;
} Another possible solution is to query the contexts for the specified IDs ( |
Thanks @halftrainedharry for your solution. I have briefly tested it and works as expected. This solution requires to specify all
Your second solution is probably more intuitive and would work as one expect from docs:
|
Here is some example code for the second solution: protected function processIds(string $ids = '', string $type = 'parents', int $depth = 10): array
{
if ($ids === '') {
return [];
}
$ids = $this->cleanIds($ids);
$idArray = explode(',', $ids);
if ($type === 'parents') {
// Read the 'contexts' property
$ctx = !empty($this->config['contexts']) ? $this->config['contexts'] : $this->modx->context->get('key');
$ctx = $this->cleanIds($ctx);
$ctxArray = !empty($ctx) ? explode(',', $ctx) : [];
// Query the contexts for all the IDs
$c = $this->modx->newQuery(modResource::class);
$c->select('id,context_key');
$c->where(['id:IN' => $idArray]);
$ctxLookup = [];
if ($c->prepare() && $c->stmt->execute()) {
while (($row = $c->stmt->fetch(\PDO::FETCH_ASSOC)) !== false) {
$ctxLookup[$row['id']] = $row['context_key'];
if (!in_array($row['context_key'], $ctxArray)){
$ctxArray[] = $row['context_key'];
}
}
}
// Set the 'contexts' property
$this->config['contexts'] = implode(',', $ctxArray);
foreach ($idArray as $id) {
if (array_key_exists($id, $ctxLookup)){
// Specify the correct context for this ID
$idArray = array_merge($idArray, $this->modx->getChildIds($id, $depth, ['context' => $ctxLookup[$id]]));
} else {
$idArray = array_merge($idArray, $this->modx->getChildIds($id, $depth));
}
}
$idArray = array_unique($idArray);
sort($idArray);
}
$this->ids = $idArray;
return $this->ids;
} Because SimpleSearch always restricts the result to the current context when no SimpleSearch/core/components/simplesearch/src/Driver/SimpleSearchDriverBasic.php Lines 230 to 233 in 56d692b
Also with this solution, every search that uses the |
If I get it right, now if But if only
Other option would be to have some special value for Our use case for all this: we have several large, multi-language multi-product sites organized into separate contexts. But not entire context should be searched, only children of "main menu" resource and homepage in every context. I know we can limit search using Regarding speed: while site search is done only ocassionally, it's ok to have it slower in some complicated scenarios. |
Other users that use the For you current use case, you might want to use a custom driver. |
Thanks @halftrainedharry , for now we will stick with the first solution, enabling us to search inside specified parent ids and contexts. I hope this solution will make it into future release, now we use just a hot fix. |
If &idType='parents' and some &ids are specified, search is limited to current context only.
This is due to method processIds() in SimpleSearchDriver.php, which uses modx->getChildIds($id, $depth). MODX docs (here: https://docs.modx.com/3.x/en/extending-modx/modx-class/reference/modx.getchildids) briefly mentions that this method search only current context, if no other context is specified as additional condition in third optional param.
I don't see any simple workaround here. The best would be to construct this third param for getChildIds with all contexts specified in &contexts script parameter if possible.
At the moment specifing &contexts further limits results returned by processIds()
The text was updated successfully, but these errors were encountered: