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.

General

Frontmatter bulk editing

Solved by Uli Hake View solution

Started by Dieter Tremel 8 years ago · 5 replies · 892 views
8 years ago

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?

8 years ago Solution

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

PHP
$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
8 years ago

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?

8 years ago

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
8 years ago

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

PHP
    /**
     * 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

YAML
taxonomy:
    country:
        - austria

to

YAML
taxonomy:
    country: austria

😀

👍 1
8 years ago

Freut mich. Viel Erfolg weiterhin.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 82 12 hours ago
General · by pamtbaau, 17 hours ago
1 55 16 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 348 5 days ago
General · by Duc , 5 days ago
3 43 5 days ago