How to switch off Subsites extension on a DataList

When running subsites the old (2.4) way of disabling or enabling subsites is:

Subsite::disable_subsite_filter(true);
$ret = Page::get()->filter(...);
Subsite::disable_subsite_filter(false);

This disables Subsites to augment the query (and filter only for the current subsite), but from Silverstripe 3.0 on queries are lazy, which is good, cause the database gets only hit when the data is really needed.

BUT... when you need the data, Subsites doesn't know anymore, that you need the global data and it filters again.

One possible solution would be to grab the data right now and make an ArrayList out of it. But that means the database is hit, regardless if you need the data or not. Wouldn't it be better, to tell the DataList to ignore Subsites completely? Well, not directly, but you can flag the DataList that Subsites won't alter the query:

$list = SiteTree::get()
    ->alterDataQuery(function($query, $list) {
        $query->setQueryParam('Subsite.filter', false);
    });

This way you can pass the data around and it won't be filtered for the current subsite only. Unfortunately it only works for the SiteTreeSubsites extension and is at the time of writing not implemented through the whole Subsites module.

Rate this post

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments