public function render(array $data, ContextInterface $context) { $this->_View->Html->css('Garderobe/BootstrapKit.switch/bootstrap-switch.min.css', ['block' => true]); $this->_View->Html->script('Garderobe/BootstrapKit.switch/bootstrap-switch.min.js', ['block' => true]); $js = "\n \$(function(){\n \$('.bootstrap-switcher').bootstrapSwitch();\n });\n "; $this->_View->Html->scriptBlock($js, ['block' => true]); if (!isset($data['class'])) { $data['class'] = 'bootstrap-switcher'; } else { $data['class'] .= ' bootstrap-switcher'; } $hidden = ''; if (!isset($data['hiddenField'])) { $data['hiddenField'] = true; } if ($data['hiddenField'] != true || $data['hiddenField'] !== false) { $hiddenOptions = ['type' => 'hidden', 'name' => $data['name'], 'val' => $data['hiddenField'] !== true ? $data['hiddenField'] : '0', 'form' => isset($data['form']) ? $data['form'] : null, 'secure' => false]; if (isset($data['disabled']) && $data['disabled']) { $hiddenOptions['disabled'] = 'disabled'; } $hidden = $this->_hidden->render($hiddenOptions, $context); } unset($data['hiddenField']); return $hidden . parent::render($data, $context); }
/** * test render escape * * @return void */ public function testRenderAttributes() { $label = new LabelWidget($this->templates); $data = ['text' => 'My > text', 'for' => 'some-id', 'id' => 'some-id', 'data-foo' => 'value']; $result = $label->render($data, $this->context); $expected = ['label' => ['id' => 'some-id', 'data-foo' => 'value', 'for' => 'some-id'], 'My > text', '/label']; $this->assertHtml($expected, $result); }
/** * Renders a label element for a given radio button. * * In the future this might be refactored into a separate widget as other * input types (multi-checkboxes) will also need labels generated. * * @param array $radio The input properties. * @param false|string|array $label The properties for a label. * @param string $input The input widget. * @param \Cake\View\Form\ContextInterface $context The form context. * @param bool $escape Whether or not to HTML escape the label. * @return string Generated label. */ protected function _renderLabel($radio, $label, $input, $context, $escape) { if ($label === false) { return false; } $labelAttrs = is_array($label) ? $label : []; $labelAttrs += ['for' => $radio['id'], 'escape' => $escape, 'text' => $radio['text'], 'templateVars' => $radio['templateVars'], 'input' => $input]; return $this->_label->render($labelAttrs, $context); }
/** * Ensure templateVars option is hooked up. * * @return void */ public function testRenderTemplateVars() { $this->templates->add(['label' => '<label custom="{{custom}}" {{attrs}}>{{text}}</label>']); $label = new LabelWidget($this->templates); $data = ['templateVars' => ['custom' => 'value'], 'text' => 'Label Text']; $result = $label->render($data, $this->context); $expected = ['label' => ['custom' => 'value'], 'Label Text', '/label']; $this->assertHtml($expected, $result); }
/** * Render a single checkbox & wrapper. * * @param array $checkbox An array containing checkbox key/value option pairs * @param \Cake\View\Form\ContextInterface $context Context object. * @return string */ protected function _renderInput($checkbox, $context) { $input = $this->_templates->format('checkbox', ['name' => $checkbox['name'] . '[]', 'value' => $checkbox['escape'] ? h($checkbox['value']) : $checkbox['value'], 'attrs' => $this->_templates->formatAttributes($checkbox, ['name', 'value', 'text'])]); $labelAttrs = ['for' => $checkbox['id'], 'escape' => $checkbox['escape'], 'text' => $checkbox['text'], 'input' => $input]; if (!empty($checkbox['checked']) && empty($labelAttrs['class'])) { $labelAttrs['class'] = 'selected'; } $label = $this->_label->render($labelAttrs, $context); return $this->_templates->format('checkboxWrapper', ['label' => $label, 'input' => $input]); }