Exemple #1
0
 /**
  * Render a collection by iterating through all fieldsets and elements.
  *
  * If no arguments are provided, returns object instance.
  *
  * @param  ElementInterface $element    Form collection or fieldset to populate in the view
  * @param  bool $wrap
  * @param  string|bool $partial         Name of partial view script
  * @return string|self
  */
 public function __invoke(ElementInterface $element = null, $wrap = true, $partial = true)
 {
     if (0 === func_num_args()) {
         return $this;
     }
     if ($element instanceof Collection && ($element->allowRemove() || $element->allowAdd())) {
         $headScript = $this->getView()->plugin('headScript');
         $basePath = $this->getView()->plugin('basePath');
         $headScript()->appendFile($basePath('assets/cms-common/js/form/collection.js'));
     }
     $this->setShouldWrap($wrap);
     $this->setPartial($partial);
     return $this->render($element);
 }
Exemple #2
0
 public function render(ElementInterface $element, $allowRemove = false)
 {
     $renderer = $this->getView();
     if (!method_exists($renderer, 'plugin')) {
         // Bail early if renderer is not pluggable
         return '';
     }
     $markup = '';
     $addButton = '';
     $removeButton = '';
     $escapeHtmlHelper = $this->getEscapeHtmlHelper();
     $elementHelper = $this->getElementHelper();
     $fieldsetHelper = $this->getFieldsetHelper();
     foreach ($element->getIterator() as $elementOrFieldset) {
         if ($elementOrFieldset instanceof FieldsetInterface) {
             if ($fieldsetHelper instanceof $this && $element instanceof Collection) {
                 $markup .= $fieldsetHelper($elementOrFieldset, $element->allowRemove());
             } else {
                 $markup .= $fieldsetHelper($elementOrFieldset);
             }
         } elseif ($elementOrFieldset instanceof ElementInterface) {
             $markup .= $elementHelper($elementOrFieldset);
         }
     }
     // If $templateMarkup is not empty, use it for simplify adding new element in JavaScript
     if ($element instanceof CollectionElement && $element->shouldCreateTemplate()) {
         $markup .= $this->renderTemplate($element);
         if ($element->allowAdd()) {
             $addButton = '<button onclick="' . str_replace('__placeholder__', $element->getTemplatePlaceholder(), $this->addButtonEvent) . '" type="button" class="close"><i class="glyphicon glyphicon-plus"></i></button>';
         }
     } elseif ($element instanceof Fieldset && $allowRemove) {
         $removeButton = sprintf($this->removeButtonMarkup, $this->removeButtonEvent, $this->removeButtonContent);
     }
     $label = $element->getLabel();
     if (!empty($label)) {
         if (null !== ($translator = $this->getTranslator())) {
             $label = $translator->translate($label, $this->getTranslatorTextDomain());
         }
         $label = $escapeHtmlHelper($label);
         $label = sprintf('<legend>%s%s%s</legend>', $label, $removeButton, $addButton);
         $wrapper = $label;
     } else {
         $wrapper = sprintf('<div class="collection-header">%s%s</div>', $removeButton, $addButton);
     }
     $markup = sprintf('<fieldset>%s%s</fieldset>', $wrapper, $markup);
     return $markup;
 }
 /**
  * Determines whether this element needs the add/remove buttons at all.
  * @param ElementInterface $element
  * @return boolean
  */
 protected function needsButtons(ElementInterface $element)
 {
     if (!$element instanceof Collection) {
         return false;
     }
     return $element->allowAdd() || $element->allowRemove();
 }