Jump to content

Selector, sort by something a couple steps removed


SteveB
 Share

Recommended Posts

I'm trying to select pages based on one of the page's page fields (easy) but want to sort results by the name of a page pointed to by a page field on the page's parent.

We're dealing with four templates...

maker:

has a name

mark_type:

has a name

pattern:

has page field 'maker' to select a maker

has children which are marks

mark:

is child of a pattern

has page field 'mark_type'

Select all the marks of a certain mark_type, sort by name of the parent's maker

$pa = $pages->find("template=mark, mark_type={$markId}, sort=[parent.maker].name, limit={$limit}, start={$start}";

Nothing I've tried for the sort works. Is there way? In some case there will be as many as 1500 matches so I don't want to have to get them all and then reorganize them in order to show 25 on a paginated page.

Link to comment
Share on other sites

A page of the mark template always has a page of the pattern template as its parent. That pattern page has a page field which is either empty or, more often, points to a page of the maker template.

pattern:

has page field 'maker' to select a maker

has children which are marks

mark:

is child of a pattern

has page field 'mark_type'

So if we were building a query we'd be needing to do some joins to get from the mark (chosen by it's mark type) to it's parent's page field (giving us a maker id) to the name of the maker.

Maybe that's beyond what selector syntax can do. I could do it in the template file with MySQL and a bunch of joins.

Link to comment
Share on other sites

In the end I did a wire('db')->query($qry). In order to get the three page ids and two sort strings I needed from the id of the mark_type. There were 6 joins.

Then there was the issue of making pagination markup since I was no longer getting that for free by doing a normal page->find.

Used $pages->find($selector)->count; with a very simple selector just to get the total number of pages being shown.

Then made up this function (based on a 2013 Soma post) to use MarkupPagerNav directly to make the pagination.

function pagination($page, $limit, $total){
    $baseUrl = $page->url;
    if($seg = wire('input')->urlSegment(1)) $baseUrl .= $seg . '/';
    $start = (wire('input')->pageNum - 1) * $limit;
    $pagerNav = new ProcessWire\PagerNav($total, $limit, wire('input')->pageNum);
    $pager = $pagerNav->getPager();
    $last = count($pager) - 1;
    foreach($pager as $k=>$link) {
        $str = '';
        if($k == 0) $str = 'MarkupPagerNavFirst MarkupPagerNavFirstNum';
        elseif($k == $last - 1) $str = 'MarkupPagerNavLastNum';
        elseif($k == $last) $str = 'MarkupPagerNavNext MarkupPagerNavLast';
        if($link->pageNum == wire('input')->pageNum) $str = 'MarkupPagerNavOn ' . $str;

        $classStr = empty($str) ? '' : " class='{$str}'";
        if($link->type == "separator") $item = '…';
        else $item = "<a href='{$baseUrl}page{$link->pageNum}/'><span>$link->label</span></a>";
        $pagerMarkup .= "\t<li{$classStr}>$item</li>\n";
    }
    // output paginator markup
    return "<ul class='MarkupPagerNav'>\n" . $pagerMarkup . "</ul>";
}

(using PW 3.0.10)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...