Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Radios.php
Go to the documentation of this file.
1 <?php
13 
15 
16 class Radios extends AbstractElement
17 {
24  public function __construct(
25  Factory $factoryElement,
26  CollectionFactory $factoryCollection,
27  Escaper $escaper,
28  $data = []
29  ) {
30  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
31  $this->setType('radios');
32  }
33 
37  public function getElementHtml()
38  {
39  $html = '';
40  $value = $this->getValue();
41  if ($values = $this->getValues()) {
42  foreach ($values as $option) {
43  $html .= $this->_optionToHtml($option, $value);
44  }
45  }
46  $html .= $this->getAfterElementHtml();
47  return $html;
48  }
49 
55  protected function _optionToHtml($option, $selected)
56  {
57  $html = '<div class="admin__field admin__field-option">' .
58  '<input type="radio"' . $this->getRadioButtonAttributes($option);
59  if (is_array($option)) {
60  $html .= 'value="' . $this->_escape(
61  $option['value']
62  ) . '" class="admin__control-radio" id="' . $this->getHtmlId() . $option['value'] . '"';
63  if ($option['value'] == $selected) {
64  $html .= ' checked="checked"';
65  }
66  $html .= ' />';
67  $html .= '<label class="admin__field-label" for="' .
68  $this->getHtmlId() .
69  $option['value'] .
70  '"><span>' .
71  $option['label'] .
72  '</span></label>';
73  } elseif ($option instanceof \Magento\Framework\DataObject) {
74  $html .= 'id="' . $this->getHtmlId() . $option->getValue() . '"' . $option->serialize(
75  ['label', 'title', 'value', 'class', 'style']
76  );
77  if (in_array($option->getValue(), $selected)) {
78  $html .= ' checked="checked"';
79  }
80  $html .= ' />';
81  $html .= '<label class="inline" for="' .
82  $this->getHtmlId() .
83  $option->getValue() .
84  '">' .
85  $option->getLabel() .
86  '</label>';
87  }
88  $html .= '</div>';
89  return $html;
90  }
91 
95  public function getHtmlAttributes()
96  {
97  return array_merge(parent::getHtmlAttributes(), ['name']);
98  }
99 
104  protected function getRadioButtonAttributes($option)
105  {
106  $html = '';
107  foreach ($this->getHtmlAttributes() as $attribute) {
108  if ($value = $this->getDataUsingMethod($attribute, $option['value'])) {
109  $html .= ' ' . $attribute . '="' . $value . '" ';
110  }
111  }
112  return $html;
113  }
114 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getDataUsingMethod($key, $args=null)
Definition: DataObject.php:218
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16
__construct(Factory $factoryElement, CollectionFactory $factoryCollection, Escaper $escaper, $data=[])
Definition: Radios.php:24