eZ Platform Discussions

Search query Criterions / Content Search / Location Search


#1

Hey there,

I want to fetch all pages with an specific content type, but only of the first level or of a specific parent node id (home id).
According to the documentation I could add a criterion for the parentLocationId or the location depth.
Both are not working as expected. Nothing changes. Except an error message that I can’t use Location/Depth in a content search, only in a location search. I couldn’t find any example of a correct location search.
Any ideas?
https://doc.ezplatform.com/en/1.7/guide/search/#search-criteria-reference

This is the controller:

   private function getQueryForContentTypes(array $contentTypes, $locationid = null): Query
    {
        $options = [
            new Criterion\Subtree($this->currentLocation->pathString),
            new Criterion\Visibility(Criterion\Visibility::VISIBLE),
            new Criterion\ContentTypeIdentifier($contentTypes),
            new Criterion\Location\Depth(Criterion\Operator::LTE, 1)
        ];

        if ($locationid) {
            $options[] = new Criterion\ParentLocationId($locationid);
        }

        $query = new Query();
        $query->query = new Criterion\LogicalAnd($options);

        return $query;
    }

Thanks in advance.


#2

Hi!

You will have to use LocationQuery instead of pure Query object and use findLocations method instead of findContent. After that, your query should work.