Search code examples
zend-framework2zend-framework-moduleszend-filter

In a Zend Framework 2 Module.php the getFilterConfig() method is never called


I have a Generic module in my Zend Framework 2 application with some filters in it.

For my entity I created a filter class with an InputFilterAwareInterface:

public function getInputFilter()
{
    if (!$this->inputFilter) {
        $inputFilter = new InputFilter();
        $factory     = new InputFactory();
        $inputFilter->add(
            $factory->createInput(
                array(
                    'name'     => 'desc',
                    'required' => false,
                    'filters'  => array(
                        array('name' => 'myfilter'),
                    ),
                )
            )
        );

        $this->inputFilter = $inputFilter;
    }
    return $this->inputFilter;
}

To load myfilter I implemented the Generic's Module.php with a FilterProviderInterface:

public function getFilterConfig()
{
    // Breakpoint at next line
    return array(
        'factories' => array(
            'myfilter' => function($sl) {
                $myfilter = $sl->getServiceLocator()->get('myfilterfactory');
                return new Filter\Filter\MyFilter($myfilter);
            },
        ),
    );
}

When I run the application an exception is thrown:

Zend\ServiceManager\Exception\ServiceNotFoundException
Zend\Filter\FilterPluginManager::get was unable to fetch or create an instance for myfilter

I ran the debugger and realized that the getFilterConfig() method is never called, because the breakpoint is not triggered.

What do I need to insert in my code to get the filter config loaded?

EDIT:

The code is executed but the debugger does not stop at the breakpoint. When I insert an additional statement and set the breakpoint to this, the execution stops.

In a controller I do the following

$serviceManager = $this->getServiceLocator();
$filterManager = $serviceManager->get('FilterManager');

and within the filter manager I have myfilter in the factories array. But still I get the exception.

How does the Zend\InputFilter\Factory (as InputFactory) know about the filter factory?


Solution

  • Finally I figured out what went wrong and how to solve this:

    Within the Zend\InputFilter\Factory that I used as InputFactory a defaultFilterChain is created from scratch which generates a new FilterPluginManager that does not get the service manager's filters.

    To solve this I insert a factory for the entity filter into the service manager which injects a defaultFilterChain with filters of the service manager:

    'EntityFilter' => function($sm) {
        $filterManager = $sm->get('FilterManager');            
        $filterChain = new FilterChain;
        $filterChain->setPluginManager($filterManager);
        return new EntityFilter($filterChain);
     }
    

    Within the getInputFilter method of the filter class EntityFilter this is inserted into the InputFactory:

    $factory = new InputFactory();
    $factory->setDefaultFilterChain($this->defaultFilterChain);
    

    Now the InputFactory can also use these filters of the service manager's filter manager that includes myfilter.