Jump to content
Tobi C.

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

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.

Share this post


Link to post
Share on other sites

Hi @flydev

thanks for your help. The first thread was exactly what I was searching for.

$this->addHookAfter('ProcessPageEdit::loadPage', $this, 'onProcessPageEdit');

That did the job just fine.

  • Like 1

Share this post


Link to post
Share on other sites
1 minute ago, Tobi C. said:

That did the job just fine.

You could experiment with Pages::added too. This would also run the code when you add a new page through the API.

  • Like 1

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

You may additionally try out the hook "setupNew"

$this->addHookAfter('Pages::setupNew', $this, 'setDefaults');
$this->addHookAfter('Pages::added', $this, 'setDefaults');

or as @BitPoet already mentioned "Pages::added".

Share this post


Link to post
Share on other sites

Thanx, @horst, I already tried this and some other hooks but they didn't change anythings.
I dont think that the hook is the problem as setting the title works. 

Share this post


Link to post
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 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...