BasicWidget.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View\Widget;
  16. use Cake\View\Form\ContextInterface;
  17. use Cake\View\Widget\WidgetInterface;
  18. /**
  19. * Basic input class.
  20. *
  21. * This input class can be used to render basic simple
  22. * input elements like hidden, text, email, tel and other
  23. * types.
  24. */
  25. class BasicWidget implements WidgetInterface
  26. {
  27. /**
  28. * StringTemplate instance.
  29. *
  30. * @var \Cake\View\StringTemplate
  31. */
  32. protected $_templates;
  33. /**
  34. * Constructor.
  35. *
  36. * @param \Cake\View\StringTemplate $templates Templates list.
  37. */
  38. public function __construct($templates)
  39. {
  40. $this->_templates = $templates;
  41. }
  42. /**
  43. * Render a text widget or other simple widget like email/tel/number.
  44. *
  45. * This method accepts a number of keys:
  46. *
  47. * - `name` The name attribute.
  48. * - `val` The value attribute.
  49. * - `escape` Set to false to disable escaping on all attributes.
  50. *
  51. * Any other keys provided in $data will be converted into HTML attributes.
  52. *
  53. * @param array $data The data to build an input with.
  54. * @param \Cake\View\Form\ContextInterface $context The current form context.
  55. * @return string
  56. */
  57. public function render(array $data, ContextInterface $context)
  58. {
  59. $data += [
  60. 'name' => '',
  61. 'val' => null,
  62. 'type' => 'text',
  63. 'escape' => true,
  64. 'templateParams' => []
  65. ];
  66. $data['value'] = $data['val'];
  67. unset($data['val']);
  68. return $this->_templates->format('input', [
  69. 'name' => $data['name'],
  70. 'type' => $data['type'],
  71. 'templateParams' => $data['templateParams'],
  72. 'attrs' => $this->_templates->formatAttributes(
  73. $data,
  74. ['name', 'type']
  75. ),
  76. ]);
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function secureFields(array $data)
  82. {
  83. if (!isset($data['name']) || $data['name'] === '') {
  84. return [];
  85. }
  86. return [$data['name']];
  87. }
  88. }