How to access to header custom properties with php?

I already know how to access properties from header in twig templates.
If I want to get the title of an article and printing it in a page, I can write:
page.header.title
If I want to get strings associated to my custom keys, I can do that like this:
page.header.metadata.author

Doing this, I call the string stored in this yaml frontmatter:
metadata:
> author: ‘author-person’

How can I store in a php var these contents?
I’m searching through docs since yesterday for hours and hours but I didn’t find a specifical solution. I saw that I can recall taxonomy with a method, but I don’t get the general approach.

There are numerous ways to store data.

You’re already comfortable with storing data as variables in a page header (BTW also frequently named “frontmatter”) so you could create a page just to store your data. And likely hide it from the menu by setting visibility: false in its frontmatter.

More suggestions are given in Store and read data in grav

I hope this helps.

1 Like

Thank you, the link you passed is very useful and I think it has answers to a lot of my questions, but I really can’t find the answer to my original question. I’m sure it’s my fault: maybe I didn’t explain my problem well. What I have to do it’s basically get the following info, for example:

page.header.metadata.author

BUT I don’t want to access the data like this, in twig; instead, I want to do the same thing in php (exactly the same thing). The problem is all here: I can solve the problem in twig, but I’m asking here because I think it’s better to split the logic and the template, so I would prefer to get the info in php and store this info in a variable that I will recall in the twig template. Maybe it’s a simple task, but I’m having problems and I’m not able to do that.
Thank you again

You need to create a plugin for that. To learn how, follow the Plugin Tutorial.

In your plugin you first must decide on what Grav events you want to act.

Finally, for an example of how to read and handle page header or frontmatter variables see for example https://github.com/bleutzinn/grav-plugin-add-page-by-form/blob/master/add-page-by-form.php#L366

This in addition to the resources mentioned earlier should get you going again.

1 Like

I’m studying from this examples, but I continue to obtain the same error when trying to get frontmatter infos:
"Undefined property: stdClass::$pagefrontmatter"

In my php, I did this:

public function onTwigSiteVariables()
    {
        $page = $this->grav['page'];
        $header = $page->header();

        // Get variables from the plugin configuration
        if (isset($header->pagefrontmatter) && is_array($header->pagefrontmatter))
        {
          $page_frontmatter = $header->pagefrontmatter;
        }
        else
        {
          $page_frontmatter = array();
        }
...

I basically copypaste the function in the form plugin or other plugins but it doesn’t work: it’s like $pagefrontmatter doesn’t exists at all.

EDIT: I get it. I simply didn’t know that to make this vars available in twig, I was supposed to add those like this:

$twig = $this->grav['twig'];
$twig->twig_vars['LOL'] = 'LOL string';

Now that I know this, I should be able to do the rest. I’m gonna try ASAP

EDIT 2: Although I know how to do more, I continue to can’t find pagefrontmatter.
EDIT 3: I was wrong in trying to recall from pagefrontmatter. It’s sufficient to recall objects from $page->header();

Thank you very much for your suggestions

I’m glad you figured it out. In hindsight the code I pointed you to may be confusing. There (line 366) “pageconfig” and below that “pagefrontmatter” which you tried to use are actually variables in the page header. Using title instead would have been simpler since that variable is by default present in every page header.
Sorry for not being very clear on that.

Oh, I see! I didn’t noticed that. By the way, I enjoy diving into the code, when I have some input. It’s good for learning, so thanks again for pointing me the right code to start.