Jump to content
BillH

Setting the value of a field when loading a page for editing

Recommended Posts

It feels like this should be obvious or it should be easy to find a solution in the forums, but I've got stuck!

I'm trying to set the value of a field (based on other field values) when an existing page loads for editing.

Here's a reduced test version of the code that doesn't work, from ready.php:

wire()->addHookAfter('Page::loaded', function($event) {

    $page = $event->object;
    
    if ($page->template == 'article') {

        // Get field values and do calculations here
		// but for testing:
        $calculatedValue = "Testing 123";
    
        $page->of(false);
        $page->set('test_field', $calculatedValue);

    }

});

Note that if I start the code like this, it works fine when the page is saved:

wire()->addHookAfter('Pages::saveReady', function($event) {

    $page = $event->arguments[0];

	if ($page->template...

So I presume there's something wrong with the hook or getting the page.

But what?

Share this post


Link to post
Share on other sites

Maybe it's enough to hook into it before the page is ready to be saved.

wire()->addHookBefore('Pages::saveReady', function($event) {

 

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...