Zend Form and View Helpers - xorock/zend-expressive-phptalrenderer GitHub Wiki

Zend Form and View Helpers

Note: The following functions will affect the overall system performance

Zend Form has several convenient extras placed inside a View\Helper folder. By default, all helpers are used with Zend\View\PHPRenderer. We can simply enable them, but We also need to include Zend\View and Zend\I18n to our project:

composer require zendframework/zend-view
composer require zendframework/zend-i18n

I also suggest You to include Expressive Configuration Manager:

composer require mtymek/expressive-config-manager

Now We can change our config.php file to take advantage of Expressive Configuration Manager:

use Zend\Expressive\ConfigManager\ConfigManager;
use Zend\Expressive\ConfigManager\PhpFileProvider;

$configManager = new ConfigManager([
    \Zend\Form\ConfigProvider::class,
    \Zend\InputFilter\ConfigProvider::class,
    \Zend\Hydrator\ConfigProvider::class,
    new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
], 'data/config-cache.php');

return new ArrayObject($configManager->getMergedConfig());

Config providers will automatically register all factories and other options. Now We need to create our own HelperPluginManagerFactory.

namespace Zend\Expressive\Phptal;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Config;
use Zend\View\HelperPluginManager;
use Zend\View\Renderer\PhpRenderer;

class HelperPluginManagerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $manager = new HelperPluginManager($container);

        $config = $container->has('config') ? $container->get('config') : [];
        $config = isset($config['view_helpers']) ? $config['view_helpers'] : [];
        (new Config($config))->configureServiceManager($manager);
        $manager->setRenderer((new PhpRenderer)->setHelperPluginManager($manager));
        return $manager;
    }
}

Our factory searches through the container for view_helpers key, creates new PHPRenderer instance and then injects HelperPluginManager.

Next step is to inform Service Manager about our factory:

use Zend\View\HelperPluginManager;
use Zend\Expressive\Phptal\HelperPluginManagerFactory;

return [
    'dependencies' => [
        'factories' => [
            HelperPluginManager::class => HelperPluginManagerFactory::class,
        ],
    ],
];

We are ready to go.

ExampleActionFactory

public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
    // some other code
    $albumForm = $container->get(AlbumDataForm::class); // Zend\Form
    /* @var $pluginManager \Zend\View\HelperPluginManager */
    $pluginManager = $container->get(\Zend\View\HelperPluginManager::class);
    $controller = new ExampleAction($router, $template, $albumForm, $pluginManager);
}

ExampleAction

public function __construct(
    RouterInterface $router, 
    TemplateRendererInterface $template, 
    $albumForm, 
    $pluginManager
) {
    $this->router   = $router;
    $this->template = $template;
    $this->form     = $albumForm;
    $this->pluginManager = $pluginManager;
}

public function __invoke(
    ServerRequestInterface $request,
    ResponseInterface $response,
    callable $next = null
) {
    return new HtmlResponse($this->template->render('example::form', [
        'form' => $this->form,
        'manager' => $this->pluginManager,
        // You can also pass whole renderer
        // 'renderer' => $this->pluginManager->getRenderer()
    ]));
}

example/form.html

<tal:block metal:fill-slot="content">
    ${structure php: manager.get('form').render(form)}
    ${structure php: renderer.form(form)}
</tal:block>

HelperPluginManager will search for key form inside registered Service Manager (manager.get('form')), then Zend\Form\View\Helper\Form::render method will display the entire form. When renderer is passed, it will call magic __call and proxy to HelperPluginManager.

⚠️ **GitHub.com Fallback** ⚠️