How to show my module's form on specify page type when visit the page.
Created 8 years ago by oimkenI am trying to make my self a vote module,
and I got a working backend part similar to Page module,
Concept:
- I create
votes,vote_types,vote_fieldsjust like page module structure. - Create a Relation Field to select
vote_typesand assign it to avote typeofPageModule - In page template, display
vote module' form when vistit page if page type isvote type.
I finished step 1 and 2, but there is a thing beyond my php ability, I tried for days and no luck, please help me....thanks very much.
How to get my vote form show on page? Here is my attempts:
try to show the vote form just like ContactPlugin:
protected $bindings = [
'vote' => VoteFormBuilder::class,
];
failure, the error is somethings like missing vote type, sorry for did not log the error info because it is days ago.
try to make a twig plugin just like structure() of page module do:
in VoteModulePlugin :
public function getFunctions()
{
return [
new \Twig_SimpleFunction(
'vote',
function ($typeId) {
return new PluginCriteria(
'render',
function (VoteEntryFormBuilder $form) use ($typeId) {
$this->dispatch(new AddEntryFormFromRequest($form,$typeId));
$this->dispatch(new AddVoteFormFromRequest($form,$typeId));
return $form->render();
}
);
}
),
];
}
Failure, error PluginCriteria::__toString() must return a string value")
Any tips or guide will be helpful, thanks you very much.
Bind the form builder to a string like my_form then just use the form function in your view:
{{ form('my_form')|raw }}
This will automatically wrap the builder in a criteria so you can do like .redirect('foo') on it too (.get() is the trigger to get the form if you want to set it to a variable).
Thanks your replies, guys.
@ ryanthompson , I watch the video for several times, I knew there are many way to create a from..
But there are some differences:
-
this
vote formis a multiple formclass VoteFrontFormBuilder extends MultipleFormBuilderjust like create page form ofpage modulein backend. -
I set a
relation fieldfor page type, to bind the vote type id to the page, so i can get the vote type id through{{ page.entry.relate_votetype.id }} -
I need to pass the
{{ page.entry.relate_votetype.id }}to form builder to get all the fields.
So .... I do not know how ....
I try to show create page form in front to see what's wrong with my vote-module:
How to make a create page form show in front-end:
in Anomaly\PagesModule\PagesModuleServiceProvider
protected $bindings = [
'Anomaly\Streams\Platform\Model\Pages\PagesPagesEntryModel' => 'Anomaly\PagesModule\Page\PageModel',
'Anomaly\Streams\Platform\Model\Pages\PagesTypesEntryModel' => 'Anomaly\PagesModule\Type\TypeModel',
'form_page' => PageEntryFormBuilder::class, //add form binding
];
in Twig:
{{ form('form_page')|raw }}
The errors
FatalErrorException in eebbff232daa271f718bddc1a2ee793dd3885d11ba1837188bfcd8c09a99a0c3.php line 0:
Method Anomaly\Streams\Platform\Ui\Form\FormCriteria::__toString() must not throw an exception, caught Error: Call to a member function getFormMode() on null
Is
$form->render()returning the string? as PHPs__toStringmust always return a string.https://github.com/anomalylabs/streams-platform/blob/fc6b387ad47d904aba771c1aff3486174280e57e/src/Addon/Plugin/PluginCriteria.php#L158
If
$form-render()has a presenter, then you could perhaps doreturn (string) $form->render()Another question is, do you need this PluginCriteria? I have not used it, so just guessing...
Twig_SimpleFunctioncan just return this result.