ActionPool.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\AbstractBlock;
  8. use Magento\Framework\View\Element\BlockInterface;
  9. use Magento\Framework\View\Element\UiComponent\Context;
  10. use Magento\Framework\View\Element\UiComponentInterface;
  11. use Magento\Framework\View\Element\UiComponent\Control\ActionPoolInterface;
  12. /**
  13. * Class ActionPool
  14. */
  15. class ActionPool implements ActionPoolInterface
  16. {
  17. /**
  18. * Actions toolbar block name
  19. */
  20. const ACTIONS_PAGE_TOOLBAR = 'page.actions.toolbar';
  21. /**
  22. * Render context
  23. *
  24. * @var Context
  25. */
  26. protected $context;
  27. /**
  28. * Actions pool
  29. *
  30. * @var Item[]
  31. */
  32. protected $items;
  33. /**
  34. * Button factory
  35. *
  36. * @var ItemFactory
  37. */
  38. protected $itemFactory;
  39. /**
  40. * @var AbstractBlock
  41. */
  42. protected $toolbarBlock;
  43. /**
  44. * Construct
  45. *
  46. * @param Context $context
  47. * @param ItemFactory $itemFactory
  48. */
  49. public function __construct(Context $context, ItemFactory $itemFactory)
  50. {
  51. $this->context = $context;
  52. $this->itemFactory = $itemFactory;
  53. }
  54. /**
  55. * Get toolbar block
  56. *
  57. * @return bool|BlockInterface
  58. */
  59. public function getToolbar()
  60. {
  61. return $this->context->getPageLayout()
  62. ? $this->context->getPageLayout()->getBlock(static::ACTIONS_PAGE_TOOLBAR)
  63. : false;
  64. }
  65. /**
  66. * Add button
  67. *
  68. * @param string $key
  69. * @param array $data
  70. * @param UiComponentInterface $component
  71. * @return void
  72. */
  73. public function add($key, array $data, UiComponentInterface $component)
  74. {
  75. $data['id'] = isset($data['id']) ? $data['id'] : $key;
  76. $toolbar = $this->getToolbar();
  77. if ($toolbar !== false) {
  78. $this->items[$key] = $this->itemFactory->create();
  79. $this->items[$key]->setData($data);
  80. $container = $this->createContainer($key, $component);
  81. $toolbar->setChild($key, $container);
  82. }
  83. }
  84. /**
  85. * Remove button
  86. *
  87. * @param string $key
  88. * @return void
  89. */
  90. public function remove($key)
  91. {
  92. unset($this->items[$key]);
  93. }
  94. /**
  95. * Update button
  96. *
  97. * @param string $key
  98. * @param array $data
  99. * @return void
  100. */
  101. public function update($key, array $data)
  102. {
  103. if (isset($this->items[$key])) {
  104. $this->items[$key]->setData($data);
  105. }
  106. }
  107. /**
  108. * Add html block
  109. *
  110. * @param string $type
  111. * @param string $name
  112. * @param array $arguments
  113. * @return void
  114. */
  115. public function addHtmlBlock($type, $name = '', array $arguments = [])
  116. {
  117. $toolbar = $this->getToolbar();
  118. $container = $this->context->getPageLayout()->createBlock($type, $name, $arguments);
  119. if ($toolbar) {
  120. $toolbar->setChild($name, $container);
  121. }
  122. }
  123. /**
  124. * Create button container
  125. *
  126. * @param string $key
  127. * @param UiComponentInterface $view
  128. * @return Container
  129. */
  130. protected function createContainer($key, UiComponentInterface $view)
  131. {
  132. $container = $this->context->getPageLayout()->createBlock(
  133. \Magento\Ui\Component\Control\Container::class,
  134. 'container-' . $view->getName() . '-' . $key,
  135. [
  136. 'data' => [
  137. 'button_item' => $this->items[$key],
  138. 'context' => $view,
  139. ]
  140. ]
  141. );
  142. return $container;
  143. }
  144. }