How to get FormCollection to recognize Custom Form View Helper

I created a custom view helper and element for TreeView. I have existing modules that display forms simply by calling formCollection($this->form). I want to utilize the formTreeView() helper but it only displays a simple text box.

If I create a new phtml and manually call formTreeView($this->form->get(‘TREE-VIEW’)); it displays as expected.

So far it is the correct behaviour, because if your helper is not added to the allowed types for rendering, the default view helper FormInput will be used.

Every time you use the view helpers Form, FormCollection or FormRow, the proxy helper FormElement is used. This form helper detects the class or type to fetch the related helper to render.

You must add your view helper to the proxy helper and this can be done in the factory for your custom view helper:

class FormTreeViewFactory
{
    public function __invoke(Psr\Container\ContainerInterface $container): FormTreeView
    {
        $helper = new FormTreeView();

        /** @var Laminas\View\HelperPluginManager $viewHelperManager */
        $viewHelperManager = $container->get('ViewHelperManager');

        /** @var Laminas\Form\View\Helper\FormElement $formElementHelper */
        $formElementHelper = $viewHelperManager->get(Laminas\Form\View\Helper\FormElement::class);
        $formElementHelper->addType('tree-view', 'treeView');
        // or
        $formElementHelper->addClass(FormTreeView::class, 'treeView');

        return $helper;
    }
}

See:

I get the concept, but in your factory example, when does the $renderer variable get instantiated? What class is it? I was unable to determine what class has a plugin method. Thanks for your assistance.

Sorry, copy and paste error. I have corrected the code example.