Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

PHP theme function to return list of pages with specific template

Started by Nick 8 years ago · 1 replies · 621 views
8 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

8 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 Thomas, 1 week ago
3 92 2 hours ago
Support · by Anna, 3 days ago
2 94 23 hours ago
Support · by Justin Young, 24 hours ago
1 62 24 hours ago
Support · by Duc , 1 week ago
2 98 6 days ago
Support · by Colin Hume, 1 week ago
2 91 6 days ago