Fieldset.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\Data\Form;
  8. use Magento\Framework\Escaper;
  9. /**
  10. * Form fieldset
  11. *
  12. * @api
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @since 100.0.2
  15. */
  16. class Fieldset extends AbstractElement
  17. {
  18. /**
  19. * @param Factory $factoryElement
  20. * @param CollectionFactory $factoryCollection
  21. * @param Escaper $escaper
  22. * @param array $data
  23. */
  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->_renderer = Form::getFieldsetRenderer();
  32. $this->setType('fieldset');
  33. if (isset($data['advancedSection'])) {
  34. $this->setAdvancedLabel($data['advancedSection']);
  35. }
  36. }
  37. /**
  38. * Get elements html
  39. *
  40. * @return string
  41. */
  42. public function getElementHtml()
  43. {
  44. $html = $this->getBeforeElementHtml();
  45. $html .= '<fieldset area-hidden="false" id="' . $this->getHtmlId() . '"' . $this->serialize(
  46. ['class']
  47. ) . $this->_getUiId() . '>' . "\n";
  48. if ($this->getLegend()) {
  49. $html .= '<legend ' . $this->_getUiId('legend') . '>' . $this->getLegend() . '</legend>' . "\n";
  50. }
  51. $html .= $this->getChildrenHtml();
  52. $html .= '</fieldset>' . "\n";
  53. $html .= $this->getAfterElementHtml();
  54. return $html;
  55. }
  56. /**
  57. * Get Children element's array
  58. *
  59. * @return AbstractElement[]
  60. */
  61. public function getChildren()
  62. {
  63. $elements = [];
  64. foreach ($this->getElements() as $element) {
  65. if ($element->getType() != 'fieldset') {
  66. $elements[] = $element;
  67. }
  68. }
  69. return $elements;
  70. }
  71. /**
  72. * Get Children element's html
  73. *
  74. * @return string
  75. */
  76. public function getChildrenHtml()
  77. {
  78. return $this->_elementsToHtml($this->getChildren());
  79. }
  80. /**
  81. * Get Basic elements' array
  82. *
  83. * @return AbstractElement[]
  84. */
  85. public function getBasicChildren()
  86. {
  87. $elements = [];
  88. foreach ($this->getElements() as $element) {
  89. if (!$element->isAdvanced()) {
  90. $elements[] = $element;
  91. }
  92. }
  93. return $elements;
  94. }
  95. /**
  96. * Get Basic elements' html in sorted order
  97. *
  98. * @return string
  99. */
  100. public function getBasicChildrenHtml()
  101. {
  102. return $this->_elementsToHtml($this->getBasicChildren());
  103. }
  104. /**
  105. * Get Number of Basic Children
  106. *
  107. * @return int
  108. */
  109. public function getCountBasicChildren()
  110. {
  111. return count($this->getBasicChildren());
  112. }
  113. /**
  114. * Get Advanced elements'
  115. *
  116. * @return array
  117. */
  118. public function getAdvancedChildren()
  119. {
  120. $elements = [];
  121. foreach ($this->getElements() as $element) {
  122. if ($element->isAdvanced()) {
  123. $elements[] = $element;
  124. }
  125. }
  126. return $elements;
  127. }
  128. /**
  129. * Get Advanced elements' html in sorted order
  130. *
  131. * @return string
  132. */
  133. public function getAdvancedChildrenHtml()
  134. {
  135. return $this->_elementsToHtml($this->getAdvancedChildren());
  136. }
  137. /**
  138. * Whether fieldset contains advance section
  139. *
  140. * @return bool
  141. */
  142. public function hasAdvanced()
  143. {
  144. foreach ($this->getElements() as $element) {
  145. if ($element->isAdvanced()) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. /**
  152. * Get SubFieldset
  153. *
  154. * @return AbstractElement[]
  155. */
  156. public function getSubFieldset()
  157. {
  158. $elements = [];
  159. foreach ($this->getElements() as $element) {
  160. if ($element->getType() == 'fieldset' && !$element->isAdvanced()) {
  161. $elements[] = $element;
  162. }
  163. }
  164. return $elements;
  165. }
  166. /**
  167. * Enter description here...
  168. *
  169. * @return string
  170. */
  171. public function getSubFieldsetHtml()
  172. {
  173. return $this->_elementsToHtml($this->getSubFieldset());
  174. }
  175. /**
  176. * Enter description here...
  177. *
  178. * @return string
  179. */
  180. public function getDefaultHtml()
  181. {
  182. $html = '<div><h4 class="icon-head head-edit-form fieldset-legend">' . $this->getLegend() . '</h4>' . "\n";
  183. $html .= $this->getElementHtml();
  184. $html .= '</div>';
  185. return $html;
  186. }
  187. /**
  188. * Add field to fieldset
  189. *
  190. * @param string $elementId
  191. * @param string $type
  192. * @param array $config
  193. * @param bool $after
  194. * @param bool $isAdvanced
  195. * @return AbstractElement
  196. */
  197. public function addField($elementId, $type, $config, $after = false, $isAdvanced = false)
  198. {
  199. $element = parent::addField($elementId, $type, $config, $after);
  200. if ($renderer = Form::getFieldsetElementRenderer()) {
  201. $element->setRenderer($renderer);
  202. }
  203. $element->setAdvanced($isAdvanced);
  204. return $element;
  205. }
  206. /**
  207. * Return elements as html string
  208. *
  209. * @param AbstractElement[] $elements
  210. * @return string
  211. */
  212. protected function _elementsToHtml($elements)
  213. {
  214. $html = '';
  215. foreach ($elements as $element) {
  216. $html .= $element->toHtml();
  217. }
  218. return $html;
  219. }
  220. }