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

Blueprints function call for page titles

Started by Muut Archive 9 years ago · 1 replies · 743 views
9 years ago

I want to use a function call in my blueprints where I fetch the titles of all pages located in a directory, but I have no idea how to do this. The example in the tutorial fetches all paths, so I want to do something similar where I can define which directory (ie '/blog/' or '/filters/') as a parameter and then get a list of all children in my select input where selected it will save the title of the page to the markdown file.

9 years ago

I kind of solved this myself by adding below to my themename.php file.

I don't have much experience with PHP, doing mostly front-end, but this works perfectly and on first glance doesn't look hacky at all.

---php
use Grav\Common\Grav;

/**

  • Get page titles returned.
  • @return array
    */
    public static function getPageTitle($collection_options, $path)
    {

    PHP
    $grav = Grav::instance();
    $page = $grav['page'];
    $collection = $page->evaluate([$collection_options => $path]);
    $titles = [];
    
    foreach ($collection as $item) {
        $titles[$item->title()] = $item->title();
    }
    
    return $titles;
    

    }

    TXT
    
    

and then adding the method to my blueprint like this:

---yaml
header.taxonomy.functionality:
type: select
label: Functionalities
multiple: true
data-options@: ['\Grav\Theme\Themename::getPageTitle', '@page.modular', '/product/functionalities']
validate:
type: commalist

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1333 9 years ago
Archive · by Muut Archive, 9 years ago
2 924 9 years ago
Archive · by Muut Archive, 9 years ago
2 4055 9 years ago
Archive · by Muut Archive, 9 years ago
1 2933 9 years ago
Archive · by Muut Archive, 9 years ago
3 1110 9 years ago