Created
November 9, 2022 07:40
-
-
Save roman204/7219788ec16ae4b503bc9e3197fc3213 to your computer and use it in GitHub Desktop.
overwrite cms block chooser to get storeView into grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CopeX\LandingPageIdentifier\Block\Adminhtml\Page\Widget; | |
use Magento\Backend\Block\Template\Context; | |
use Magento\Backend\Helper\Data; | |
use Magento\Cms\Model\Page; | |
use Magento\Cms\Model\PageFactory; | |
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory; | |
use Magento\Framework\View\Model\PageLayout\Config\BuilderInterface; | |
class Chooser extends \Magento\Backend\Block\Widget\Grid\Extended | |
{ | |
protected function _construct() | |
{ | |
parent::_construct(); | |
//$this->setDefaultSort('name'); | |
$this->setUseAjax(true); | |
$this->setDefaultFilter(['chooser_is_active' => '1']); | |
} | |
/** | |
* Grid Row JS Callback | |
* | |
* @return string | |
*/ | |
public function getRowClickCallback() | |
{ | |
$chooserJsObject = $this->getId(); | |
$js = ' | |
function (grid, event) { | |
var trElement = Event.findElement(event, "tr"); | |
var pageTitle = trElement.down("td").next().innerHTML; | |
var pageId = trElement.down("td").innerHTML.replace(/^\s+|\s+$/g,""); | |
' . | |
$chooserJsObject . | |
'.setElementValue(pageId); | |
' . | |
$chooserJsObject . | |
'.setElementLabel(pageTitle); | |
' . | |
$chooserJsObject . | |
'.close(); | |
} | |
'; | |
return $js; | |
} | |
/** | |
* @param Context $context | |
* @param Data $backendHelper | |
* @param Page $cmsPage | |
* @param PageFactory $pageFactory | |
* @param CollectionFactory $collectionFactory | |
* @param BuilderInterface $pageLayoutBuilder | |
* @param array $data | |
*/ | |
public function __construct( | |
\Magento\Backend\Block\Template\Context $context, | |
\Magento\Backend\Helper\Data $backendHelper, | |
\Magento\Cms\Model\Page $cmsPage, | |
\Magento\Cms\Model\PageFactory $pageFactory, | |
\Magento\Cms\Model\ResourceModel\Page\CollectionFactory $collectionFactory, | |
\Magento\Framework\View\Model\PageLayout\Config\BuilderInterface $pageLayoutBuilder, | |
array $data = [] | |
) { | |
$this->pageLayoutBuilder = $pageLayoutBuilder; | |
$this->_cmsPage = $cmsPage; | |
$this->_pageFactory = $pageFactory; | |
$this->_collectionFactory = $collectionFactory; | |
parent::__construct($context, $backendHelper, $data); | |
} | |
public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) | |
{ | |
$uniqId = $this->mathRandom->getUniqueHash($element->getId()); | |
$sourceUrl = $this->getUrl('cms/page_widget/chooser', ['uniq_id' => $uniqId]); | |
$chooser = $this->getLayout()->createBlock( | |
\Magento\Widget\Block\Adminhtml\Widget\Chooser::class | |
)->setElement( | |
$element | |
)->setConfig( | |
$this->getConfig() | |
)->setFieldsetId( | |
$this->getFieldsetId() | |
)->setSourceUrl( | |
$sourceUrl | |
)->setUniqId( | |
$uniqId | |
); | |
if ($element->getValue()) { | |
$page = $this->_pageFactory->create()->load((int)$element->getValue()); | |
if ($page->getId()) { | |
$chooser->setLabel($this->escapeHtml($page->getTitle())); | |
} | |
} | |
$element->setData('after_element_html', $chooser->toHtml()); | |
return $element; | |
} | |
protected function _prepareColumns() | |
{ | |
$this->addColumn( | |
'chooser_id', | |
[ | |
'header' => __('ID'), | |
'index' => 'page_id', | |
'header_css_class' => 'col-id', | |
'column_css_class' => 'col-id' | |
] | |
); | |
$this->addColumn( | |
'chooser_title', | |
[ | |
'header' => __('Title'), | |
'index' => 'title', | |
'header_css_class' => 'col-title', | |
'column_css_class' => 'col-title' | |
] | |
); | |
$this->addColumn( | |
'chooser_identifier', | |
[ | |
'header' => __('URL Key'), | |
'index' => 'identifier', | |
'header_css_class' => 'col-url', | |
'column_css_class' => 'col-url' | |
] | |
); | |
$this->addColumn( | |
'chooser_page_layout', | |
[ | |
'header' => __('Layout'), | |
'index' => 'page_layout', | |
'type' => 'options', | |
'options' => $this->pageLayoutBuilder->getPageLayoutsConfig()->getOptions(), | |
'header_css_class' => 'col-layout', | |
'column_css_class' => 'col-layout' | |
] | |
); | |
$this->addColumn( | |
'chooser_is_active', | |
[ | |
'header' => __('Status'), | |
'index' => 'is_active', | |
'type' => 'options', | |
'options' => $this->_cmsPage->getAvailableStatuses(), | |
'header_css_class' => 'col-status', | |
'column_css_class' => 'col-status' | |
] | |
); | |
$this->addColumn( | |
'store_id', | |
[ | |
'header' => __('Store View'), | |
'index' => 'store_id', | |
'type' => 'store', | |
'store_all' => true, | |
'store_view' => true, | |
'sortable' => false, | |
'filter_condition_callback' => [$this, '_filterStoreCondition'] | |
] | |
); | |
return parent::_prepareColumns(); | |
} | |
protected function _prepareCollection() | |
{ | |
$collection = $this->_collectionFactory->create(); | |
/* @var $collection \Magento\Cms\Model\ResourceModel\Page\CollectionFactory */ | |
$collection->setFirstStoreFlag(true); | |
$this->setCollection($collection); | |
return parent::_prepareCollection(); | |
} | |
protected function _filterStoreCondition($collection, \Magento\Framework\DataObject $column) | |
{ | |
if (!($value = $column->getFilter()->getValue())) { | |
return; | |
} | |
$this->getCollection()->addStoreFilter($value); | |
} | |
/** | |
* Get grid url | |
* | |
* @return string | |
*/ | |
public function getGridUrl() | |
{ | |
return $this->getUrl('cms/page_widget/chooser', ['_current' => true]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment