How to render a form without theme layout
Created 7 years ago by oimken

Here is my plugin, I want it to show a form on a page with specify page type:

Oimken\VotesModule\Vote\Command\RenderVote.php

<?php namespace Oimken\VotesModule\Vote\Command;

use Illuminate\View\Factory;
use Oimken\VotesModule\Vote\Form\Command\AddFrontFormFromTypeId;
use Oimken\VotesModule\Vote\Form\VoteEntryFormBuilder;
use Oimken\VotesModule\Vote\Form\VoteFrontFormBuilder;
use Illuminate\Foundation\Bus\DispatchesJobs;

class RenderVote
{

    use DispatchesJobs;

    protected $typeId;

    public function __construct($typeId)
    {
        $this->typeId = $typeId;
    }

    public function handle(VoteEntryFormBuilder $form, Factory $view)
    {

        $this->dispatch(new AddFrontFormFromTypeId($form, $this->typeId));

        return  $form->render(); // This works, render the form with theme's layout

// try to render the form content only, without the layout...   failure.
       return $view->make(
            $options->get('view', 'oimken.module.votes::vote'),
//            compact('votes', 'options')
       )->render();
    }

Oimken\VotesModule\VotesModulePlugin.php

<?php namespace Oimken\VotesModule;

use Anomaly\Streams\Platform\Addon\Plugin\Plugin;
use Oimken\VotesModule\Vote\Command\RenderVote;

class VotesModulePlugin extends Plugin
{

    /**
     * Get the plugin functions.
     *
     * @return array
     */
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction(
                'vote',
                function ($typeId) {
                    return $this->dispatch(new RenderVote($typeId));
                }
            ),
        ];
    }
}

...pyrocms/addons/pyro/oimken/votes-module/resources/views/vote.twig

{{ form('vote') }}

......./pyrocms/core/pyrocms/starter-theme/resources/views/layouts/default.twig

<!DOCTYPE html>
<html lang="en">

<head>
    {% block metadata deferred %}
    {% include "theme::partials/metadata" %}
    {% endblock %}
</head>

<body>

{% include "theme::partials/navigation" %}

<div id="content">
{{ vote(1)|raw }} {# type id = 1 #}
</div>

{% include "theme::partials/footer" %}
{% include "theme::partials/scripts" %}

</body>

</html>

I almost exhaustion to deal with this, Please help me , many many thanks.

daviesgeek  —  7 years ago

In RenderVote.php:

return $form->make()->getFormContent();
keevitaja  —  7 years ago

@oimken in similar situations sometimes you can cast presentable object to string just by casting it with (string) $object

this works when object has __toString() magic method attached to it

oimken  —  7 years ago

OMG ! That's Sweet! It works!

but missing the form's sections VoteEntryFormSections