Jump to content

sorted pagearray lost in included template


bmacnaughton
 Share

Recommended Posts

I have a situation where a pagearray appears to lose its sort order.

1. template xyzzy.php fetches a pagearray with code similar to:

// in an external include file
function get_pages($selector) {
    $found = wire('pages')->find($selector);
    wire('log')->save('fubar', count($found));
    return $found;
}

// in the template xyzzy
$pa = get_pages($some_selector);
$pa->sort('-timestamp');

2. main.inc is appended to the template. in main.inc an additional file is included, './layouts/xyzzy.php'

3. './layouts/xyzzy.php' then references $pa as such:

<? foreach($pa as $p): ?>
    // do stuff with $p

    // log for debugging
    wire('log')->save('xyzzy', $p->name);
<? endforeach ?>

4. The $pa that './xyzzy.php' sees is sorted while the $pa that './layouts/xyzzy.php' sees is NOT sorted. It seems like the PageArray/WireArray got copied at some point but the './layouts/xyzzy.php' file shouldn't have been included until 'main.inc' is appended.

 

What am I missing?

Link to comment
Share on other sites

20 minutes ago, horst said:

im not sure why it should behave that like, but have you tried to add "sort=-timestamp" directly to yout selector, and skip that extra step?

No, I hadn't tried that but it does the trick. Apparently the initial declaration (where I get the PageArray) is used by the `./layouts/xyzzy.php` file and, I'm guessing the call to sort() causes some copy operation to occur. 

I would appreciate it if someone (Ryan?) could weigh in on this. It's odd to me because the `./layouts/xyzzy.php' file is appended (by being included via 'main.inc') to './xyzzy.php'. That is after the sort occurred and no new variable is created.

Thanks for the shortcut that avoids the issue though.

Link to comment
Share on other sites

8 hours ago, horst said:

im not sure why it should behave that like, but have you tried to add "sort=-timestamp" directly to yout selector, and skip that extra step?

Horst, the problem was in our code. I imagined it was more complicated than it was (multiple people making changes sometimes leads to surprises).

I very much appreciate your comment about adding the "sort=-timestamp" clause to the selector. I hadn't done that before.

  • 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

×
×
  • Create New...