CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • BasicWidget
  • ButtonWidget
  • CheckboxWidget
  • DateTimeWidget
  • FileWidget
  • LabelWidget
  • MultiCheckboxWidget
  • NestingLabelWidget
  • RadioWidget
  • SelectBoxWidget
  • TextareaWidget
  • WidgetLocator

Interfaces

  • WidgetInterface
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\View\Widget;
 16: 
 17: use Cake\View\Form\ContextInterface;
 18: use Cake\View\Helper\IdGeneratorTrait;
 19: use Traversable;
 20: 
 21: /**
 22:  * Input widget class for generating a set of radio buttons.
 23:  *
 24:  * This class is intended as an internal implementation detail
 25:  * of Cake\View\Helper\FormHelper and is not intended for direct use.
 26:  */
 27: class RadioWidget implements WidgetInterface
 28: {
 29:     use IdGeneratorTrait;
 30: 
 31:     /**
 32:      * Template instance.
 33:      *
 34:      * @var \Cake\View\StringTemplate
 35:      */
 36:     protected $_templates;
 37: 
 38:     /**
 39:      * Label instance.
 40:      *
 41:      * @var \Cake\View\Widget\LabelWidget
 42:      */
 43:     protected $_label;
 44: 
 45:     /**
 46:      * Constructor
 47:      *
 48:      * This class uses a few templates:
 49:      *
 50:      * - `radio` Used to generate the input for a radio button.
 51:      *   Can use the following variables `name`, `value`, `attrs`.
 52:      * - `radioWrapper` Used to generate the container element for
 53:      *   the radio + input element. Can use the `input` and `label`
 54:      *   variables.
 55:      *
 56:      * @param \Cake\View\StringTemplate $templates Templates list.
 57:      * @param \Cake\View\Widget\LabelWidget $label Label widget instance.
 58:      */
 59:     public function __construct($templates, $label)
 60:     {
 61:         $this->_templates = $templates;
 62:         $this->_label = $label;
 63:     }
 64: 
 65:     /**
 66:      * Render a set of radio buttons.
 67:      *
 68:      * Data supports the following keys:
 69:      *
 70:      * - `name` - Set the input name.
 71:      * - `options` - An array of options. See below for more information.
 72:      * - `disabled` - Either true or an array of inputs to disable.
 73:      *    When true, the select element will be disabled.
 74:      * - `val` - A string of the option to mark as selected.
 75:      * - `label` - Either false to disable label generation, or
 76:      *   an array of attributes for all labels.
 77:      * - `required` - Set to true to add the required attribute
 78:      *   on all generated radios.
 79:      * - `idPrefix` Prefix for generated ID attributes.
 80:      *
 81:      * @param array $data The data to build radio buttons with.
 82:      * @param \Cake\View\Form\ContextInterface $context The current form context.
 83:      * @return string
 84:      */
 85:     public function render(array $data, ContextInterface $context)
 86:     {
 87:         $data += [
 88:             'name' => '',
 89:             'options' => [],
 90:             'disabled' => null,
 91:             'val' => null,
 92:             'escape' => true,
 93:             'label' => true,
 94:             'empty' => false,
 95:             'idPrefix' => null,
 96:             'templateVars' => [],
 97:         ];
 98:         if ($data['options'] instanceof Traversable) {
 99:             $options = iterator_to_array($data['options']);
100:         } else {
101:             $options = (array)$data['options'];
102:         }
103: 
104:         if (!empty($data['empty'])) {
105:             $empty = $data['empty'] === true ? 'empty' : $data['empty'];
106:             $options = ['' => $empty] + $options;
107:         }
108:         unset($data['empty']);
109: 
110:         $this->_idPrefix = $data['idPrefix'];
111:         $this->_clearIds();
112:         $opts = [];
113:         foreach ($options as $val => $text) {
114:             $opts[] = $this->_renderInput($val, $text, $data, $context);
115:         }
116: 
117:         return implode('', $opts);
118:     }
119: 
120:     /**
121:      * Disabled attribute detection.
122:      *
123:      * @param array $radio Radio info.
124:      * @param array|true|null $disabled The disabled values.
125:      * @return bool
126:      */
127:     protected function _isDisabled($radio, $disabled)
128:     {
129:         if (!$disabled) {
130:             return false;
131:         }
132:         if ($disabled === true) {
133:             return true;
134:         }
135:         $isNumeric = is_numeric($radio['value']);
136: 
137:         return (!is_array($disabled) || in_array((string)$radio['value'], $disabled, !$isNumeric));
138:     }
139: 
140:     /**
141:      * Renders a single radio input and label.
142:      *
143:      * @param string|int $val The value of the radio input.
144:      * @param string|array $text The label text, or complex radio type.
145:      * @param array $data Additional options for input generation.
146:      * @param \Cake\View\Form\ContextInterface $context The form context
147:      * @return string
148:      */
149:     protected function _renderInput($val, $text, $data, $context)
150:     {
151:         $escape = $data['escape'];
152:         if (is_int($val) && isset($text['text'], $text['value'])) {
153:             $radio = $text;
154:         } else {
155:             $radio = ['value' => $val, 'text' => $text];
156:         }
157:         $radio['name'] = $data['name'];
158: 
159:         if (!isset($radio['templateVars'])) {
160:             $radio['templateVars'] = [];
161:         }
162:         if (!empty($data['templateVars'])) {
163:             $radio['templateVars'] = array_merge($data['templateVars'], $radio['templateVars']);
164:         }
165: 
166:         if (empty($radio['id'])) {
167:             if (isset($data['id'])) {
168:                 $radio['id'] = $data['id'] . '-' . trim(
169:                     $this->_idSuffix($radio['value']),
170:                     '-'
171:                 );
172:             } else {
173:                 $radio['id'] = $this->_id($radio['name'], $radio['value']);
174:             }
175:         }
176:         if (isset($data['val']) && is_bool($data['val'])) {
177:             $data['val'] = $data['val'] ? 1 : 0;
178:         }
179:         if (isset($data['val']) && (string)$data['val'] === (string)$radio['value']) {
180:             $radio['checked'] = true;
181:             $radio['templateVars']['activeClass'] = 'active';
182:         }
183: 
184:         if (!is_bool($data['label']) && isset($radio['checked']) && $radio['checked']) {
185:             $data['label'] = $this->_templates->addClass($data['label'], 'selected');
186:         }
187: 
188:         $radio['disabled'] = $this->_isDisabled($radio, $data['disabled']);
189:         if (!empty($data['required'])) {
190:             $radio['required'] = true;
191:         }
192:         if (!empty($data['form'])) {
193:             $radio['form'] = $data['form'];
194:         }
195: 
196:         $input = $this->_templates->format('radio', [
197:             'name' => $radio['name'],
198:             'value' => $escape ? h($radio['value']) : $radio['value'],
199:             'templateVars' => $radio['templateVars'],
200:             'attrs' => $this->_templates->formatAttributes($radio + $data, ['name', 'value', 'text', 'options', 'label', 'val', 'type']),
201:         ]);
202: 
203:         $label = $this->_renderLabel(
204:             $radio,
205:             $data['label'],
206:             $input,
207:             $context,
208:             $escape
209:         );
210: 
211:         if ($label === false &&
212:             strpos($this->_templates->get('radioWrapper'), '{{input}}') === false
213:         ) {
214:             $label = $input;
215:         }
216: 
217:         return $this->_templates->format('radioWrapper', [
218:             'input' => $input,
219:             'label' => $label,
220:             'templateVars' => $data['templateVars'],
221:         ]);
222:     }
223: 
224:     /**
225:      * Renders a label element for a given radio button.
226:      *
227:      * In the future this might be refactored into a separate widget as other
228:      * input types (multi-checkboxes) will also need labels generated.
229:      *
230:      * @param array $radio The input properties.
231:      * @param array|string|false $label The properties for a label.
232:      * @param string $input The input widget.
233:      * @param \Cake\View\Form\ContextInterface $context The form context.
234:      * @param bool $escape Whether or not to HTML escape the label.
235:      * @return string|bool Generated label.
236:      */
237:     protected function _renderLabel($radio, $label, $input, $context, $escape)
238:     {
239:         if (isset($radio['label'])) {
240:             $label = $radio['label'];
241:         } elseif ($label === false) {
242:             return false;
243:         }
244:         $labelAttrs = is_array($label) ? $label : [];
245:         $labelAttrs += [
246:             'for' => $radio['id'],
247:             'escape' => $escape,
248:             'text' => $radio['text'],
249:             'templateVars' => $radio['templateVars'],
250:             'input' => $input,
251:         ];
252: 
253:         return $this->_label->render($labelAttrs, $context);
254:     }
255: 
256:     /**
257:      * {@inheritDoc}
258:      */
259:     public function secureFields(array $data)
260:     {
261:         return [$data['name']];
262:     }
263: }
264: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs