This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit

eZ Community » Forums » eZ Publish 5 Platform » Automatic sort

Automatic sort

Automatic sort

Thursday 26 September 2013 6:25:18 pm - 13 replies


I would like to sort my contents. But I don't want to force the sort method in my controller ( With eZ 4, I was able to use the sort method defined in the back office.

Can I do this with eZ 5 ?


Thursday 26 September 2013 10:03:24 pm

Hi Nicolas

This is automatically done when using LocationService::loadLocationChildren(). If you want to do this for search, then you'll need to convert location's sortField / sortOrder to the appropriate sort clause object, like it's done in the LocationService (unfortunately this method is not public).

Friday 27 September 2013 9:38:53 am


I think the loadLocationChildren() is enough in my case. Thanks.

Friday 27 September 2013 11:27:17 am

Wait. I now remember why I wrote my own method : loadLocationChildren() does not use "visibility"!

It should according to the description : "Loads children which are readable by the current user of a location object sorted by sortField and sortOrder"

I have declared a bug :

Modified on Friday 27 September 2013 11:36:51 am by Nicolas Lescure

Friday 27 September 2013 11:43:58 am

This is perfectly normal as in public API, visibility is not considered as a permission, so this is not a bug. Visibility status must be explicitly provided now.

Friday 27 September 2013 12:23:23 pm

I understand this not a permission, from a eZ Pubish technical point of view. BUT, I think this is normal, when I want to get locations, I get only the ones I can access. The old fetch was perfect for that.

So if I understand, you tell me that

$this->repository->canUser( 'content', 'read', $childLocation->getContentInfo(), $childLocation )

is "true" even if the location is hidden?

Visibility status must be explicitly provided now.

What do you mean? Have you an example?

Monday 30 September 2013 9:54:08 am

Maybe André can explain this more deeply.

What do you mean? Have you an example?

This is exactly what you were trying to do (passing the visibility criterion to the search service like in Damien's example for SummerCamp). However, I think we should add the possibility to inject a criterion in the loadLocationChildren() method.

Monday 30 September 2013 10:25:04 am

OK. I'm now sure I have to use the search service, and not the location service. 

And if I need to use the sort order defined in the BO, I have to copy/paste the code in the protected method you gave (getSortClauseBySortField()). 

Monday 01 December 2014 9:39:25 am

Thanks Nicolas and Jérôme, this was exactly what I needed.

Instead of copying the code from LocationService, I rather extended it like this:

 * Extending the LocationService class to be able to use protected function getSortClauseBySortField()
 * The purpose is to sort searches by sort settings set on the location (in admin interface)
namespace XXX\SiteBundle\eZ\Publish\Core\Repository;
use eZ\Publish\Core\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Location;
class SortLocationService extends LocationService
    public function __construct()
    public function getSortClauseFromLocation( Location $location )
        return $this->getSortClauseBySortField( $location->sortField,
                                                $location->sortOrder );

It's a hack, to be able to use the protected function. happy.gif Emoticon How about making this function public somehow/somewhere?

Saturday 31 January 2015 2:43:41 pm

What Arne said. Why is this method not public?

Monday 02 February 2015 1:44:31 pm

getSortClauseBySortField() ?
I guess we can make that public, needs to be added to API basically, and implementation should rather throw NotImplemented on sort fields not supported yet instead of fallback to a different sort method. And it should probably rather be on SearchService as that is where you also have findLocations().

Modified on Monday 02 February 2015 1:49:00 pm by André R

Wednesday 18 March 2015 10:27:46 pm

Hello Arne,

Very nice your solution! Now I can access the private method.

I tried to use it here but got into an other problem.

All my location objects have sortField and sortOrder like this:

<em>protected</em> 'sortField' => int 1<em>protected</em> 'sortOrder' => int 1

So this way the getSortClauseBySortField will always bring the default response:

$sortOrder = $sortOrder == APILocation::SORT_ORDER_DESC ? Query::SORT_DESC : Query::SORT_ASC;

switch ( $sortField ){
      return new SortClause\Location\Path( $sortOrder )

Is there some other tip you can give about this?

Thursday 19 March 2015 3:27:45 pm

Newbee mistake!!!

The class works as a charm. I was having a hard time changing the order inside CMS.

The method getSortClauseBySortField() should really be public.

Thanks you all!

Saturday 21 March 2015 6:47:54 pm

Hello Alexandre,

Do you think you could create a detailed enhancement on ?


Modified on Saturday 21 March 2015 6:48:19 pm by // Heath


You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from