SaveSplitButton.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Ui\Component\Control;
  7. use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
  8. use Magento\Ui\Component\Control\Container;
  9. /**
  10. * Represents split-button with pre-configured options
  11. * Provide an ability to show drop-down list with options clicking on the "Save" button
  12. *
  13. * @api
  14. * @since 101.0.0
  15. */
  16. class SaveSplitButton implements ButtonProviderInterface
  17. {
  18. /**
  19. * @var string
  20. */
  21. private $targetName;
  22. /**
  23. * @param string $targetName
  24. */
  25. public function __construct(string $targetName)
  26. {
  27. $this->targetName = $targetName;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. * @since 101.0.0
  32. */
  33. public function getButtonData()
  34. {
  35. return [
  36. 'label' => __('Save &amp; Continue'),
  37. 'class' => 'save primary',
  38. 'data_attribute' => [
  39. 'mage-init' => [
  40. 'buttonAdapter' => [
  41. 'actions' => [
  42. [
  43. 'targetName' => $this->targetName,
  44. 'actionName' => 'save',
  45. 'params' => [
  46. // first param is redirect flag
  47. false,
  48. ]
  49. ]
  50. ]
  51. ]
  52. ]
  53. ],
  54. 'class_name' => Container::SPLIT_BUTTON,
  55. 'options' => $this->getOptions(),
  56. 'sort_order' => 40,
  57. ];
  58. }
  59. /**
  60. * @return array
  61. */
  62. private function getOptions(): array
  63. {
  64. $options = [
  65. [
  66. 'label' => __('Save &amp; Close'),
  67. 'data_attribute' => [
  68. 'mage-init' => [
  69. 'buttonAdapter' => [
  70. 'actions' => [
  71. [
  72. 'targetName' => $this->targetName,
  73. 'actionName' => 'save',
  74. 'params' => [
  75. // first param is redirect flag
  76. true,
  77. ],
  78. ],
  79. ],
  80. ],
  81. ],
  82. ],
  83. 'sort_order' => 10,
  84. ],
  85. [
  86. 'label' => __('Save &amp; New'),
  87. 'data_attribute' => [
  88. 'mage-init' => [
  89. 'buttonAdapter' => [
  90. 'actions' => [
  91. [
  92. 'targetName' => $this->targetName,
  93. 'actionName' => 'save',
  94. 'params' => [
  95. // first param is redirect flag, second is data that will be added to post
  96. // request
  97. true,
  98. [
  99. 'redirect_to_new' => 1,
  100. ],
  101. ],
  102. ],
  103. ],
  104. ],
  105. ],
  106. ],
  107. 'sort_order' => 20,
  108. ],
  109. ];
  110. return $options;
  111. }
  112. }