0

I am creating a template with custom page creation forms. on Page B I currently have a textbox where the user types the name of their institution and then a drop down where they can select a logo for the institution.

PAGE B:

.logo:
   type: filepicker
   folder: 'user/pages/01.home/04._affiliates/'
   label: Select a logo for the institution
   preview_images: true
   accept:
     - .svg

These logos are on another page uploaded in the header.media_order

However, on Page A I have custom fields with the following format:

PAGE A: custom fields format

Ideally, I would like a single dropdown on Page B where a user can choose an institution from the header.associations on the user/pages/01.home/04._affiliates/ page (Page A) and from there be able to access the name and SVG logo in Page B's twig template.

SelectUnique provides a dropdown box, but there is little to no documentation on it, so I don't know if it is possible to get access to such data. Is this even possible?

gcoulby
  • 534
  • 2
  • 10
  • 20

1 Answers1

1

I don't know if this is the 'correct' way to do things. However, this was a solution I came up with:

I used Created a custom plugin called ListInstitutes with a list() function. Here is the code:

EDIT: Code updated to reflect comments by @passerby

<?php
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;


class ListInstitutes extends Plugin
{
    
    public static function list()
    {
        $out_array = array();

        //get static access to Grav page instance 
        $page = Grav::instance()['page'];
    
        //get the affiliates page
        $affiliates = $page->find("/home/_affiliates");

        //get the uri prefix for associated media 
        $media_uri = "/" . str_replace("://", "/", $affiliates->getMediaUri()) . "/";
    
        //get the headers for the affiliates page
        $header = $affiliates->header();
    
        //get the associations from the headers 
        $associations = $header->associations;
    
        foreach($associations as $association){
            $out_array[$media_uri . $association["logo"]."||".$association["institution"]] = $association["institution"];
        }  
        return $out_array;
    }
}

I then created this field in my blueprint:

header.affil:
    type: select
    label: Institute
    size: medium
    classes: fancy
    data-options@: '\Grav\Plugin\ListInstitutes::list'
    default: ''
    options:
       '': 'Select'

By doing this I was able to retrieve the logos from the headers on the other page and then populate the dropdown in the new page. It would be nice if this was supported in the same way as filepicker, but this works.

gcoulby
  • 534
  • 2
  • 10
  • 20
  • A bit more 'correct' would be to follow the Grav standard way of creating plugins. See the devtools plugin (https://github.com/getgrav/grav-plugin-devtools) and create a plugin using `$ bin/plugin devtools newplugin`. Also, shouldn't the PHP method `list` be `static`? – passerby Nov 08 '20 at 08:07
  • I presume you need to save the value of `.affil` into the header of the page, if so, its field name should be `header.affil` – passerby Nov 08 '20 at 08:12
  • `$media_uri` isn't used anywhere. – passerby Nov 08 '20 at 08:14
  • I updated the code above to reflect your changes. List was static then I removed it for testing something else, but it doesn't effect it. However, you are right it SHOULD be static so I have changed. $media_uri was supposed to be used. Is the inclusion of header needed for every field, in practice I find its only needed on the first field declared, then .affil works after that (is this bad practice though)? Thanks for your feedback. I will create the plugin again using the devtools according to the documentation. – gcoulby Nov 09 '20 at 09:43
  • Yes, you'll need `\Grav\Common\Page\Page->header()` to get/set the entire header object (See https://learn.getgrav.org/16/api#class-gravcommonpagepage). I think you can mark your answer as 'solution'... – passerby Nov 09 '20 at 14:33