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

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

Started by Ilya Pesterev 8 years ago · 1 replies · 803 views
8 years ago

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?

8 years ago

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
<?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:

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 🙂

Good luck!

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1140 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 63 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 138 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 114 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 132 7 months ago