Grid.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Block\Adminhtml\Page;
  7. /**
  8. * Adminhtml cms pages grid
  9. */
  10. class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
  11. {
  12. /**
  13. * @var \Magento\Cms\Model\ResourceModel\Page\CollectionFactory
  14. */
  15. protected $_collectionFactory;
  16. /**
  17. * @var \Magento\Cms\Model\Page
  18. */
  19. protected $_cmsPage;
  20. /**
  21. * @var \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface
  22. */
  23. protected $pageLayoutBuilder;
  24. /**
  25. * @param \Magento\Backend\Block\Template\Context $context
  26. * @param \Magento\Backend\Helper\Data $backendHelper
  27. * @param \Magento\Cms\Model\Page $cmsPage
  28. * @param \Magento\Cms\Model\ResourceModel\Page\CollectionFactory $collectionFactory
  29. * @param \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface $pageLayoutBuilder
  30. * @param array $data
  31. */
  32. public function __construct(
  33. \Magento\Backend\Block\Template\Context $context,
  34. \Magento\Backend\Helper\Data $backendHelper,
  35. \Magento\Cms\Model\Page $cmsPage,
  36. \Magento\Cms\Model\ResourceModel\Page\CollectionFactory $collectionFactory,
  37. \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface $pageLayoutBuilder,
  38. array $data = []
  39. ) {
  40. $this->_collectionFactory = $collectionFactory;
  41. $this->_cmsPage = $cmsPage;
  42. $this->pageLayoutBuilder = $pageLayoutBuilder;
  43. parent::__construct($context, $backendHelper, $data);
  44. }
  45. /**
  46. * @return void
  47. */
  48. protected function _construct()
  49. {
  50. parent::_construct();
  51. $this->setId('cmsPageGrid');
  52. $this->setDefaultSort('identifier');
  53. $this->setDefaultDir('ASC');
  54. }
  55. /**
  56. * Prepare collection
  57. *
  58. * @return \Magento\Backend\Block\Widget\Grid
  59. */
  60. protected function _prepareCollection()
  61. {
  62. $collection = $this->_collectionFactory->create();
  63. /* @var $collection \Magento\Cms\Model\ResourceModel\Page\Collection */
  64. $collection->setFirstStoreFlag(true);
  65. $this->setCollection($collection);
  66. return parent::_prepareCollection();
  67. }
  68. /**
  69. * Prepare columns
  70. *
  71. * @return \Magento\Backend\Block\Widget\Grid\Extended
  72. */
  73. protected function _prepareColumns()
  74. {
  75. $this->addColumn('title', ['header' => __('Title'), 'index' => 'title']);
  76. $this->addColumn('identifier', ['header' => __('URL Key'), 'index' => 'identifier']);
  77. $this->addColumn(
  78. 'page_layout',
  79. [
  80. 'header' => __('Layout'),
  81. 'index' => 'page_layout',
  82. 'type' => 'options',
  83. 'options' => $this->pageLayoutBuilder->getPageLayoutsConfig()->getOptions()
  84. ]
  85. );
  86. /**
  87. * Check is single store mode
  88. */
  89. if (!$this->_storeManager->isSingleStoreMode()) {
  90. $this->addColumn(
  91. 'store_id',
  92. [
  93. 'header' => __('Store View'),
  94. 'index' => 'store_id',
  95. 'type' => 'store',
  96. 'store_all' => true,
  97. 'store_view' => true,
  98. 'sortable' => false,
  99. 'filter_condition_callback' => [$this, '_filterStoreCondition']
  100. ]
  101. );
  102. }
  103. $this->addColumn(
  104. 'is_active',
  105. [
  106. 'header' => __('Status'),
  107. 'index' => 'is_active',
  108. 'type' => 'options',
  109. 'options' => $this->_cmsPage->getAvailableStatuses()
  110. ]
  111. );
  112. $this->addColumn(
  113. 'creation_time',
  114. [
  115. 'header' => __('Created'),
  116. 'index' => 'creation_time',
  117. 'type' => 'datetime',
  118. 'header_css_class' => 'col-date',
  119. 'column_css_class' => 'col-date'
  120. ]
  121. );
  122. $this->addColumn(
  123. 'update_time',
  124. [
  125. 'header' => __('Modified'),
  126. 'index' => 'update_time',
  127. 'type' => 'datetime',
  128. 'header_css_class' => 'col-date',
  129. 'column_css_class' => 'col-date'
  130. ]
  131. );
  132. $this->addColumn(
  133. 'page_actions',
  134. [
  135. 'header' => __('Action'),
  136. 'sortable' => false,
  137. 'filter' => false,
  138. 'renderer' => \Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action::class,
  139. 'header_css_class' => 'col-action',
  140. 'column_css_class' => 'col-action'
  141. ]
  142. );
  143. return parent::_prepareColumns();
  144. }
  145. /**
  146. * After load collection
  147. *
  148. * @return void
  149. */
  150. protected function _afterLoadCollection()
  151. {
  152. $this->getCollection()->walk('afterLoad');
  153. parent::_afterLoadCollection();
  154. }
  155. /**
  156. * Filter store condition
  157. *
  158. * @param \Magento\Framework\Data\Collection $collection
  159. * @param \Magento\Framework\DataObject $column
  160. * @return void
  161. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  162. */
  163. protected function _filterStoreCondition($collection, \Magento\Framework\DataObject $column)
  164. {
  165. if (!($value = $column->getFilter()->getValue())) {
  166. return;
  167. }
  168. $this->getCollection()->addStoreFilter($value);
  169. }
  170. /**
  171. * Row click url
  172. *
  173. * @param \Magento\Framework\DataObject $row
  174. * @return string
  175. */
  176. public function getRowUrl($row)
  177. {
  178. return $this->getUrl('*/*/edit', ['page_id' => $row->getId()]);
  179. }
  180. }