I successfully added a custom view helper with the following configuration:
https://gist.github.com/webdevilopers/b22f7471fd2b8d60cdea#file-module-php
The view helper has a custom variable named foo
:
https://gist.github.com/webdevilopers/b22f7471fd2b8d60cdea#file-abstractformautocomplete-php
As I mentioned this setup works fine as long as I have only a single element using the view helper.
As soon as I add more than one form element the setFoo
method gets only called once and the foo
variable remains set throughout the following elements.
https://gist.github.com/webdevilopers/b22f7471fd2b8d60cdea#file-autocompleteform-php
I read about Shared Services in ZF2 - is this such a case? How can I prevent this behaviour?
You've basically answered your own question here. Services are shared by default, so the initial instance of your view helper is reused for subsequent calls unless you configure it otherwise. To do this, add the shared
parameter to your view helper config (untested):
public function getViewHelperConfig()
{
return array(
'invokables' => array(
'formelement' => 'Application\Form\View\Helper\FormElement',
'formautocompletehidden' => 'Application\Form\View\Helper\FormAutocompleteHidden'
),
'shared' => array(
'formelement' => false,
'formautocompletehidden' => false
),
);
}
Edit: as mentioned in the comments, this doesn't currently work for view helpers due to a bug in ZF2.