getElement(); if ((!$form instanceof Zend_Form) && (!$form instanceof Zend_Form_DisplayGroup)) { return $content; } $separator = $this->getSeparator(); $translator = $form->getTranslator(); $items = []; $view = $form->getView(); $wrappers = $this->getOption('wrappers'); /** @var Zend_Form_Element|Zend_Form|Zend_Form_DisplayGroup $item */ foreach ($form as $item) { $item->setView($view)->setTranslator($translator); $items[$item->getName()] = $item->render(); } if ($wrappers) { $items = $this->wrap($items, $wrappers); } $elementContent = implode($separator, $items); switch ($this->getPlacement()) { case self::PREPEND: return $elementContent . $separator . $content; case self::APPEND: default: return $content . $separator . $elementContent; } } protected function wrap(array $items, array $wrappers) { $result = []; reset($items); while (($name = key($items)) && ($html = current($items))) { next($items); if (empty($wrappers)) { $result[] = $html; continue; } foreach ($wrappers as $wrapperIndex => $wrapper) { if (!in_array($name, $wrapper['elements'])) { $result[] = $html; continue 2; } $decorators = $this->_getDecorators($wrapper['decorators']); $content = ''; foreach ($wrapper['elements'] as $_name) { if (array_key_exists($_name, $items)) { $content .= $items[$_name]; unset($items[$_name]); } } $result[] = $this->renderDecorators($decorators, $content); unset($wrappers[$wrapperIndex]); continue 2; } } return $result; } protected function renderDecorators($decorators, $content) { /** @var \Zend_Form_Decorator_Interface $decorator */ foreach ($decorators as $decorator) { $content = $decorator->render($content); } return $content; } protected function _getDecorators(array $decoratorsInfo) { $decorators = []; foreach ($decoratorsInfo as $decoratorInfo) { $decorators[] = $this->_getDecorator($decoratorInfo); } return $decorators; } /** * @param $decoratorInfo * @return $this|Zend_Form_Decorator_Interface * @throws Zend_Form_Exception */ protected function _getDecorator($decoratorInfo) { if ($decoratorInfo instanceof Zend_Form_Decorator_Interface) { return $decoratorInfo; } elseif (is_string($decoratorInfo)) { return $this->_loadDecorator($decoratorInfo); } elseif (is_array($decoratorInfo)) { $argc = count($decoratorInfo); $options = []; if (isset($decoratorInfo['decorator'])) { $decorator = $decoratorInfo['decorator']; if (isset($decoratorInfo['options'])) { $options = $decoratorInfo['options']; } return $this->_loadDecorator($decorator, $options); } else { switch (true) { case (0 == $argc): break; case (1 <= $argc): $decorator = array_shift($decoratorInfo); case (2 <= $argc): $options = array_shift($decoratorInfo); default: return $this->_loadDecorator($decorator, $options); break; } } } else { throw new Zend_Form_Exception('Invalid wrapper decorator'); } return $this; } protected function _loadDecorator($name, $options = []) { $class = $this->getElement()->getPluginLoader(Qs_Form::DECORATOR)->load($name); if (null === $options) { $decorator = new $class; } else { $decorator = new $class($options); } $name = $decorator->getOption('name'); $decorator->setElement(new Zend_Form_Element_Hidden($name ? $name : 'custom')); return $decorator; } }