Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Header injection

Started by Muut Archive 10 years ago · 18 replies · 951 views
10 years ago

How would I inject YAML into the header before it gets processed? I thought I could go into the onBuildPagesInitialized event, but that appears to be too early. I want to use Grav\Common\Page\Header to set new values before it gets rendered. Is onPageInitialized too late? I've tried both but I can't find the right event key or method to get me the page object. Any guidance appreciated!

10 years ago

onPageProcessed might be the earliest, but the taxonomy hasn't been loaded yet, and that's something I'd need access to. Hrm.

10 years ago

I recognize that there are lots of headers I can't meaningfully change at these event points (I see plugins are already processed), but I'm thinking of injecting metadata keys so Grav can output the correct <meta> tags.

10 years ago

The filesource plugin hooks onto the onPageContentRawevent and gets the Page object via $event['page'], but that's not working at those other event points.

10 years ago

I'm an idiot. I'd delete this whole thread if I could. I did this already in another plugin I did. The answer is $page = $this->grav['page']; /sighs/

10 years ago

So while I can now access the headers, when I inject new 'metadata' subkeys and values, they don't get rendered in the final HTML. I'm currently hooked into onPageInitialized.

I tried hooking to onPageProcessed but that appears to be too early. All I managed to do was force a 404. I want to inject <meta> tags (and possibly other header content as well) based on taxonomy.

10 years ago

You need to set the modified header back on the page object with:

PHP
$page->header($header);

However, if you wan this to persist to cache rather than doing this on every page requests for this page, then you need to use a onPageContentRaw() or onPageContentProcessed

10 years ago

That's what I do:

PHP
$page = $this->grav['page'];
$header = $page->header();
$header = new \Grav\Common\Page\Header((array) $header);
$header->set('metadata.some.nested', 'value');
$page->header($header->items);

But still no <meta> gets outputted.

Thanks for the life cycle tips. I thought it might be too late by those points. I'll try those.

10 years ago

That's the last line of code in that function, though. Not sure if there's something else I need to do to finalize it.

10 years ago

Yeah, moving to onPageContentProcessed didn't change anything either. I dumped the $header at the top and $page->header() at the very end, and the changes make it, but no meta tags are outputted.

10 years ago

I did some more testing. If I stick to onPageInitialized, then it turns out I can activate plugins (e.g., setting the header pubmed.active: true). I assume that the expanded shortcode still gets cached.

10 years ago

Actually I have you the wrong event, should be onPageProcessed

10 years ago

Well, they are processed when pages are built with Page::init(). After each page is processed onPageProcessed is called passing that page via the event. this is the best place to manipulate anything related the headers.

This init() method calls Page::metadata() where the metadata is pulled from headers, merged with site-level config, and stored in Page::$metadata - https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Page/Page.php#L1328-L1386

So, you need to get the $page->metadata() do your work on it an set it back via $page->metadata($metadata) from the onPageProcessed() event. That should fix everything :)

10 years ago

The trick, though, is that I want the core Grav metadata code to do the work. If the user wants to inject Twitter cards or a refresh value, the Grav code already detects and handles that. I don't want to duplicate that function and it's supporting hashes. Ideally I'd nullify the existing metadata and just retrigger the metadata() method. But I can't do that because Page::metadata is protected, and Page::metadata() doesn't have a facility for nullifying the data once it's initialized.

I don't have any skin in this game. It was a plugin idea I saw and so decided to tinker with. It seems to work for activating plugins, and I need to test if it's also working for custom twig variables, but it's the <meta> tags that are not working. If there's no easy fix on the Grav side of things, I understand it's not a priority and I'll just scrap the plugin for now. If there is a way to accommodate, then let me know and I'll go from there. For now I'll leave this one and look at webmentions.

Thanks!

10 years ago

Well I think you can do whatever you need by getting the pre-processed metadata with Page::metadata() this gets a simple array. Then you can null things, or set new things, or modify existing things directly on this array, then set the 'modified' array back with Page::metadata($metadata).

10 years ago

But the core code detects things like http_equiv, and that's reflected in the returned array. So the plugin would have to also detect those things and adjust the array correctly (basically copy/paste the Page::metadata() function). Ideally, I'd let the Grav code do that by it just reparsing the modified header.

10 years ago

For now, because everything else is working as expected, I'm going to copy/paste the Grav code and just publish. If a workaround becomes available, then great.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1361 9 years ago
Archive · by Muut Archive, 9 years ago
2 939 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2958 9 years ago
Archive · by Muut Archive, 9 years ago
3 1123 9 years ago