How to show my module's form on specify page type when visit the page.
Created 7 years ago by oimken

I am trying to make my self a vote module, and I got a working backend part similar to Page module, Concept:

  1. I create votes,vote_types,vote_fields just like page module structure.
  2. Create a Relation Field to select vote_types and assign it to a vote type of PageModule
  3. In page template, display vote module' form when vistit page if page type is vote 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.

keevitaja  —  7 years ago

Is $form->render() returning the string? as PHPs __toString must 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 do return (string) $form->render()

Another question is, do you need this PluginCriteria? I have not used it, so just guessing... Twig_SimpleFunction can just return this result.

ryanthompson  —  7 years ago

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).

oimken  —  7 years ago

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:

  1. this vote form is a multiple form class VoteFrontFormBuilder extends MultipleFormBuilder just like create page form of page module in backend.

  2. I set a relation field for 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 }}

  3. I need to pass the {{ page.entry.relate_votetype.id }} to form builder to get all the fields.

So .... I do not know how ....

oimken  —  7 years ago

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