Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Chooser.php
Go to the documentation of this file.
1 <?php
7 
11 class Chooser extends \Magento\Backend\Block\Widget\Grid\Extended
12 {
16  protected $_blockFactory;
17 
22 
30  public function __construct(
31  \Magento\Backend\Block\Template\Context $context,
32  \Magento\Backend\Helper\Data $backendHelper,
33  \Magento\Cms\Model\BlockFactory $blockFactory,
34  \Magento\Cms\Model\ResourceModel\Block\CollectionFactory $collectionFactory,
35  array $data = []
36  ) {
37  $this->_blockFactory = $blockFactory;
38  $this->_collectionFactory = $collectionFactory;
39  parent::__construct($context, $backendHelper, $data);
40  }
41 
47  protected function _construct()
48  {
49  parent::_construct();
50  $this->setDefaultSort('block_identifier');
51  $this->setDefaultDir('ASC');
52  $this->setUseAjax(true);
53  $this->setDefaultFilter(['chooser_is_active' => '1']);
54  }
55 
62  public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
63  {
64  $uniqId = $this->mathRandom->getUniqueHash($element->getId());
65  $sourceUrl = $this->getUrl('cms/block_widget/chooser', ['uniq_id' => $uniqId]);
66 
67  $chooser = $this->getLayout()->createBlock(
68  \Magento\Widget\Block\Adminhtml\Widget\Chooser::class
69  )->setElement(
70  $element
71  )->setConfig(
72  $this->getConfig()
73  )->setFieldsetId(
74  $this->getFieldsetId()
75  )->setSourceUrl(
76  $sourceUrl
77  )->setUniqId(
78  $uniqId
79  );
80 
81  if ($element->getValue()) {
82  $block = $this->_blockFactory->create()->load($element->getValue());
83  if ($block->getId()) {
84  $chooser->setLabel($this->escapeHtml($block->getTitle()));
85  }
86  }
87 
88  $element->setData('after_element_html', $chooser->toHtml());
89  return $element;
90  }
91 
97  public function getRowClickCallback()
98  {
99  $chooserJsObject = $this->getId();
100  $js = '
101  function (grid, event) {
102  var trElement = Event.findElement(event, "tr");
103  var blockId = trElement.down("td").innerHTML.replace(/^\s+|\s+$/g,"");
104  var blockTitle = trElement.down("td").next().innerHTML;
105  ' .
106  $chooserJsObject .
107  '.setElementValue(blockId);
108  ' .
109  $chooserJsObject .
110  '.setElementLabel(blockTitle);
111  ' .
112  $chooserJsObject .
113  '.close();
114  }
115  ';
116  return $js;
117  }
118 
124  protected function _prepareCollection()
125  {
126  $this->setCollection($this->_collectionFactory->create());
127  return parent::_prepareCollection();
128  }
129 
135  protected function _prepareColumns()
136  {
137  $this->addColumn(
138  'chooser_id',
139  ['header' => __('ID'), 'align' => 'right', 'index' => 'block_id', 'width' => 50]
140  );
141 
142  $this->addColumn('chooser_title', ['header' => __('Title'), 'align' => 'left', 'index' => 'title']);
143 
144  $this->addColumn(
145  'chooser_identifier',
146  ['header' => __('Identifier'), 'align' => 'left', 'index' => 'identifier']
147  );
148 
149  $this->addColumn(
150  'chooser_is_active',
151  [
152  'header' => __('Status'),
153  'index' => 'is_active',
154  'type' => 'options',
155  'options' => [0 => __('Disabled'), 1 => __('Enabled')]
156  ]
157  );
158 
159  return parent::_prepareColumns();
160  }
161 
167  public function getGridUrl()
168  {
169  return $this->getUrl('cms/block_widget/chooser', ['_current' => true]);
170  }
171 }
__()
Definition: __.php:13
$block
Definition: block.php:8
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, \Magento\Cms\Model\BlockFactory $blockFactory, \Magento\Cms\Model\ResourceModel\Block\CollectionFactory $collectionFactory, array $data=[])
Definition: Chooser.php:30
prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
Definition: Chooser.php:62
$element
Definition: element.phtml:12