Wrapper.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Block;
  7. use Magento\Framework\View\Element\Template;
  8. use Magento\Framework\View\Element\UiComponentInterface;
  9. use Magento\Ui\Model\UiComponentGenerator;
  10. /**
  11. * This block is wrapper for UI component, this done in order to save compatability with old
  12. * widgets mechanism
  13. */
  14. class Wrapper extends \Magento\Framework\View\Element\Template
  15. {
  16. /**
  17. * @var UiComponentGenerator
  18. */
  19. private $uiComponentGenerator;
  20. /**
  21. * Wrapper constructor.
  22. * @param Template\Context $context
  23. * @param UiComponentGenerator $uiComponentGenerator
  24. * @param array $data
  25. */
  26. public function __construct(Template\Context $context, UiComponentGenerator $uiComponentGenerator, array $data = [])
  27. {
  28. parent::__construct($context, $data);
  29. $this->uiComponentGenerator = $uiComponentGenerator;
  30. }
  31. /**
  32. * Add external data to data provider
  33. *
  34. * Can be usefull when we need to inject common data for few instances of UI components
  35. *
  36. * @param UiComponentInterface $uiComponent
  37. * @param array $widgetData
  38. * @return void
  39. */
  40. private function injectDataInDataSource(UiComponentInterface $uiComponent, array $widgetData)
  41. {
  42. $context = $uiComponent->getContext();
  43. $configData = $context->getDataProvider()->getConfigData();
  44. $context->getDataProvider()
  45. ->setConfigData(
  46. array_replace($configData, $widgetData)
  47. );
  48. }
  49. /**
  50. * This is a second way to configure Ui component on a fly, with different entire data
  51. * Instead of DataProvider it allows to launch few instances of one Ui Component on one page, depend
  52. * on entire data
  53. *
  54. * @param UiComponentInterface $uiComponent
  55. * @param array $data
  56. * @return void
  57. */
  58. private function addDataToChildComponents(UiComponentInterface $uiComponent, array $data)
  59. {
  60. foreach ($uiComponent->getChildComponents() as $childComponent) {
  61. if (isset($data[$childComponent->getName()]) && is_array($data[$childComponent->getName()])) {
  62. $childComponent->setData(
  63. 'config',
  64. array_replace_recursive(
  65. $childComponent->getData('config'),
  66. $data[$childComponent->getName()]
  67. )
  68. );
  69. }
  70. $this->addDataToChildComponents($childComponent, $data);
  71. }
  72. }
  73. /**
  74. * Create and render ui Component
  75. * Additional settings and data can be provided in this method
  76. * This data will be merged and can be used on store front with according ui component.
  77. *
  78. * @param array $data -> data, that can be injected to data source or to child components
  79. * @return string
  80. */
  81. public function renderApp($data = [])
  82. {
  83. /** @var \Magento\Ui\Component\AbstractComponent $uiComponent */
  84. $uiComponent = $this->uiComponentGenerator
  85. ->generateUiComponent($this->getData('uiComponent'), $this->getLayout());
  86. $this->injectDataInDataSource($uiComponent, $this->getData());
  87. $this->addDataToChildComponents($uiComponent, $data);
  88. return (string) $uiComponent->render();
  89. }
  90. }