Jump to content

Recommended Posts

Posted

Hi all. 

I have to set default values for some fields (page select and text fields) on my page. I think the /site/templates/admin.php is the appropriate place? But I can't get anything to work from there. That is my current code, that looks to me like it should work. But it doesn't :-) 

wire()->addHookAfter('Page::added', function($event) {
	wire('page')->setAndSave('page_select_field', 1042);
	wire('page')->setAndSave('text_field', 'Default Value');
});

require($config->paths->adminTemplates . 'controller.php');

 

Posted

It depends. The admin.php is only executed if you visit the admin backend. If you add pages as part of any other page in your website the hook would need to be in site/ready.php. 

Also your hook was not correct. There's no Page::added hook (nor function), and in the Pages::added you can retrieve the saved page from the functions arguments.

$this->wire()->addHookAfter('Pages::added', function($event) {
	$page = $event->arguments(0);

	if($page->template != 'xxx') return;

	$page->setAndSave([
		'page_select_field' => 1042,
		'text_field' => 'Default Value'
	]);
});

 

  • Like 2
Posted

Thanks for your quick response! I looked in Captain Hook and saw …

#1280: public function ___added(Page $page)

… but didn't noticed it was in the Pages section. Had just seen Page $page. 

Anyhow, the code above works great. I only need it for pages created in the CMS backend. 

17 minutes ago, LostKobrakai said:

if you add pages as part of any other page

I guess you mean front-end pages (template), right? 

  • 6 years later...
Posted

Hi there! I've been looking for some similar solutions, but my case with the admin section setting/overriding default values,  (need to setAndSave ''disable_automatic_append_of_file:_main.php' => true',  'prepend_file' => 'site-globals.php'), doing above modification in templates/admin.php,  it seems like i'm missing something or it just doesn't reflect in the admin corresponding tabs.

Please somebody correct me

Posted

@uzlander

I'd use saveReady and isNew() to initialize a default value, so the hook only apply the first time page is created:

$this->addHookBefore('Pages::saveReady(template=my-template)', function (HookEvent $event): void {
    $page = $event->arguments(0);
    if ($page->isNew()) {
        $page->myField = "myDefaultValue";
    }
});

Or like this, replacing isNew() by id=0 in hook selector:

$this->addHookBefore('Pages::saveReady(template=my-template, id=0)', function (HookEvent $event): void {
    $page = $event->arguments(0);
    $page->myField = "myDefaultValue";
});

 

  • 5 weeks later...
Posted

Oh, thank you so much for elaborating && kind reply )).

Am about to test your solution above #da2#, i believe it should work though..
I've been out for a while and now having an issue accessing an OS(zorin glitch), so i'm gonna start over with fresh installation and work it through.

Thanks a lot !:)

  • Like 1

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
  • Recently Browsing   0 members

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