Date set in 'page.changeStatus:before' hook not available for sorting


#1

In the 'page.changeStatus:before' hook I do
$page->update(['published' => date('Y-m-d')]);

This works and the date is correctly set after the status change.
But when I try to use this date as sorting number with

num: "{{ page.published.toDate('%y%m%d') }}"

the date seems not to be available for the actual status change, although the (previously empty) field should have been set :before the status change as the hook suggests.

The page folder will be prefixed with 0_ instead of the date.
After changing the status a second time (date field not empty) the page gets prefixed with 190310_.

I didn’t try to use a timestamp, but I assume it would be the time from the first status change.

Is this a bug or why does the changes of a page made ‘:before’ the status change do not apply? :face_with_monocle:


#2

This is the order of the things in the changeStatus() method:


    protected function changeStatusToListed(int $position = null): self
    {
        // create a sorting number for the page
        $num = $this->createNum($position);

        // don't sort if not necessary
        if ($this->status() === 'listed' && $num === $this->num()) {
            return $this;
        }

        $page = $this->commit('changeStatus', [$this, 'listed', $num], function ($page, $status, $position) {
            return $page->publish()->changeNum($position);
        });

        if ($this->blueprint()->num() === 'default') {
            $page->resortSiblingsAfterListing($num);
        }

        return $page;
    }

As you can see, the number is calculated first, and then passed to the commit() method. The hooks are triggered within the commit method. So the date you update in your before hook is not taken into account.

So this is not a bug. Maybe you can overwrite the changeStatus/commit methods in a page model (not tested). And you might want to create an issue in the ideas repo to change the behavior.


#3

Okay, thank you for the clarification @texnixe :slight_smile:

Just thougt to simply use my published date which I’m setting in the changeStatus:before hook anyways, (which would allow me to transform the date format of the sorting number).

But it’s pretty unimportant since I can simply use num: date.
And overwriting the changeStatus/commit method seems exaggerated only to have 190311_ instead of 20190311_ in the folder name :rofl:

It was more like “Why is this field not available for the changeStatus action when I’m setting it :before?!”
I might add an issue to the ideas repo then :wink: