Radios.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Radio buttons collection
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Data\Form\Element;
  12. use Magento\Framework\Escaper;
  13. class Radios 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('radios');
  29. }
  30. /**
  31. * @return string
  32. */
  33. public function getElementHtml()
  34. {
  35. $html = '';
  36. $value = $this->getValue();
  37. if ($values = $this->getValues()) {
  38. foreach ($values as $option) {
  39. $html .= $this->_optionToHtml($option, $value);
  40. }
  41. }
  42. $html .= $this->getAfterElementHtml();
  43. return $html;
  44. }
  45. /**
  46. * @param array $option
  47. * @param array $selected
  48. * @return string
  49. */
  50. protected function _optionToHtml($option, $selected)
  51. {
  52. $html = '<div class="admin__field admin__field-option">' .
  53. '<input type="radio"' . $this->getRadioButtonAttributes($option);
  54. if (is_array($option)) {
  55. $html .= 'value="' . $this->_escape(
  56. $option['value']
  57. ) . '" class="admin__control-radio" id="' . $this->getHtmlId() . $option['value'] . '"';
  58. if ($option['value'] == $selected) {
  59. $html .= ' checked="checked"';
  60. }
  61. $html .= ' />';
  62. $html .= '<label class="admin__field-label" for="' .
  63. $this->getHtmlId() .
  64. $option['value'] .
  65. '"><span>' .
  66. $option['label'] .
  67. '</span></label>';
  68. } elseif ($option instanceof \Magento\Framework\DataObject) {
  69. $html .= 'id="' . $this->getHtmlId() . $option->getValue() . '"' . $option->serialize(
  70. ['label', 'title', 'value', 'class', 'style']
  71. );
  72. if (in_array($option->getValue(), $selected)) {
  73. $html .= ' checked="checked"';
  74. }
  75. $html .= ' />';
  76. $html .= '<label class="inline" for="' .
  77. $this->getHtmlId() .
  78. $option->getValue() .
  79. '">' .
  80. $option->getLabel() .
  81. '</label>';
  82. }
  83. $html .= '</div>';
  84. return $html;
  85. }
  86. /**
  87. * @return array
  88. */
  89. public function getHtmlAttributes()
  90. {
  91. return array_merge(parent::getHtmlAttributes(), ['name']);
  92. }
  93. /**
  94. * @param array $option
  95. * @return string
  96. */
  97. protected function getRadioButtonAttributes($option)
  98. {
  99. $html = '';
  100. foreach ($this->getHtmlAttributes() as $attribute) {
  101. if ($value = $this->getDataUsingMethod($attribute, $option['value'])) {
  102. $html .= ' ' . $attribute . '="' . $value . '" ';
  103. }
  104. }
  105. return $html;
  106. }
  107. }