Skip to content

page model keeps old content state on page update #2569

@mullema

Description

@mullema

I use a page model to instantiate objects based on current content state.

Describe the bug
Upon page update, the content state in a page model does not get updated.

To Reproduce

  1. Add the text 'before update' into the text field on the home page
  2. Extend the home page PageModel and add a page.update:after hook
<?php

use Kirby\Cms\Page;

class HomePage extends Page {
    protected $myText;
    protected $uniqId;

    public function __construct(array $props)
    {
        parent::__construct($props);
        $this->myText = $this->content()->text();
        $this->uniqId = uniqid();
    }

    public function getMyText() {
        return $this->myText;
    }

    public function getUniqId() {
        return $this->uniqId;
    }
}
// site/config/config.php
return [
    'debug' => true,
    'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
            error_log("uniqid: new:" . $newPage->getUniqId() . " old:" . $oldPage->getUniqId() . " text: new:" . $newPage->getMyText() . " old:" . $oldPage->getMyText());
        }
    ]
];
  1. In the panel: Edit the text field on the home page to 'after update'
  2. See output in your error_log:
    uniqid: new:5e91d593705de old:5e91d5936f4d4 text: new:before update old:before update

note that the uniqid has changed, myText didn't.

same if I try to update the page in a template:

$oldPage = page('home');
$newPage = $oldPage->update(['text' => 'after update']);

error_log("uniqid: new:" . $newPage->getUniqId() . " old:" . $oldPage->getUniqId() . " text: new:" . $newPage->getMyText() . " old:" . $oldPage->getMyText());

Expected behavior
I'd expect the property myText also to change. Therefore the log should be
uniqid: new:5e91d593705de old:5e91d5936f4d4 text: new:after update old:before update

Kirby Version
3.3.5

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions