Jump to content

[solved] How to set default values for repeater fields on adding a new page in backend


Tobi C.
 Share

Recommended Posts

Hi,

I am a little stuck, while trying to set some default headlines for repeater fields when an user creates a new page of a specific template. I think this has to be done with hooks but I dont know which hook to use for that case.

The repeater creates a minimum of 4 instances and each contains of a headline and a textarea.

Maybe anyone here can help. Thanks.

Link to comment
Share on other sites

The hook works fine and quicktesting it by changing the page title it worked like a charm. Digging deeper into it I ran into another problem:

It seems that there is a timing problem with the repeater on page > add new.

If I create a new subpage, give it a title and hit save the url switches from "/admin/page/add/?parent_id=1" to "/admin/page/edit/?id=1234&new=1". now I am able to see all fields including the repeater. My module changes the title correctly but has no influence to the repeater fields. if I reload the page or click on edit in the page tree it works like expected but not on the first initial form rendering. 

Here is my current module code:

<?php namespace ProcessWire;
class SetDefaultValues extends Process implements Module {
    public static function getModuleInfo() {
        return array(
            'title' => 'Set Default Values',
            'version' => 1,
            'summary' => __('Set default values on page add / edit'),
            'singular' => true,
            'autoload' => true,
        );
    }

    public function init() {
        $this->addHookAfter('ProcessPageEdit::loadPage', $this, 'setDefaultValues');
    }

    public function setDefaultValues(HookEvent $event) {
        $page = $event->return;
        if ($page->template == 'my_template') {
            $defaults = array(
                'Default Headline A',
                'Default Headline B',
                'Default Headline C',
            );

            $page->title = "Default Title"; // this works
            if ($page->my_repeater) {
                foreach ($page->my_repeater as $i => $block) {
                    $block->headline = empty($block->headline) ? $defaults[$i] : $block->headline; // only works on edit, not on add
                }
            }
        }
    }
}

Any ideas how to solve this problem?

Link to comment
Share on other sites

It was a timing problem. I tried to set headlines of repeater elements that didn't exist on the first ProcessPageEdit::loadPage. The solution was to set min repeaters back to 0 and create the needed repeaters by api on Pages::added. 

<?php namespace ProcessWire;
class SetDefaultValues extends Process implements Module {
    public static function getModuleInfo() {
        return array(
            'title' => 'Set Default Values',
            'version' => 1,
            'summary' => __('Set default values on new pages'),
            'singular' => true,
            'autoload' => true,
        );
    }

    public function init() {
		$this->addHookAfter('Pages::added', $this, 'createDefaultRepeaterFields');
    }

    public function createDefaultRepeaterFields(HookEvent $event) {
        $page = $event->arguments(0);

        if ($page->template == 'my_template') {
            if (count($page->my_repeater) < 1) {
                $defaults = array(
                    'Headline A',
                    'Headline B',
                    'Headline n',
                );

                foreach ($defaults as $default) {
                    $r = $page->my_repeater->getNew();
                    $r->headline = $default;
                    $r->save();
                    $page->my_repeater->add($r);
                }
                $page->save();
            }
        }
    }
}

Thanks for helping with this.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...