/** * Render options * * @param MultiCheckboxElement $element * @param array $options * @param array $selectedOptions * @param array $attributes * @return string */ protected function renderOptions(MultiCheckboxElement $element, array $options, array $selectedOptions, array $attributes) { $escapeHtmlHelper = $this->getEscapeHtmlHelper(); $labelHelper = $this->getLabelHelper(); $labelClose = $labelHelper->closeTag(); $labelPosition = $this->getLabelPosition(); $globalLabelAttributes = []; $closingBracket = $this->getInlineClosingBracket(); if ($element instanceof LabelAwareInterface) { $globalLabelAttributes = $element->getLabelAttributes(); } if (empty($globalLabelAttributes)) { $globalLabelAttributes = $this->labelAttributes; } $combinedMarkup = []; $count = 0; foreach ($options as $key => $optionSpec) { $count++; if ($count > 1 && array_key_exists('id', $attributes)) { unset($attributes['id']); } $value = ''; $label = ''; $inputAttributes = $attributes; $labelAttributes = $globalLabelAttributes; $selected = isset($inputAttributes['selected']) && $inputAttributes['type'] != 'radio' && $inputAttributes['selected']; $disabled = isset($inputAttributes['disabled']) && $inputAttributes['disabled']; if (is_scalar($optionSpec)) { $optionSpec = ['label' => $optionSpec, 'value' => $key]; } if (isset($optionSpec['value'])) { $value = $optionSpec['value']; } if (isset($optionSpec['label'])) { $label = $optionSpec['label']; } if (isset($optionSpec['selected'])) { $selected = $optionSpec['selected']; } if (isset($optionSpec['disabled'])) { $disabled = $optionSpec['disabled']; } if (isset($optionSpec['label_attributes'])) { $labelAttributes = isset($labelAttributes) ? array_merge($labelAttributes, $optionSpec['label_attributes']) : $optionSpec['label_attributes']; } if (isset($optionSpec['attributes'])) { $inputAttributes = array_merge($inputAttributes, $optionSpec['attributes']); } if (in_array($value, $selectedOptions)) { $selected = true; } $inputAttributes['value'] = $value; $inputAttributes['checked'] = $selected; $inputAttributes['disabled'] = $disabled; $input = sprintf('<input %s%s', $this->createAttributesString($inputAttributes), $closingBracket); if (null !== ($translator = $this->getTranslator())) { $label = $translator->translate($label, $this->getTranslatorTextDomain()); } if (!$element instanceof LabelAwareInterface || !$element->getLabelOption('disable_html_escape')) { $label = $escapeHtmlHelper($label); } $labelOpen = $labelHelper->openTag($labelAttributes); $template = $labelOpen . '%s%s' . $labelClose; switch ($labelPosition) { case self::LABEL_PREPEND: $markup = sprintf($template, $label, $input); break; case self::LABEL_APPEND: default: $markup = sprintf($template, $input, $label); break; } $combinedMarkup[] = $markup; } return implode($this->getSeparator(), $combinedMarkup); }
/** * @see \Zend\Form\View\Helper\FormMultiCheckbox::renderOptions() * @param \Zend\Form\Element\MultiCheckbox $oElement * @param array $aOptions * @param array $aSelectedOptions * @param array $aAttributes * @return string */ protected function renderOptions(MultiCheckbox $oElement, array $aOptions, array $aSelectedOptions, array $aAttributes) { $iIterator = 0; $aGlobalLabelAttributes = $oElement->getLabelAttributes() ?: $this->labelAttributes; $sMarkup = ''; $oLabelHelper = $this->getLabelHelper(); foreach ($aOptions as $key => $aOptionspec) { if (is_scalar($aOptionspec)) { $aOptionspec = array('label' => $aOptionspec, 'value' => $key); } $iIterator++; if ($iIterator > 1 && array_key_exists('id', $aAttributes)) { unset($aAttributes['id']); } //Option attributes $aInputAttributes = $aAttributes; if (isset($aOptionspec['attributes'])) { $aInputAttributes = \Zend\Stdlib\ArrayUtils::merge($aInputAttributes, $aOptionspec['attributes']); } //Option value $aInputAttributes['value'] = isset($aOptionspec['value']) ? $aOptionspec['value'] : ''; //Selected option if (in_array($aInputAttributes['value'], $aSelectedOptions)) { $aInputAttributes['checked'] = true; } elseif (isset($aOptionspec['selected'])) { $aInputAttributes['checked'] = !!$aOptionspec['selected']; } else { $aInputAttributes['checked'] = isset($aInputAttributes['selected']) && $aInputAttributes['type'] !== 'radio' && $aInputAttributes['selected'] != false; } //Disabled option if (isset($aOptionspec['disabled'])) { $aInputAttributes['disabled'] = !!$aOptionspec['disabled']; } else { $aInputAttributes['disabled'] = isset($aInputAttributes['disabled']) && $aInputAttributes['disabled'] != false; } //Render option $sOptionMarkup = sprintf('<input %s%s', $this->createAttributesString($aInputAttributes), $this->getInlineClosingBracket()); //Option label $sLabel = isset($aOptionspec['label']) ? $aOptionspec['label'] : ''; if ($sLabel) { $aLabelAttributes = $aGlobalLabelAttributes; if (isset($aOptionspec['label_attributes'])) { $aLabelAttributes = isset($aLabelAttributes) ? array_merge($aLabelAttributes, $aOptionspec['label_attributes']) : $aOptionspec['label_attributes']; } if (null !== ($oTranslator = $this->getTranslator())) { $sLabel = $oTranslator->translate($sLabel, $this->getTranslatorTextDomain()); } if (!$oElement instanceof \Zend\Form\LabelAwareInterface || !$oElement->getLabelOption('disable_html_escape')) { $sLabel = $this->getEscapeHtmlHelper()->__invoke($sLabel); } switch ($this->getLabelPosition()) { case self::LABEL_PREPEND: $sOptionMarkup = sprintf($oLabelHelper->openTag($aLabelAttributes) . '%s%s' . $oLabelHelper->closeTag(), $sLabel, $sOptionMarkup); break; case self::LABEL_APPEND: default: $sOptionMarkup = sprintf($oLabelHelper->openTag($aLabelAttributes) . '%s%s' . $oLabelHelper->closeTag(), $sOptionMarkup, $sLabel); break; } } $sMarkup .= ($sMarkup ? $this->getSeparator() : '') . $sOptionMarkup; } return $sMarkup; }