Widget.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block;
  7. /**
  8. * Base widget class
  9. *
  10. * @api
  11. * @SuppressWarnings(PHPMD.NumberOfChildren)
  12. * @since 100.0.2
  13. */
  14. class Widget extends \Magento\Backend\Block\Template
  15. {
  16. /**
  17. * @return string
  18. */
  19. public function getId()
  20. {
  21. if (null === $this->getData('id')) {
  22. $this->setData('id', $this->mathRandom->getUniqueHash('id_'));
  23. }
  24. return $this->getData('id');
  25. }
  26. /**
  27. * Get HTML ID with specified suffix
  28. *
  29. * @param string $suffix
  30. * @return string
  31. */
  32. public function getSuffixId($suffix)
  33. {
  34. return "{$this->getId()}_{$suffix}";
  35. }
  36. /**
  37. * @return string
  38. */
  39. public function getHtmlId()
  40. {
  41. return $this->getId();
  42. }
  43. /**
  44. * Get current url
  45. *
  46. * @param array $params url parameters
  47. * @return string current url
  48. */
  49. public function getCurrentUrl($params = [])
  50. {
  51. if (!isset($params['_current'])) {
  52. $params['_current'] = true;
  53. }
  54. return $this->getUrl('*/*/*', $params);
  55. }
  56. /**
  57. * @param string $label
  58. * @param string|null $title
  59. * @param string|null $link
  60. * @return void
  61. */
  62. protected function _addBreadcrumb($label, $title = null, $link = null)
  63. {
  64. $this->getLayout()->getBlock('breadcrumbs')->addLink($label, $title, $link);
  65. }
  66. /**
  67. * Create button and return its html
  68. *
  69. * @param string $label
  70. * @param string $onclick
  71. * @param string $class
  72. * @param string $buttonId
  73. * @param array $dataAttr
  74. * @return string
  75. */
  76. public function getButtonHtml($label, $onclick, $class = '', $buttonId = null, $dataAttr = [])
  77. {
  78. return $this->getLayout()->createBlock(
  79. \Magento\Backend\Block\Widget\Button::class
  80. )->setData(
  81. ['label' => $label, 'onclick' => $onclick, 'class' => $class, 'type' => 'button', 'id' => $buttonId]
  82. )->setDataAttribute(
  83. $dataAttr
  84. )->toHtml();
  85. }
  86. }