-
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
Ability to provide all initialized forms #307
Conversation
… initialized forms.
Here are some examples of how you can manipulate the forms and their data from your plugin. There are likely more, but there I took from a single project where I'm customizing form per page. Adding a form to the page from your plugin: public function onFormPageHeaderProcessed(Event $event)
{
/** @var Page $page */
$page = $event['page'];
if ($page->template() === 'flex-shop') {
$form = CompiledYamlFile::instance('blueprints://products/form.yaml')->content();
}
if (isset($form['form'])) {
$header = $event['header'];
$header->form = $form['form'];
}
}
} Initialize the form: public function onFormInitialized(Event $event)
{
/** @var Uri $uri */
$uri = $this->grav['uri'];
/** @var \Grav\Plugin\Form\Form $form */
$form = $event['form'];
$id = $uri->param('id');
if ($form->name() === 'flex-shop-order') {
// Load products somehow...
$product = Products::get(...);
if (!$product) {
return;
}
$product->updateForm($form);
}
} Custom validation and data conversion: public function onFormValidationProcessed(Event $event)
{
/** @var Form $form */
$form = $event['form'];
if ($form->name() === 'flex-shop-product-form') {
$data = $form->getData();
$size = $data->get('size');
$size['sf'] = (float) ($size['sf'] ?? 0);
foreach ($size['area'] as &$area) {
$area['length'] = (float) ($area['length'] ?? 0);
$area['width'] = (float) ($area['width'] ?? 0);
}
unset($area);
if (!$size) {
throw new ValidationException('No size!');
}
$data->set('size', $size);
}
} Post-process form, like save object, etc: public function onFormProcessed(Event $event)
{
/** @var Form $form */
$form = $event['form'];
$action = $event['action'];
$params = $event['params'];
if ($action === 'my_action' && $params == true) {
// You can do whatever here
}
} |
Thank you for the examples.
Here the two hooks Cache is also an important topic and beat me during my previous attempts. I hope this clarify why I think a direct and unconditional access to currents forms is needed and beneficial. Side note: my forms do not submit to Grav. |
You missed Also, you can register your own services to the $grav object, which allows you to get the current form. Just be careful not to use a too general name as you do not want to replace existing service. |
NB: the fact that |
Latest, biggest (and actually blocking) problem is that Note: You may want to think the opposite : that cache should take care of associated assets... The underlying issue is that doctrine cache is only enabled for individuals pages but is not for a (modular) page as a whole. I already hit this issue when I tried to defer the HTML output of some bootstrap modals into the footer. |
The problem highly relates to getgrav/grav#1934. |
Looking at https://github.com/getgrav/grav/blob/1.6/CHANGELOG.md I found two very interesting additions:
It's worth trying but I don't think these two will provide the needed workaround and I don't think they will resolve the underlying issue getgrav/grav#1934 |
I see that the constructor of |
Cool, thanks. :) |
Provide
Form->getForms()
that return initialized forms.If I want to post-process a form (disregarding caching), I may use
getForm()
but it's too limited.If I'm only interested in forms having a
xtra_field
set to1
, independently of their name or route, I'm stuck. MoreovergetForm()
fails when it comes to modular pages.So just give users and developers the power to proceed with their data. Don't lock them with
protected
properties when API do not provide any (right) way to access such a data.See also getgrav/grav#2277
edit:
flat_forms
may be better after all.