Multiline.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Form multiline text elements
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Data\Form\Element;
  12. use Magento\Framework\Escaper;
  13. class Multiline 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('text');
  29. $this->setLineCount(2);
  30. }
  31. /**
  32. * @return string[]
  33. */
  34. public function getHtmlAttributes()
  35. {
  36. return [
  37. 'type',
  38. 'title',
  39. 'class',
  40. 'style',
  41. 'onclick',
  42. 'onchange',
  43. 'disabled',
  44. 'maxlength',
  45. 'data-form-part',
  46. 'data-role',
  47. 'data-action'
  48. ];
  49. }
  50. /**
  51. * @param int $suffix
  52. * @param string $scopeLabel
  53. * @return string
  54. */
  55. public function getLabelHtml($suffix = 0, $scopeLabel = '')
  56. {
  57. return parent::getLabelHtml($suffix, $scopeLabel);
  58. }
  59. /**
  60. * Get element HTML
  61. *
  62. * @return string
  63. */
  64. public function getElementHtml()
  65. {
  66. $html = '';
  67. $lineCount = $this->getLineCount();
  68. for ($i = 0; $i < $lineCount; $i++) {
  69. if ($i == 0 && $this->getRequired()) {
  70. $this->setClass('input-text admin__control-text required-entry _required');
  71. } else {
  72. $this->setClass('input-text admin__control-text');
  73. }
  74. $html .= '<div class="multi-input admin__field-control"><input id="' .
  75. $this->getHtmlId() .
  76. $i .
  77. '" name="' .
  78. $this->getName() .
  79. '[' .
  80. $i .
  81. ']' .
  82. '" value="' .
  83. $this->getEscapedValue(
  84. $i
  85. ) . '" ' . $this->serialize(
  86. $this->getHtmlAttributes()
  87. ) . ' ' . $this->_getUiId(
  88. $i
  89. ) . '/>' . "\n";
  90. if ($i == 0) {
  91. $html .= $this->getAfterElementHtml();
  92. }
  93. $html .= '</div>';
  94. }
  95. return $html;
  96. }
  97. /**
  98. * @return mixed
  99. * @SuppressWarnings(PHPMD.NPathComplexity)
  100. */
  101. public function getDefaultHtml()
  102. {
  103. $html = '';
  104. $lineCount = $this->getLineCount();
  105. for ($i = 0; $i < $lineCount; $i++) {
  106. $html .= $this->getNoSpan() === true ? '' : '<span class="field-row">' . "\n";
  107. if ($i == 0) {
  108. $html .= '<label for="' .
  109. $this->getHtmlId() .
  110. $i .
  111. '">' .
  112. $this->getLabel() .
  113. ($this->getRequired() ? ' <span class="required">*</span>' : '') .
  114. '</label>' .
  115. "\n";
  116. if ($this->getRequired()) {
  117. $this->setClass('input-text required-entry');
  118. }
  119. } else {
  120. $this->setClass('input-text');
  121. $html .= '<label>&nbsp;</label>' . "\n";
  122. }
  123. $html .= '<input id="' .
  124. $this->getHtmlId() .
  125. $i .
  126. '" name="' .
  127. $this->getName() .
  128. '[' .
  129. $i .
  130. ']' .
  131. '" value="' .
  132. $this->getEscapedValue(
  133. $i
  134. ) . '"' . $this->serialize(
  135. $this->getHtmlAttributes()
  136. ) . ' />' . "\n";
  137. if ($i == 0) {
  138. $html .= $this->getAfterElementHtml();
  139. }
  140. $html .= $this->getNoSpan() === true ? '' : '</span>' . "\n";
  141. }
  142. return $html;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getEscapedValue($index = null)
  148. {
  149. $value = $this->getValue();
  150. if (is_string($value)) {
  151. $value = explode("\n", $value);
  152. if (is_array($value)) {
  153. $this->setValue($value);
  154. }
  155. }
  156. return parent::getEscapedValue($index);
  157. }
  158. }