Button.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component\Control;
  7. use Magento\Framework\View\Element\Template;
  8. use Magento\Framework\View\Element\UiComponent\Control\ControlInterface;
  9. /**
  10. * Class Button
  11. */
  12. class Button extends Template implements ControlInterface
  13. {
  14. /**
  15. * Define block template
  16. *
  17. * @return void
  18. */
  19. protected function _construct()
  20. {
  21. $this->setTemplate($this->getTemplatePath());
  22. parent::_construct();
  23. }
  24. /**
  25. * Retrieve template path
  26. *
  27. * @return string
  28. */
  29. protected function getTemplatePath()
  30. {
  31. return 'Magento_Ui::control/button/default.phtml';
  32. }
  33. /**
  34. * Retrieve button type
  35. *
  36. * @return string
  37. */
  38. public function getType()
  39. {
  40. if (in_array($this->getData('type'), ['reset', 'submit'])) {
  41. return $this->getData('type');
  42. }
  43. return 'button';
  44. }
  45. /**
  46. * Retrieve attributes html
  47. *
  48. * @return string
  49. */
  50. public function getAttributesHtml()
  51. {
  52. $disabled = $this->getDisabled() ? 'disabled' : '';
  53. $title = $this->getTitle();
  54. if (empty($title)) {
  55. $title = $this->getLabel();
  56. }
  57. $classes = ['action-', 'scalable'];
  58. if ($this->hasData('class')) {
  59. $classes[] = $this->getClass();
  60. }
  61. if ($disabled) {
  62. $classes[] = $disabled;
  63. }
  64. return $this->attributesToHtml($this->prepareAttributes($title, $classes, $disabled));
  65. }
  66. /**
  67. * Retrieve onclick handler
  68. *
  69. * @return string|null
  70. */
  71. public function getOnClick()
  72. {
  73. if ($this->hasData('on_click')) {
  74. return $this->getData('on_click');
  75. } else {
  76. $url = $this->hasData('url') ? $this->getData('url') : $this->getUrl();
  77. if (!empty($url)) {
  78. return sprintf("location.href = '%s';", $url);
  79. }
  80. return null;
  81. }
  82. }
  83. /**
  84. * Prepare attributes
  85. *
  86. * @param string $title
  87. * @param array $classes
  88. * @param string $disabled
  89. * @return array
  90. */
  91. protected function prepareAttributes($title, $classes, $disabled)
  92. {
  93. $attributes = [
  94. 'id' => $this->getId(),
  95. 'name' => $this->getElementName(),
  96. 'title' => $title,
  97. 'type' => $this->getType(),
  98. 'class' => implode(' ', $classes),
  99. 'onclick' => $this->getOnClick(),
  100. 'style' => $this->getStyle(),
  101. 'value' => $this->getValue(),
  102. 'disabled' => $disabled,
  103. ];
  104. if ($this->getDataAttribute()) {
  105. foreach ($this->getDataAttribute() as $key => $attr) {
  106. $attributes['data-' . $key] = is_scalar($attr) ? $attr : json_encode($attr);
  107. }
  108. }
  109. return $attributes;
  110. }
  111. /**
  112. * Attributes list to html
  113. *
  114. * @param array $attributes
  115. * @return string
  116. */
  117. protected function attributesToHtml($attributes)
  118. {
  119. $html = '';
  120. foreach ($attributes as $attributeKey => $attributeValue) {
  121. if ($attributeValue === null || $attributeValue == '') {
  122. continue;
  123. }
  124. $html .= $attributeKey . '="' . $this->escapeHtmlAttr($attributeValue, false) . '" ';
  125. }
  126. return $html;
  127. }
  128. }