Column.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component\Listing\Columns;
  7. use Magento\Ui\Component\AbstractComponent;
  8. use Magento\Framework\View\Element\UiComponentFactory;
  9. use Magento\Framework\View\Element\UiComponentInterface;
  10. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  11. /**
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Column extends AbstractComponent implements ColumnInterface
  16. {
  17. const NAME = 'column';
  18. /**
  19. * Wrapped component
  20. *
  21. * @var UiComponentInterface
  22. */
  23. protected $wrappedComponent;
  24. /**
  25. * UI component factory
  26. *
  27. * @var UiComponentFactory
  28. */
  29. protected $uiComponentFactory;
  30. /**
  31. * Constructor
  32. *
  33. * @param ContextInterface $context
  34. * @param UiComponentFactory $uiComponentFactory
  35. * @param array $components
  36. * @param array $data
  37. */
  38. public function __construct(
  39. ContextInterface $context,
  40. UiComponentFactory $uiComponentFactory,
  41. array $components = [],
  42. array $data = []
  43. ) {
  44. $this->uiComponentFactory = $uiComponentFactory;
  45. parent::__construct($context, $components, $data);
  46. }
  47. /**
  48. * Get component name
  49. *
  50. * @return string
  51. */
  52. public function getComponentName()
  53. {
  54. return static::NAME . '.' . $this->getData('config/dataType');
  55. }
  56. /**
  57. * Prepare component configuration
  58. *
  59. * @return void
  60. */
  61. public function prepare()
  62. {
  63. $this->addFieldToSelect();
  64. $dataType = $this->getData('config/dataType');
  65. if ($dataType) {
  66. $this->wrappedComponent = $this->uiComponentFactory->create(
  67. $this->getName(),
  68. $dataType,
  69. array_merge(['context' => $this->getContext()], (array) $this->getData())
  70. );
  71. $this->wrappedComponent->prepare();
  72. $wrappedComponentConfig = $this->getJsConfig($this->wrappedComponent);
  73. // Merge JS configuration with wrapped component configuration
  74. $jsConfig = array_replace_recursive($wrappedComponentConfig, $this->getJsConfig($this));
  75. $this->setData('js_config', $jsConfig);
  76. $this->setData(
  77. 'config',
  78. array_replace_recursive(
  79. (array)$this->wrappedComponent->getData('config'),
  80. (array)$this->getData('config')
  81. )
  82. );
  83. }
  84. $this->applySorting();
  85. parent::prepare();
  86. }
  87. /**
  88. * To prepare items of a column
  89. *
  90. * @param array $items
  91. * @return array
  92. */
  93. public function prepareItems(array & $items)
  94. {
  95. return $items;
  96. }
  97. /**
  98. * Add field to select
  99. * @return void
  100. */
  101. protected function addFieldToSelect()
  102. {
  103. if ($this->getData('config/add_field')) {
  104. $this->getContext()->getDataProvider()->addField($this->getName());
  105. }
  106. }
  107. /**
  108. * Apply sorting
  109. *
  110. * @return void
  111. */
  112. protected function applySorting()
  113. {
  114. $sorting = $this->getContext()->getRequestParam('sorting');
  115. $isSortable = $this->getData('config/sortable');
  116. if ($isSortable !== false
  117. && !empty($sorting['field'])
  118. && !empty($sorting['direction'])
  119. && $sorting['field'] === $this->getName()
  120. ) {
  121. $this->getContext()->getDataProvider()->addOrder(
  122. $this->getName(),
  123. strtoupper($sorting['direction'])
  124. );
  125. }
  126. }
  127. }