Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

PHP theme function to return list of pages with specific template

Started by Nick 9 years ago · 1 replies · 900 views
9 years ago

First post, so be gentle!

I want to create a select (dropdown) in admin with a list of pages which have the specific template 'parish', i.e. with the pagename 'parish.md'.

I have created a theme (incubator) and in and in incubator.phpI have a function which I want to return the array of page slugs/titles which I can use in @data-options for the select ('@data-options': '\Grav\Theme\Incubator::getParishes')

I am missing something - I know the function below is all wrong but how do I achieve what I want?

PHP
public static function getParishes()
{
        $collection = new Collection();
        $collection = $collection->ofType('parish')->order('date', 'desc');
        //dump($collection);
        $titles = [];

        foreach ($collection as $page) {
            $titles[$page->slug()] = $page->title();
        }
        return $titles;
}

$collection appears to just contain everything. That said I don't even know how to iterate through $collection!

Thanks

Nick

9 years ago

This works as a workaround:

PHP
class Incubator extends Theme
{
    // Access plugin events in this class

    public static function getDioceses()
        {
            $page = Grav::instance()['page'];
            $collections = $page->evaluate(['@root.descendants'])->ofType('parish');
            $titles = [];
            foreach ($collections as $k=>$v) {
                $titles[$v->slug()] = $v->title();
            }
            return $titles;
        }
}
last edited 02/16/18 by Nick

Suggested topics

Topic Participants Replies Views Activity
Support · by TomW, 2 weeks ago
8 227 9 hours ago
Support · by alex, 2 months ago
8 667 24 hours ago
Support · by Supple, 5 days ago
1 98 1 day ago
Support · by TomW, 1 week ago
3 159 3 days ago
Support · by TomW, 6 days ago
2 106 5 days ago