Frontmatter bulk editing

I have a lot of pages in a site generated by a conversion from typo3. Now I want to improve custom settings and taxonomy in the frontmatter of a large subset of these pages.

Do you have any tool suggestion beside sed to batch edit the custom headers? For example I have to move a field from a custom header to taxonomy. Anyone who did similar operations and can give me hints?

I’ve automized operations in some contexts using events. To give you an idea:

$page = $pagesobj->get($pathtopage);
if ($page) {
    if (isset($page->header()->filterforme)) {
          $page->modifyHeader('whatever', false);
          $page->save();
    }
}

Everything you need is available in the Pages and the Page class. The admin plugin has several event hooks that you may use to execute bulk routines: onAdminAfterSave, onAdminSave but you can use as well the onPagesInitialized event.

1 Like

Interesting, it’s all there, right. Thank you for that. I’am not a php-lover coming from java and unnaturally try to avoid it, but without a real reason. I will read the API pages of the events and the modification calls. Where should I place that code? Would it be in a plugin or elsewhere?

For things related with your site only you can use the theme.php file, that’s one possibility, or you prepare a re-usable plugin for your bulk edits to have it at hand in the future. Grav offers good documentation and it’s in fact quite easy if you have some basic knowledge.

1 Like

Thank you very much for your answer, I had a try and success with my first plugin:

    /**
     * When pages are initialized read and modify headers.
     *
     * @param Event $e
     */
    public function onPagesInitialized(Event $e) {
        $debugger = $this->grav['debugger'];
        
        // Get page
        $page = $this->grav['page'];

        $collection = $page->evaluate(['@page.children' => '/jkirchenkapellen'])->ofType('country');
        foreach ($collection as $cpage) {
            $changed = false;
            if (is_array($cpage->header()->taxonomy['country']) && isset($cpage->header()->taxonomy['country'][0])) {
                $countryKey = $cpage->header()->taxonomy['country'][0];
                $cpage->header()->taxonomy['country'] = $countryKey;
                $changed = true;
                $debugger->addMessage($cpage->header()->taxonomy['country']);
            }
            if ($changed) {
                $cpage->save();
            }
        }
    }

This changes

taxonomy:
    country:
        - austria

to

taxonomy:
    country: austria

:grinning:

1 Like

Freut mich. Viel Erfolg weiterhin.