Skip to content

Commit

Permalink
Merge pull request #3129 from AD7six/3.0-config-view
Browse files Browse the repository at this point in the history
update View NS to use config, not settings
  • Loading branch information
AD7six committed Mar 27, 2014
2 parents 7aa49e7 + 65bd8a8 commit f3ecce5
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 298 deletions.
10 changes: 9 additions & 1 deletion src/Core/InstanceConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ trait InstanceConfigTrait {
*/
protected $_config = [];

/**
* Whether the config property has already been configured with defaults
*
* @var bool
*/
protected $_configInitialized = false;

/**
* ### Usage
*
Expand Down Expand Up @@ -63,8 +70,9 @@ trait InstanceConfigTrait {
* @throws \Cake\Error\Exception When trying to set a key that is invalid
*/
public function config($key = null, $value = null) {
if ($this->_config === [] && $this->_defaultConfig) {
if (!$this->_configInitialized) {
$this->_config = $this->_defaultConfig;
$this->_configInitialized = true;
}

if (is_array($key) || func_num_args() === 2) {
Expand Down
22 changes: 14 additions & 8 deletions src/View/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\InstanceConfigTrait;
use Cake\Core\Object;
use Cake\Core\Plugin;
use Cake\Event\EventListener;
Expand Down Expand Up @@ -48,19 +49,21 @@
*/
class Helper extends Object implements EventListener {

use InstanceConfigTrait;

/**
* Settings for this helper.
* List of helpers used by this helper
*
* @var array
*/
public $settings = array();
public $helpers = array();

/**
* List of helpers used by this helper
* Default config for this helper.
*
* @var array
*/
public $helpers = array();
protected $_defaultConfig = [];

/**
* A helper lookup table used to lazy load helper objects.
Expand Down Expand Up @@ -141,14 +144,17 @@ class Helper extends Object implements EventListener {
* Default Constructor
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
* @param array $config Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
public function __construct(View $View, $config = array()) {
$this->_View = $View;
$this->request = $View->request;
if ($settings) {
$this->settings = Hash::merge($this->settings, $settings);

if ($config) {
$config = Hash::merge($this->_defaultConfig, $config);
}
$this->config($config);

if (!empty($this->helpers)) {
$this->_helperMap = $View->Helpers->normalizeArray($this->helpers);
}
Expand Down
102 changes: 48 additions & 54 deletions src/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,49 @@ class FormHelper extends Helper {
protected $_datetimeParts = ['year', 'month', 'day', 'hour', 'minute', 'second', 'meridian'];

/**
* Settings for the helper.
* Default config for the helper.
*
* @var array
*/
public $settings = [
protected $_defaultConfig = [
'errorClass' => 'form-error',
'typeMap' => [
'string' => 'text', 'datetime' => 'datetime', 'boolean' => 'checkbox',
'timestamp' => 'datetime', 'text' => 'textarea', 'time' => 'time',
'date' => 'date', 'float' => 'number', 'integer' => 'number',
'decimal' => 'number', 'binary' => 'file', 'uuid' => 'string'
],
'widgets' => [],
'registry' => null,
'templates' => [
'button' => '<button{{attrs}}>{{text}}</button>',
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
'checkboxContainer' => '<div class="checkbox">{{input}}{{label}}</div>',
'dateWidget' => '{{month}}{{day}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}',
'error' => '<div class="error-message">{{content}}</div>',
'errorList' => '<ul>{{content}}</ul>',
'errorItem' => '<li>{{text}}</li>',
'file' => '<input type="file" name="{{name}}"{{attrs}}>',
'fieldset' => '<fieldset>{{content}}</fieldset>',
'formstart' => '<form{{attrs}}>',
'formend' => '</form>',
'hiddenblock' => '<div style="display:none;">{{content}}</div>',
'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>',
'inputsubmit' => '<input type="{{type}}"{{attrs}}>',
'label' => '<label{{attrs}}>{{text}}</label>',
'legend' => '<legend>{{text}}</legend>',
'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
'radioContainer' => '{{input}}{{label}}',
'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
'formGroup' => '{{label}}{{input}}',
'checkboxFormGroup' => '{{input}}{{label}}',
'groupContainer' => '<div class="input {{type}}{{required}}">{{content}}</div>',
'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}{{error}}</div>',
'submitContainer' => '<div class="submit">{{content}}</div>',
]
];

Expand Down Expand Up @@ -130,55 +162,19 @@ class FormHelper extends Helper {
*/
protected $_contextProviders;

/**
* Default templates the FormHelper uses.
*
* @var array
*/
protected $_defaultTemplates = [
'button' => '<button{{attrs}}>{{text}}</button>',
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
'checkboxContainer' => '<div class="checkbox">{{input}}{{label}}</div>',
'dateWidget' => '{{month}}{{day}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}',
'error' => '<div class="error-message">{{content}}</div>',
'errorList' => '<ul>{{content}}</ul>',
'errorItem' => '<li>{{text}}</li>',
'file' => '<input type="file" name="{{name}}"{{attrs}}>',
'fieldset' => '<fieldset>{{content}}</fieldset>',
'formstart' => '<form{{attrs}}>',
'formend' => '</form>',
'hiddenblock' => '<div style="display:none;">{{content}}</div>',
'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>',
'inputsubmit' => '<input type="{{type}}"{{attrs}}>',
'label' => '<label{{attrs}}>{{text}}</label>',
'legend' => '<legend>{{text}}</legend>',
'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
'radioContainer' => '{{input}}{{label}}',
'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
'formGroup' => '{{label}}{{input}}',
'checkboxFormGroup' => '{{input}}{{label}}',
'groupContainer' => '<div class="input {{type}}{{required}}">{{content}}</div>',
'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}{{error}}</div>',
'submitContainer' => '<div class="submit">{{content}}</div>',
];

/**
* Construct the widgets and binds the default context providers
*
* @param \Cake\View\View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
* @param array $config Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
$settings += ['widgets' => [], 'templates' => null, 'registry' => null];
parent::__construct($View, $settings);
public function __construct(View $View, $config = array()) {
parent::__construct($View, $config);

$config = $this->config();

$this->initStringTemplates($this->_defaultTemplates);
$this->widgetRegistry($settings['registry'], $settings['widgets']);
unset($this->settings['widgets'], $this->settings['registry']);
$this->widgetRegistry($config['registry'], $config['widgets']);
$this->config(['widgets' => null, 'registry' => null]);

$this->_addDefaultContextProviders();
}
Expand All @@ -193,7 +189,7 @@ public function __construct(View $View, $settings = array()) {
public function widgetRegistry(WidgetRegistry $instance = null, $widgets = []) {
if ($instance === null) {
if ($this->_registry === null) {
$this->_registry = new WidgetRegistry($this->_templater, $widgets);
$this->_registry = new WidgetRegistry($this->templater(), $widgets);
}
return $this->_registry;
}
Expand Down Expand Up @@ -293,7 +289,7 @@ public function create($model = null, $options = []) {
];

$this->_idPrefix = $options['idPrefix'];
$templater = $this->getTemplater();
$templater = $this->templater();

if (!empty($options['templates']) && is_array($options['templates'])) {
$templater->add($options['templates']);
Expand Down Expand Up @@ -941,7 +937,7 @@ protected function _inputType($fieldName, $options) {
}

$internalType = $context->type($fieldName);
$map = $this->settings['typeMap'];
$map = $this->config('typeMap');
$type = isset($map[$internalType]) ? $map[$internalType] : 'text';
$fieldName = array_slice(explode('.', $fieldName), -1)[0];

Expand Down Expand Up @@ -1414,7 +1410,7 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
}

$out = $this->formatTemplate('formstart', [
'attrs' => $this->_templater->formatAttributes($formOptions)
'attrs' => $this->templater()->formatAttributes($formOptions)
]);
$out .= $this->hidden('_method', ['value' => $requestMethod]);
$out .= $this->_csrfField();
Expand Down Expand Up @@ -1517,7 +1513,7 @@ public function submit($caption = null, $options = []) {

$input = $this->formatTemplate('inputsubmit', [
'type' => $type,
'attrs' => $this->_templater->formatAttributes($options),
'attrs' => $this->templater()->formatAttributes($options),
]);

return $this->formatTemplate('submitContainer', [
Expand Down Expand Up @@ -2098,7 +2094,7 @@ protected function _initInputField($field, $options = []) {
unset($options['value'], $options['default']);

if ($context->hasError($field)) {
$options = $this->addClass($options, $this->settings['errorClass']);
$options = $this->addClass($options, $this->config('errorClass'));
}
if (!empty($options['disabled']) || $secure === static::SECURE_SKIP) {
return $options;
Expand Down Expand Up @@ -2242,9 +2238,7 @@ public function widget($name, array $data = []) {
* @return void
*/
public function resetTemplates() {
$reflection = new \ReflectionClass($this);
$properties = $reflection->getDefaultProperties();
$this->templates($properties['_defaultTemplates']);
$this->templates($this->_defaultConfig['templates']);
}

/**
Expand Down
Loading

0 comments on commit f3ecce5

Please sign in to comment.