How to virtual pages in pages-section

I’ve got a pages section, where the pages are queried from page.lists

In the model i defined:

<?php
class NewsletterlistsPage extends Page {
  public function lists($options = null) {

    // this contains three real children pages
    $lists = $this->childrenAndDrafts();

    // now i want to add a virtual page
    $xx = Page::factory([
      'slug' => 'virtual',
      'template' => 'newsletterlist',
      'model' => 'newsletterlist',
      'parent' => $this,
      'draft' => true,  
      'content' => [
        'title' => "TEST",
        'slug' => 'virtual',
        'recipients' => ""
      ]
    ]);

    $lists->add($xx);

    return $lists;
  }
}

In the pages section in the panel, there are only the three real pages visible.
But if i say text: '{{ page.lists.count }}' i get 4 as result. There seems to be the forth page, but it’s not visible in the pages section list.

What could be the problem?

Hm, as far as I know, virtual pages should be defined as children of a parent, so overriding the children method in the parent model, as described in the Virtual pages guide. Only then do they become part of the site index.

I don’t think so, because if i change the section type from pages to info i can clearly see, that there are 4 Page Objects in the returning Pages Object. I can even dump them and see them. It’s just the pages section is not displaying it, as if it only would display “real” pages(?).

I can not override the children method, because that is used for the real children. I need to add a second method which i called lists and query it like this in the blueprint:

      lists2:
        type: pages
        headline: Listen ({{ page.lists.count }})
        options: query
        pages: '{{ page.lists }}'

What is a strange thing: in the headline the count is 4. But there are only 3 items in the list:

Hm, but a pages section cannot have a query or options. So your section ignores these settings and displays the children of the parent, which is the default.

Your virtual pages will only show up if they are part of the children.

@texnixe I really don’t know what I would do without you sometimes :slight_smile: :joy:

You are absolutely right, the pages section doesn’t have query and options but you said that it has parent. And that was the key to success!

Here is the working solution:

blueprint:

    sections:
      lists2:
        type: pages
        headline: Listen ({{ page.lists.children.count }})
        empty: -
        parent: page.lists

In the model i have defined lists with this function:

  public function lists($options = null) {
    $lists = $this->childrenAndDrafts();
    $this->children()->add($lists);

    $xx = Page::factory([
      'slug' => 'virtual',
      'template' => 'newsletterlist',
      'model' => 'newsletterlist',
      'parent' => $this,
      'content' => [
        'title' => "TEST",
        'slug' => 'virtual',
        'recipients' => ""
      ]
    ]);
    $this->children()->add($xx);
    return $this;
  }
}

Basically i’m retrieving the children, adding these to the lists-method as den children, and then adding my own additional pages as often as needed - and returning this as the result.
That works great:

Interesting, because the parent is supposed to be a single page, not a collection. Need to see this with my own eyes…

The parent is a single page in this case, which has the children i want to display.

Ah, alright, your function returns the page object, I didn’t read closely. Clever!

This is a simple and good solution for me to add virtual pages to the collection in a pages-section. Perfect :slight_smile: