Skip to content
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

Configure FormElementManager #16

Open
weierophinney opened this issue Dec 31, 2019 · 4 comments
Open

Configure FormElementManager #16

weierophinney opened this issue Dec 31, 2019 · 4 comments
Labels
Question Further information is requested

Comments

@weierophinney
Copy link
Contributor

I have a little module that configures the FormElementManager like this:

return[
    'form_elements' => [
        'factories' => [
            PriceFieldset::class        => PriceFieldsetFactory::class,
            ProductFormInterface::class => ProductFormFactory::class,
        ],
    ],
];

This config is loaded by a ConfigProvider class and loaded by the ConfigManager in the /config/config.php file like this:

$configManager = new ConfigManager(
    [
        Zend\Form\ConfigProvider::class,
        Product\ConfigProvider::class,
        new PhpFileProvider($pattern),
    ],
    $cachedConfigFile
);

When I want to access the ProductFormInterface::class from the FormElementManager it is not found. I need to these lines to the /config/container.php file to get this running.

$container = new ServiceManager();
(new Config($config['dependencies']))->configureServiceManager($container);

// Inject config
$container->setService('config', $config);

/** @var FormElementManagerV3Polyfill $formElementManager */
$formElementManager = $container->get('FormElementManager');
$formElementManager->configure($config['form_elements']);

return $container;

What am I doing wrong? I don't think this is a wanted behaviour. In my module I also have some validators, filters, hydrators and input filters as well. Currently, I need to add the configuration manually for every specialized service managers.


Originally posted by @RalfEggert at zendframework/zend-expressive#387

@weierophinney weierophinney added the Question Further information is requested label Dec 31, 2019
@weierophinney
Copy link
Contributor Author

How do you configure FormElementManager? Do you have a factory that injects it with configuration?

I did something similar here: https://github.com/mtymek/blast-input-filter - I configure validator, filter and input filter plugin managers in a similar manner as you do. Take a look - maybe it will give you some hints.
Alternatively, maybe you would like to contribute to my component and add support for form elements?


Originally posted by @mtymek at zendframework/zend-expressive#387 (comment)

@weierophinney
Copy link
Contributor Author

Like I said, I only have a special ConfigProvider which reads the config file and adds it to the configuration in the /config/config.php. i thought that all the configuration is passed to formElementManager and others. To fix this I added the lines I mentioned.

Your package looks like a better solution though I am missing hydrators and form elements. The name of the package seems a bit misleading when adding hydrators and form elements.


Originally posted by @RalfEggert at zendframework/zend-expressive#387 (comment)

@weierophinney
Copy link
Contributor Author

So, basically you're missing a factory that does the code you put in config.php ;) By itself, FormElementManager doesn't know that it should look for form_elements config key.

My package doesn't have hydrators and form elements because I don't use them (well, I use hydrators but I keep them in main SM...). But I'll be happy to accept PR or even consider changing the package name to something more relevant afterwards.


Originally posted by @mtymek at zendframework/zend-expressive#387 (comment)

@weierophinney
Copy link
Contributor Author

@RalfEggert for this i usually use Delegator Factories like

<?php

namespace My\Namespace\Form;

use Interop\Container\ContainerInterface;
use Zend\Form\FormElementManager\FormElementManagerV3Polyfill;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\Factory\DelegatorFactoryInterface;

class FormElementManagerDelegatorFactory implements DelegatorFactoryInterface
{
    public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null)
    {
        $formElementManager = $callback();

        $config = $container->has('config') ? $container->get('config') : [];
        $config = isset($config['form_elements']) ? $config['form_elements'] : [];
        (new Config($config))->configureServiceManager($formElementManager);

        return $formElementManager;
    }
}

and in my ConfigProvider config

<?php

return [
    'dependencies' => [
        'delegators' => [
            'HydratorManager' => [
                My\Namespace\Hydrator\HydratorManagerDelegatorFactory::class,
            ],
            'InputFilterManager' => [
                My\Namespace\InputFilter\InputFilterManagerDelegatorFactory::class,
            ],
            'FormElementManager' => [
                My\Namespace\Form\FormElementManagerDelegatorFactory::class,
            ],
        ],
    ],
];

yes, class naming could be improved ;-)


Originally posted by @skors at zendframework/zend-expressive#387 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant