How to add custom fields for all page template without creating individual blueprints?


#1

I have several types of pages with unique blueprints but i need to create custom fields which will work on all types of pages. How can I do that?


#2

I am not sure if you have a multisite or default setup. I solved this (on my multisite setup) by augmenting the environment setup of the ‘blueprints’ stream as follows:

<?php

use Grav\Common\Utils;

return [
'streams' => [
  'blueprints' => [
                 'type' => 'ReadOnlyStream',
                 'prefixes' => [
                     '' => [
                         "user/blueprints",
                         "system/blueprints"
                     ]
                 ]
              ],
    ]
];

I assume you could just put this into a setup.php file.

What this does, it augments the blueprints streams and loads the blueprints folder in /user/blueprints.

Now, make a folder

/user/blueprints/pages - and this behaves then as a replica of the /system/blueprints/pages folder – look into it to find the root blueprints.

Here’s an example then of a blueprint extension for default.yaml:

title: PLUGIN_ADMIN.DEFAULT

@extends:
  type: default
  context: blueprints://pages

form:
  fields:
    tabs:
      type: tabs
      active: 1

      fields:
        advanced:
            unset@: true
            type: ignore

        content:
            fields:

                header.myfield:
                  type: text
                  label: My custom field
                  toggleable: true
                  help: This is the tooltip for the field
                  ordering@: 1

Note the ordering - can be >1. Negative values will assume this field is at the BOTTOM of the fields list and will then decrease that index, so to put something all the way at the top, you’d do e.g -99 :slight_smile:

Good luck!