Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Container.php
Go to the documentation of this file.
1 <?php
7 
17 class Container extends \Magento\Backend\Block\Widget\Container
18 {
22  const PARAM_BLOCK_GROUP = 'block_group';
23 
24  const PARAM_BUTTON_NEW = 'button_new';
25 
26  const PARAM_BUTTON_BACK = 'button_back';
27 
31  protected $_addButtonLabel;
32 
36  protected $_backButtonLabel;
37 
41  protected $_blockGroup = 'Magento_Backend';
42 
46  protected $_template = 'Magento_Backend::widget/grid/container.phtml';
47 
53  protected function _construct()
54  {
55  parent::_construct();
56  if ($this->hasData(self::PARAM_BLOCK_GROUP)) {
57  $this->_blockGroup = $this->_getData(self::PARAM_BLOCK_GROUP);
58  }
59  if ($this->hasData(self::PARAM_BUTTON_NEW)) {
60  $this->_addButtonLabel = $this->_getData(self::PARAM_BUTTON_NEW);
61  } else {
62  // legacy logic to support all descendants
63  if ($this->_addButtonLabel === null) {
64  $this->_addButtonLabel = __('Add New');
65  }
66  $this->_addNewButton();
67  }
68  if ($this->hasData(self::PARAM_BUTTON_BACK)) {
69  $this->_backButtonLabel = $this->_getData(self::PARAM_BUTTON_BACK);
70  } else {
71  // legacy logic
72  if ($this->_backButtonLabel === null) {
73  $this->_backButtonLabel = __('Back');
74  }
75  }
76  }
77 
81  protected function _prepareLayout()
82  {
83  // check if grid was created through the layout
84  if (false === $this->getChildBlock('grid')) {
85  $this->setChild(
86  'grid',
87  $this->getLayout()->createBlock(
88  str_replace(
89  '_',
90  '\\',
91  $this->_blockGroup
92  ) . '\\Block\\' . str_replace(
93  ' ',
94  '\\',
95  ucwords(str_replace('_', ' ', $this->_controller))
96  ) . '\\Grid',
97  $this->_controller . '.grid'
99  true
100  )
101  );
102  }
103  return parent::_prepareLayout();
104  }
105 
109  public function getCreateUrl()
110  {
111  return $this->getUrl('*/*/new');
112  }
113 
117  public function getGridHtml()
118  {
119  return $this->getChildHtml('grid');
120  }
121 
125  public function getAddButtonLabel()
126  {
127  return $this->_addButtonLabel;
128  }
129 
133  public function getBackButtonLabel()
134  {
136  }
137 
143  protected function _addNewButton()
144  {
145  $this->addButton(
146  'add',
147  [
148  'label' => $this->getAddButtonLabel(),
149  'onclick' => 'setLocation(\'' . $this->getCreateUrl() . '\')',
150  'class' => 'add primary'
151  ]
152  );
153  }
154 
158  protected function _addBackButton()
159  {
160  $this->addButton(
161  'back',
162  [
163  'label' => $this->getBackButtonLabel(),
164  'onclick' => 'setLocation(\'' . $this->getBackUrl() . '\')',
165  'class' => 'back'
166  ]
167  );
168  }
169 
173  public function getHeaderCssClass()
174  {
175  return 'icon-head ' . parent::getHeaderCssClass();
176  }
177 
181  public function getHeaderWidth()
182  {
183  return 'width:50%;';
184  }
185 }
__()
Definition: __.php:13
addButton($buttonId, $data, $level=0, $sortOrder=0, $region='toolbar')
Definition: Container.php:85