Excluding page from collection in custom page method

I am writing a custom page method. I am trying to build a collection that excludes a particular page (and prepends it later on). But this does not seem to work:

$this->children()->filterBy('intendedtemplate', 'programme')->listed()->not('vessel')

The page ‘vessel’ is a child of site, so I assume the ID is right, but still does not seem to be excluded. Could it be I cannot call not() in this way from an extension?

Thank you

But the vessel is not part of your collection if you get the children of a given page? Or is it a site method?

Wait… no, I am doing it all wrong.

When using a custom page method in a sortBy: option of a type:pages section, the method is applied to every page in the collection of pages in that section right? and the method needs to return the index where each page needs to be placed, correct?

I mean, in site.yml

    sections:
      public_programmes:
        type: pages
        headline: Public
        status: listed
        sortable: false
        sortBy: sortedProgrammesPanel

Then in plugins/page-methods/index.php for example

...
'sortedProgrammesPanel' => function () {
  $sorted = site()->children()->filterBy('intendedtemplate', 'programme')->listed()->not('vessel') 
  $sorted->prepend(page('vessel'));
  return $sorted->indexOf($this);
}
...

There are two issues, one is a missing semicolon and then you need to prepend by key, I think:

  'sortedProgrammesPanel' => function () {
            $sorted = site()->children()->filterBy('intendedtemplate', 'programme')->listed()->not('vessel');
            $sorted = site()->children()->listed()->not('vessel');
            if ($page = page('vessel')) {
                $sorted->prepend($page->id(), $page);
            }
            return $sorted->indexOf($this);
   }

I edit the above to include an if statement

Yes that solves it, thank you.
Why is , in this case, prepend called with id, page ?

Because it says so in the docs?

In the reference (I’ve checked before asking) it has an example of prepend in a loop where the first argument is the key of the element position in the loop. I think I’ve been using $pages->prepend($page) without the id argument.

I now went and check the code for prepend under collection and if I am reading it right, I’d say the key argument is not mandatory is that correct ?

That also confuses me a bit. If a collection is ordered… why does it handle keys aswell ? is it both ordered and associative?

Thank you

Got to check, maybe I’m mistaken, but somehow it didn’t work without the key in my test.

Yes, I think adding the key is what made it work, but I don’t understand why. Thanks