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

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...