Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will make re-use in other widgets easier. Once widgets are in a registry, this class will allow easy replacement of label generation everywhere.
- Loading branch information
Showing
2 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v3.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\View\Input; | ||
|
||
/** | ||
* Form 'widget' for creating labels. | ||
* | ||
* Generally this element is used by other widgets, | ||
* and FormHelper itself. | ||
*/ | ||
class Label { | ||
|
||
/** | ||
* Templates | ||
* | ||
* @var Cake\View\StringTemplate | ||
*/ | ||
protected $_templates; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param Cake\View\StringTemplate $templates | ||
*/ | ||
public function __construct($templates) { | ||
$this->_templates = $templates; | ||
} | ||
|
||
/** | ||
* Render a label widget. | ||
* | ||
* Accepts the following keys in $data: | ||
* | ||
* - `text` The text for the label. | ||
* - `input` The input that can be formatted into the label if the template allows it. | ||
* - `escape` Set to false to disable HTML escaping. | ||
* | ||
* All other attributes will be converted into HTML attributes. | ||
* | ||
* @param array $data | ||
* @return string | ||
*/ | ||
public function render($data) { | ||
$data += [ | ||
'text' => '', | ||
'input' => '', | ||
'escape' => true, | ||
]; | ||
|
||
return $this->_templates->format('label', [ | ||
'text' => $data['escape'] ? h($data['text']) : $data['text'], | ||
'input' => $data['input'], | ||
'attrs' => $this->_templates->formatAttributes($data, ['text', 'input']), | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v3.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\Test\TestCase\View\Input; | ||
|
||
use Cake\TestSuite\TestCase; | ||
use Cake\View\Input\Label; | ||
use Cake\View\StringTemplate; | ||
|
||
/** | ||
* Label test case. | ||
*/ | ||
class LabelTest extends TestCase { | ||
|
||
/** | ||
* setup method. | ||
* | ||
* @return void | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$templates = [ | ||
'label' => '<label{{attrs}}>{{text}}</label>', | ||
]; | ||
$this->templates = new StringTemplate($templates); | ||
} | ||
|
||
/** | ||
* test render | ||
* | ||
* @return void | ||
*/ | ||
public function testRender() { | ||
$label = new Label($this->templates); | ||
$data = [ | ||
'text' => 'My text', | ||
]; | ||
$result = $label->render($data); | ||
$expected = [ | ||
'label' => [], | ||
'My text', | ||
'/label' | ||
]; | ||
$this->assertTags($result, $expected); | ||
} | ||
|
||
/** | ||
* test render escape | ||
* | ||
* @return void | ||
*/ | ||
public function testRenderEscape() { | ||
$label = new Label($this->templates); | ||
$data = [ | ||
'text' => 'My > text', | ||
'for' => 'Some > value', | ||
'escape' => false, | ||
]; | ||
$result = $label->render($data); | ||
$expected = [ | ||
'label' => ['for' => 'Some > value'], | ||
'My > text', | ||
'/label' | ||
]; | ||
$this->assertTags($result, $expected); | ||
} | ||
|
||
/** | ||
* test render escape | ||
* | ||
* @return void | ||
*/ | ||
public function testRenderAttributes() { | ||
$label = new Label($this->templates); | ||
$data = [ | ||
'text' => 'My > text', | ||
'for' => 'some-id', | ||
'id' => 'some-id', | ||
'data-foo' => 'value', | ||
]; | ||
$result = $label->render($data); | ||
$expected = [ | ||
'label' => ['id' => 'some-id', 'data-foo' => 'value', 'for' => 'some-id'], | ||
'My > text', | ||
'/label' | ||
]; | ||
$this->assertTags($result, $expected); | ||
} | ||
|
||
} |