Bookmark.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component;
  7. use Magento\Framework\Api\FilterBuilder;
  8. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  9. use Magento\Ui\Api\BookmarkManagementInterface;
  10. use Magento\Ui\Api\BookmarkRepositoryInterface;
  11. /**
  12. * Class Bookmark
  13. */
  14. class Bookmark extends AbstractComponent
  15. {
  16. const NAME = 'bookmark';
  17. /**
  18. * @var BookmarkRepositoryInterface
  19. */
  20. protected $bookmarkRepository;
  21. /**
  22. * @var FilterBuilder
  23. */
  24. protected $filterBuilder;
  25. /**
  26. * @var BookmarkManagementInterface
  27. */
  28. protected $bookmarkManagement;
  29. /**
  30. * @param ContextInterface $context
  31. * @param BookmarkRepositoryInterface $bookmarkRepository
  32. * @param BookmarkManagementInterface $bookmarkManagement
  33. * @param array $components
  34. * @param array $data
  35. */
  36. public function __construct(
  37. ContextInterface $context,
  38. BookmarkRepositoryInterface $bookmarkRepository,
  39. BookmarkManagementInterface $bookmarkManagement,
  40. array $components = [],
  41. array $data = []
  42. ) {
  43. parent::__construct($context, $components, $data);
  44. $this->bookmarkRepository = $bookmarkRepository;
  45. $this->bookmarkManagement = $bookmarkManagement;
  46. }
  47. /**
  48. * Get component name
  49. *
  50. * @return string
  51. */
  52. public function getComponentName()
  53. {
  54. return static::NAME;
  55. }
  56. /**
  57. * Register component
  58. *
  59. * @return void
  60. */
  61. public function prepare()
  62. {
  63. $namespace = $this->getContext()->getRequestParam('namespace', $this->getContext()->getNamespace());
  64. $config = [];
  65. if (!empty($namespace)) {
  66. $bookmarks = $this->bookmarkManagement->loadByNamespace($namespace);
  67. /** @var \Magento\Ui\Api\Data\BookmarkInterface $bookmark */
  68. foreach ($bookmarks->getItems() as $bookmark) {
  69. if ($bookmark->isCurrent()) {
  70. $config['activeIndex'] = $bookmark->getIdentifier();
  71. }
  72. $config = array_merge_recursive($config, $bookmark->getConfig());
  73. }
  74. }
  75. $this->setData('config', array_replace_recursive($config, $this->getConfiguration()));
  76. parent::prepare();
  77. $jsConfig = $this->getConfiguration();
  78. $this->getContext()->addComponentDefinition($this->getComponentName(), $jsConfig);
  79. }
  80. }