Search code examples
formsdropdownblueprintgrav

Is it possible to access headers from another page in Grav CMS blueprint form


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?


Solution

  • 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.