UiComponent.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component\Wrapper;
  7. use Magento\Framework\View\Element\Template;
  8. use Magento\Framework\View\Element\UiComponentInterface;
  9. use Magento\Framework\View\Element\UiComponent\ContainerInterface;
  10. use Magento\Framework\View\Element\Template\Context as TemplateContext;
  11. /**
  12. * Class UiComponent
  13. *
  14. * Encapsulate UI Component to represent it as standard Layout Block
  15. */
  16. class UiComponent extends Template implements ContainerInterface
  17. {
  18. /**
  19. * Ui component
  20. *
  21. * @var UiComponentInterface
  22. */
  23. protected $component;
  24. /**
  25. * @var BlockFactory
  26. */
  27. protected $blockWrapperFactory;
  28. /**
  29. * Constructor
  30. *
  31. * @param TemplateContext $context
  32. * @param UiComponentInterface $component
  33. * @param BlockFactory $blockWrapperFactory
  34. * @param array $data
  35. */
  36. public function __construct(
  37. TemplateContext $context,
  38. UiComponentInterface $component,
  39. BlockFactory $blockWrapperFactory,
  40. array $data = []
  41. ) {
  42. $this->component = $component;
  43. $this->blockWrapperFactory = $blockWrapperFactory;
  44. $this->setNameInLayout($this->component->getName());
  45. parent::__construct($context, $data);
  46. }
  47. /**
  48. * Render block HTML
  49. *
  50. * @return string
  51. */
  52. protected function _toHtml()
  53. {
  54. foreach ($this->getChildNames() as $childName) {
  55. $childBlock = $this->getLayout()->getBlock($childName);
  56. if ($childBlock) {
  57. $wrapper = $this->blockWrapperFactory->create([
  58. 'block' => $childBlock,
  59. 'data' => [
  60. 'name' => 'block_' . $childName
  61. ]
  62. ]);
  63. $this->component->addComponent('block_' . $childName, $wrapper);
  64. }
  65. }
  66. $result = $this->component->render();
  67. return (string)$result;
  68. }
  69. }