Exemplo n.º 1
0
 /**
  * Renders the fieldset content
  * @param FieldsetInterface $fieldset
  * @param string|null $formType
  * @param array $displayOptions
  * @param bool $displayButtons
  * @param bool $renderErrors
  * @throws \DluTwBootstrap\Form\Exception\UnsupportedElementTypeException
  * @return string
  */
 public function content(FieldsetInterface $fieldset, $formType = null, array $displayOptions = array(), $displayButtons = true, $renderErrors = true)
 {
     $renderer = $this->getView();
     if (!method_exists($renderer, 'plugin')) {
         // Bail early if renderer is not pluggable
         return '';
     }
     $formType = $this->formUtil->filterFormType($formType);
     $rowHelper = $renderer->plugin('form_row_twb');
     $iterator = $fieldset->getIterator();
     $html = '';
     if (array_key_exists('fieldsets', $displayOptions)) {
         $displayOptionsFieldsets = $displayOptions['fieldsets'];
     } else {
         $displayOptionsFieldsets = array();
     }
     if (array_key_exists('elements', $displayOptions)) {
         $displayOptionsElements = $displayOptions['elements'];
     } else {
         $displayOptionsElements = array();
     }
     //Iterate over all fieldset elements and render them
     foreach ($iterator as $elementOrFieldset) {
         $elementName = $elementOrFieldset->getName();
         $elementBareName = $this->formUtil->getBareElementName($elementName);
         $templateMarkup = "";
         if ($elementOrFieldset instanceof FieldsetInterface) {
             //Fieldset
             /* @var $elementOrFieldset FieldsetInterface */
             //Get fieldset display options
             if (array_key_exists($elementBareName, $displayOptionsFieldsets)) {
                 $displayOptionsFieldset = $displayOptionsFieldsets[$elementBareName];
             } else {
                 $displayOptionsFieldset = array();
             }
             $renderedMarkup = $this->render($elementOrFieldset, $formType, $displayOptionsFieldset, true, true, $renderErrors);
             $html .= "\n" . $renderedMarkup;
             if ($fieldset instanceof CollectionElement && $fieldset->shouldCreateTemplate()) {
                 $templateMarkup = $this->renderTemplate($fieldset);
             }
         } elseif ($elementOrFieldset instanceof ElementInterface) {
             //Element
             /* @var $element ElementInterface */
             if (!$displayButtons && in_array($elementOrFieldset->getAttribute('type'), array('submit', 'reset', 'button'))) {
                 //We should ignore 'button' elements and this is a 'button' element, so skip the rest of the iteration
                 continue;
             }
             //Get element display options
             if (array_key_exists($elementBareName, $displayOptionsElements)) {
                 $displayOptionsElement = $displayOptionsElements[$elementBareName];
             } else {
                 $displayOptionsElement = array();
             }
             $html .= "\n" . $rowHelper($elementOrFieldset, $formType, $displayOptionsElement, $renderErrors);
             if ($fieldset instanceof CollectionElement && $fieldset->shouldCreateTemplate()) {
                 $templateMarkup .= $rowHelper($elementOrFieldset);
             }
         } else {
             //Unsupported item type
             throw new UnsupportedElementTypeException('Fieldsets may contain only fieldsets or elements.');
         }
     }
     // If $templateMarkup is not empty, use it for simplify adding new element in JavaScript
     if (!empty($templateMarkup)) {
         $escapeHtmlAttribHelper = $this->getEscapeHtmlAttrHelper();
         $html .= sprintf('<span data-template="%s"></span>', $escapeHtmlAttribHelper($templateMarkup));
     }
     return $html;
 }