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.

Forms & Blueprints

Blueprint to create select from Page Collection

admin

Solved by pamtbaau View solution

Started by storm 4 years ago · 4 replies · 757 views
4 years ago

How do I get a list of child pages of /teams to create an admin blueprint for a user to create an event? (pick Home team and Visiting team for a game)

I can create a 'select' with options

YAML
team-select-away:
  type: select
  label: Visiting Team
  options:
    option1: "Team 1"
    option2: "Team 2"

I want the options to be populated from all child pages that exist under /teams.

YAML
team-select-home:
  type: select
  label: Home Team
  options:
    - ''
  data-options@:

I can populate options from:

TXT
  data-options@: '\Grav\Common\Page\Pages::types'

I cannot populate options from:

TXT
  data-options@: ['\Grav\Common\Page\Pages::children', '/teams']
  data-options@: ['\Grav\Common\Page\Pages::getCollection', '/teams']
  data-options@: 
    '@page.children': '/teams'

I know the field works as I've set it up. I just can't seem to figure out the YAML syntax for 'get all children of /teams and populate them in a select list.'

last edited 03/04/22 by storm
4 years ago Solution

@storm, The function being called must be a static function. The ones that are failing aren't.

Try:

  • $ bin/gpm install devtools
  • $ bin/plugin devtools new-plugin
    Follow wizard and run $ composer update in folder of new plugin.
    Let's say you called the plugin "teams".
  • Add the following function to file /user/plugins/teams/teams.php:

    PHP
    public static function teams(): array
    {
    /** @var Grav */
    $grav = Grav::instance();
    $grav['admin']->enablePages();
    
    /** @var Page */
    $teamsPage = $grav['pages']->find('/teams');
    
    $children = [];
    foreach ($teamsPage->children() as $team) {
      $children[] = $team->title();
    }
    
    return $children;
    }
    
  • In your blueprint use
    TXT
    data-options@: '\Grav\Plugin\TeamsPlugin::teams'
    
👍 1
last edited 03/05/22 by pamtbaau
4 years ago

@pamtbaau's reply is correct.

Wish I'd known Discourse is still active a few days ago when I wasted a lot of time looking for this same problem's answer and posted a question on a closed issue before finding an answer on Stack Overflow of all places. Is this forum indexed by search engines?

$grav['admin']->enablePages(); seems to be the magic key here. I would love to see this documented as an official solution in the Collections chapter of Learn, but don't feel qualified to volunteer as I don't understand it properly. It seems to me it's a reasonably common requirement.

4 years ago

@hughbris,

Wish I’d known Discourse is still active

Out of sight, out of mind... ;-)

Is this forum indexed by search engines?

Yes, contrary to Discord, Discourse is being indexed by Google. Try hughbris site:discourse.getgrav.org on Google.

last edited 03/22/22 by pamtbaau
4 years ago

@pamtbaau:
Yes, contrary to Discord, Discourse is being indexed by Google. Try hughbris site:discourse.getgrav.org on Google.

Maybe my problem is that I never search with Google :/ My wondering was only based on this post not coming up near the top when I looked for it. Did not investigate further, thanks anyway!

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 61 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 134 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 128 7 months ago