/** * Overwrite FormHelper::_selectOptions() * * - If $attributes['style'] is `<input type="checkbox">` then replace `<label>` position * - Returns an array of formatted OPTION/OPTGROUP elements * * @param array $elements Elements to format. * @param array $parents Parents for OPTGROUP. * @param bool $showParents Whether to show parents. * @param array $attributes HTML attributes. * @return array */ protected function _selectOptions($elements = array(), $parents = array(), $showParents = null, $attributes = array()) { $selectOptions = parent::_selectOptions($elements, $parents, $showParents, $attributes); if ($attributes['style'] === 'checkbox') { foreach ($selectOptions as $key => $option) { $option = preg_replace('/<div.*?>/', '', $option); $option = preg_replace('/<\\/div>/', '', $option); if (preg_match('/>(<label.*?>)/', $option, $match)) { $class = $attributes['class']; if (preg_match('/.* class="(.*)".*/', $match[1], $classMatch)) { $class = $classMatch[1] . ' ' . $attributes['class']; $match[1] = str_replace(' class="' . $classMatch[1] . '"', '', $match[1]); } $option = $match[1] . preg_replace('/<label.*?>/', ' ', $option); $option = preg_replace('/(<label.*?)(>)/', '$1 class="' . $class . '"$2', $option); } $selectOptions[$key] = $option; } } return $selectOptions; }
/** * Overwrite FormHelper::_selectOptions() * Remove form-control if added here as it would only be added to the div. * * @param array $elements * @param array $parents * @param bool $showParents * @param array $attributes * @return array */ protected function _selectOptions($elements = array(), $parents = array(), $showParents = null, $attributes = array()) { if ($attributes['style'] === 'checkbox') { if (!empty($attributes['class']) && $attributes['class'] === array('form-control')) { unset($attributes['class']); } } $selectOptions = parent::_selectOptions($elements, $parents, $showParents, $attributes); return $selectOptions; }