Button.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget;
  7. /**
  8. * Button widget
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Button extends \Magento\Backend\Block\Widget
  16. {
  17. /**
  18. * Define block template
  19. *
  20. * @return void
  21. */
  22. protected function _construct()
  23. {
  24. $this->setTemplate('Magento_Backend::widget/button.phtml');
  25. parent::_construct();
  26. }
  27. /**
  28. * Retrieve button type
  29. *
  30. * @return string
  31. */
  32. public function getType()
  33. {
  34. if (in_array($this->getData('type'), ['reset', 'submit'])) {
  35. return $this->getData('type');
  36. }
  37. return 'button';
  38. }
  39. /**
  40. * Retrieve onclick handler
  41. *
  42. * @return null|string
  43. */
  44. public function getOnClick()
  45. {
  46. return $this->getData('on_click') ?: $this->getData('onclick');
  47. }
  48. /**
  49. * Retrieve attributes html
  50. *
  51. * @return string
  52. */
  53. public function getAttributesHtml()
  54. {
  55. $disabled = $this->getDisabled() ? 'disabled' : '';
  56. $title = $this->getTitle();
  57. if (!$title) {
  58. $title = $this->getLabel();
  59. }
  60. $classes = [];
  61. $classes[] = 'action-default';
  62. $classes[] = 'scalable';
  63. if ($this->getClass()) {
  64. $classes[] = $this->getClass();
  65. }
  66. if ($disabled) {
  67. $classes[] = $disabled;
  68. }
  69. return $this->_attributesToHtml($this->_prepareAttributes($title, $classes, $disabled));
  70. }
  71. /**
  72. * Prepare attributes
  73. *
  74. * @param string $title
  75. * @param array $classes
  76. * @param string $disabled
  77. * @return array
  78. */
  79. protected function _prepareAttributes($title, $classes, $disabled)
  80. {
  81. $attributes = [
  82. 'id' => $this->getId(),
  83. 'name' => $this->getElementName(),
  84. 'title' => $title,
  85. 'type' => $this->getType(),
  86. 'class' => join(' ', $classes),
  87. 'onclick' => $this->getOnClick(),
  88. 'style' => $this->getStyle(),
  89. 'value' => $this->getValue(),
  90. 'disabled' => $disabled,
  91. ];
  92. if ($this->getDataAttribute()) {
  93. foreach ($this->getDataAttribute() as $key => $attr) {
  94. $attributes['data-' . $key] = is_scalar($attr) ? $attr : json_encode($attr);
  95. }
  96. }
  97. return $attributes;
  98. }
  99. /**
  100. * Attributes list to html
  101. *
  102. * @param array $attributes
  103. * @return string
  104. */
  105. protected function _attributesToHtml($attributes)
  106. {
  107. $html = '';
  108. foreach ($attributes as $attributeKey => $attributeValue) {
  109. if ($attributeValue === null || $attributeValue == '') {
  110. continue;
  111. }
  112. $html .= $attributeKey . '="' . $this->escapeHtmlAttr($attributeValue, false) . '" ';
  113. }
  114. return $html;
  115. }
  116. }