1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © O2TI. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* @author Bruno Elisei <[email protected]> |
6
|
|
|
* See COPYING.txt for license details. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace O2TI\AdvancedFieldsCheckout\Block\Adminhtml\Form\Field; |
10
|
|
|
|
11
|
|
|
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray; |
12
|
|
|
use Magento\Framework\DataObject; |
13
|
|
|
use Magento\Framework\Exception\LocalizedException; |
14
|
|
|
use O2TI\AdvancedFieldsCheckout\Block\Adminhtml\Form\Field\Column\FieldColumn; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AddAutocomplete - Add Autocomplete to field. |
18
|
|
|
*/ |
19
|
|
|
class AddAutocomplete extends AbstractFieldArray |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var FieldColumn |
23
|
|
|
*/ |
24
|
|
|
private $fieldRenderer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prepare rendering the new field by adding all the needed columns. |
28
|
|
|
*/ |
29
|
|
|
protected function _prepareToRender() |
30
|
|
|
{ |
31
|
|
|
$this->addColumn('field', [ |
32
|
|
|
'label' => __('Field'), |
33
|
|
|
'renderer' => $this->getFieldRenderer(), |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$this->addColumn('autocomplete', [ |
37
|
|
|
'label' => __('Autocomplete'), |
38
|
|
|
'class' => 'required-entry', |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$this->_addAfter = false; |
42
|
|
|
$this->_addButtonLabel = __('Add'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Prepare existing row data object. |
47
|
|
|
* |
48
|
|
|
* @param DataObject $row |
49
|
|
|
* |
50
|
|
|
* @throws LocalizedException |
51
|
|
|
*/ |
52
|
|
|
protected function _prepareArrayRow(DataObject $row): void |
53
|
|
|
{ |
54
|
|
|
$options = []; |
55
|
|
|
|
56
|
|
|
$field = $row->getField(); |
57
|
|
|
if ($field !== null) { |
58
|
|
|
$options['option_'.$this->getFieldRenderer()->calcOptionHash($field)] = 'selected="selected"'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$row->setData('option_extra_attrs', $options); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create Block FieldColumn. |
66
|
|
|
* |
67
|
|
|
* @throws LocalizedException |
68
|
|
|
* |
69
|
|
|
* @return FieldColumn |
70
|
|
|
*/ |
71
|
|
|
private function getFieldRenderer() |
72
|
|
|
{ |
73
|
|
|
if (!$this->fieldRenderer) { |
74
|
|
|
$this->fieldRenderer = $this->getLayout()->createBlock( |
75
|
|
|
FieldColumn::class, |
76
|
|
|
'', |
77
|
|
|
['data' => ['is_render_to_js_template' => true]] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->fieldRenderer; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|