@domsson, The following seems to be happening…
Form::getPage() should either return a page or null. However,
Form::getPage() is being defined as returning a
PageInterface and will therefor throw an Error if a null value is returned. It should probably be defined as returning a nullable value:
Why my example code fails in your test and succeeded in my test?
If the page has not yet been cached (it’s new, or has been changed),
onFormInitialized is handled before
Form::getPage() should then return
null, however, Grav decides to throw an error. I think this is a bug…
If the page has been cached already, then event
onFormInitialized is being handled after
Form::getPage() will then correctly return the page.
That explains, I guess, why my test succeeded and yours fails. I probably did my test with an already cached page… I’ll update the previous post.
- Add class variable
$page and assign it in
onFormInitialized check if
$page has been set and use the value.
onFormInitialized being called before
onPageInitialized when page is not yet cached?
Yes… However, it seems that if the page has not yet been cached,
onFormInitialized is called again after page is cached and
onPageInitialized has been called.
Hope this helps…