Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractFieldArray.php
Go to the documentation of this file.
1 <?php
8 
16 abstract class AbstractFieldArray extends \Magento\Config\Block\System\Config\Form\Field
17 {
23  protected $_columns = [];
24 
30  protected $_addAfter = true;
31 
37  protected $_addButtonLabel;
38 
44  private $_arrayRowsCache;
45 
51  protected $_isPreparedToRender = false;
52 
56  protected $_template = 'Magento_Config::system/config/form/field/array.phtml';
57 
63  protected function _construct()
64  {
65  if (!$this->_addButtonLabel) {
66  $this->_addButtonLabel = __('Add');
67  }
68  parent::_construct();
69  }
70 
78  public function addColumn($name, $params)
79  {
80  $this->_columns[$name] = [
81  'label' => $this->_getParam($params, 'label', 'Column'),
82  'size' => $this->_getParam($params, 'size', false),
83  'style' => $this->_getParam($params, 'style'),
84  'class' => $this->_getParam($params, 'class'),
85  'renderer' => false,
86  ];
87  if (!empty($params['renderer'])
88  && $params['renderer'] instanceof \Magento\Framework\View\Element\AbstractBlock
89  ) {
90  $this->_columns[$name]['renderer'] = $params['renderer'];
91  }
92  }
93 
102  protected function _getParam($params, $paramName, $defaultValue = null)
103  {
104  return empty($params[$paramName]) ? $defaultValue : $params[$paramName];
105  }
106 
113  protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
114  {
115  $this->setElement($element);
116  $html = $this->_toHtml();
117  $this->_arrayRowsCache = null;
118  // doh, the object is used as singleton!
119  return $html;
120  }
121 
129  protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
130  {
131  // override in descendants
132  }
133 
141  public function getArrayRows()
142  {
143  if (null !== $this->_arrayRowsCache) {
144  return $this->_arrayRowsCache;
145  }
146  $result = [];
148  $element = $this->getElement();
149  if ($element->getValue() && is_array($element->getValue())) {
150  foreach ($element->getValue() as $rowId => $row) {
151  $rowColumnValues = [];
152  foreach ($row as $key => $value) {
153  $row[$key] = $value;
154  $rowColumnValues[$this->_getCellInputElementId($rowId, $key)] = $row[$key];
155  }
156  $row['_id'] = $rowId;
157  $row['column_values'] = $rowColumnValues;
158  $result[$rowId] = new \Magento\Framework\DataObject($row);
159  $this->_prepareArrayRow($result[$rowId]);
160  }
161  }
162  $this->_arrayRowsCache = $result;
163  return $this->_arrayRowsCache;
164  }
165 
173  protected function _getCellInputElementId($rowId, $columnName)
174  {
175  return $rowId . '_' . $columnName;
176  }
177 
184  protected function _getCellInputElementName($columnName)
185  {
186  return $this->getElement()->getName() . '[<%- _id %>][' . $columnName . ']';
187  }
188 
196  public function renderCellTemplate($columnName)
197  {
198  if (empty($this->_columns[$columnName])) {
199  throw new \Exception('Wrong column name specified.');
200  }
201  $column = $this->_columns[$columnName];
202  $inputName = $this->_getCellInputElementName($columnName);
203 
204  if ($column['renderer']) {
205  return $column['renderer']->setInputName(
206  $inputName
207  )->setInputId(
208  $this->_getCellInputElementId('<%- _id %>', $columnName)
209  )->setColumnName(
210  $columnName
211  )->setColumn(
212  $column
213  )->toHtml();
214  }
215 
216  return '<input type="text" id="' . $this->_getCellInputElementId(
217  '<%- _id %>',
218  $columnName
219  ) .
220  '"' .
221  ' name="' .
222  $inputName .
223  '" value="<%- ' .
224  $columnName .
225  ' %>" ' .
226  ($column['size'] ? 'size="' .
227  $column['size'] .
228  '"' : '') .
229  ' class="' .
230  (isset($column['class'])
231  ? $column['class']
232  : 'input-text') . '"' . (isset($column['style']) ? ' style="' . $column['style'] . '"' : '') . '/>';
233  }
234 
240  protected function _prepareToRender()
241  {
242  // Override in descendants to add columns, change add button label etc
243  }
244 
251  protected function _toHtml()
252  {
253  if (!$this->_isPreparedToRender) {
254  $this->_prepareToRender();
255  $this->_isPreparedToRender = true;
256  }
257  if (empty($this->_columns)) {
258  throw new \Exception('At least one column must be defined.');
259  }
260  return parent::_toHtml();
261  }
262 
268  public function isAddAfter()
269  {
270  return $this->_addAfter;
271  }
272 
278  public function getColumns()
279  {
280  return $this->_columns;
281  }
282 
287  public function getAddButtonLabel()
288  {
289  return $this->_addButtonLabel;
290  }
291 }
_getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
if(!isset($_GET['name'])) $name
Definition: log.php:14
$element
Definition: element.phtml:12