Checkbox.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Form\Element;
  7. use Magento\Framework\Escaper;
  8. /**
  9. * Form checkbox element
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Checkbox extends AbstractElement
  14. {
  15. /**
  16. * @param Factory $factoryElement
  17. * @param CollectionFactory $factoryCollection
  18. * @param Escaper $escaper
  19. * @param array $data
  20. */
  21. public function __construct(
  22. Factory $factoryElement,
  23. CollectionFactory $factoryCollection,
  24. Escaper $escaper,
  25. $data = []
  26. ) {
  27. parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
  28. $this->setType('checkbox');
  29. $this->setExtType('checkbox');
  30. }
  31. /**
  32. * @return string[]
  33. */
  34. public function getHtmlAttributes()
  35. {
  36. return [
  37. 'type',
  38. 'title',
  39. 'class',
  40. 'style',
  41. 'checked',
  42. 'onclick',
  43. 'onchange',
  44. 'disabled',
  45. 'tabindex',
  46. 'data-form-part',
  47. 'data-role',
  48. 'data-action'
  49. ];
  50. }
  51. /**
  52. * @return string
  53. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  54. */
  55. public function getElementHtml()
  56. {
  57. if ($checked = $this->getChecked()) {
  58. $this->setData('checked', true);
  59. } else {
  60. $this->unsetData('checked');
  61. }
  62. return parent::getElementHtml();
  63. }
  64. /**
  65. * Set check status of checkbox
  66. *
  67. * @param bool $value
  68. * @return Checkbox
  69. */
  70. public function setIsChecked($value = false)
  71. {
  72. $this->setData('checked', $value);
  73. return $this;
  74. }
  75. /**
  76. * Return check status of checkbox
  77. *
  78. * @return bool
  79. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  80. */
  81. public function getIsChecked()
  82. {
  83. return $this->getData('checked');
  84. }
  85. }