-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Form::getPage() throws Error when returning null #518
Comments
This is expected and the behavior is correct. All the froms are created before a page gets initialized to be able to find the page where the form was located in. Forms are then cached. So there is no knowledge of which page you are on when the forms are created as the page keeps on changing but the form instances stay the same. |
Depending on whether a page has already been initialised and cached, That means there are two scenarios:
The discussion is whether scenario 1. is an unexpected/invalid state or not and should throw or return
|
Thanks for the improvements! I'm afraid however, that the new |
TypeError is kinda restricted to PHP only. The correct is LogicError as it's a programming error to call it too early. I would not use the method at all in |
OK, fair enough... Uhm... what would then be a better/save event to get the page and form before it is being send to the front-end? I'm not sure where in Grav's lifecycle fits the NB. Data from page's frontmatter is required to update the form definition. |
One place would be late in |
Thanks for the suggestion, I appreciate it. Unfortunately, adding extra fields to the form using It does seem to work during
Events fired:
Do you still expect issues using above code? |
Thanks for your testing on this. Maybe we need another event for this -- or some extra access to the form plugin to be able to manipulate the fields. I need to check the code again to verify if that works and if there are better ways to do this, but I do agree that the onPageProcessed event is too late. |
When
$form->getPage()
is called inonFormInitialized
in plugin, an Error is thrown, when page has not yet been initialized. When page is not yet cached,onFormInitialized
is called beforeonPageInitialized
and hence$form->getPage()
cannot return a page.public function getPage(): ?PageInterface
?onFormInitialized
perhaps always be fired afteronPageInitialized
?Error thrown:
Stacktrace:
The text was updated successfully, but these errors were encountered: