ToSort::_prepareToRender()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
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\FieldSortInCheckout\Block\Adminhtml\Form\Field\Address;
10
11
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
12
use Magento\Framework\DataObject;
13
use Magento\Framework\Exception\LocalizedException;
14
15
/**
16
 * Class Address Fields To Sort - Set Field to Sort.
17
 */
18
class ToSort extends AbstractFieldArray
19
{
20
    /**
21
     * @var FieldColumn
22
     */
23
    private $fieldRenderer;
24
25
    /**
26
     * Prepare rendering the new field by adding all the needed columns.
27
     */
28
    protected function _prepareToRender()
29
    {
30
        $this->addColumn('field', [
31
            'label'    => __('Field'),
32
            'renderer' => $this->getFieldRenderer(),
33
        ]);
34
        $this->addColumn('sort_order', [
35
            'label' => __('Sort Order'),
36
            'class' => 'required-entry validate-digits validate-digits-range digits-range-1-1000',
37
        ]);
38
        $this->_addAfter = false;
39
        $this->_addButtonLabel = __('Add');
40
    }
41
42
    /**
43
     * Prepare existing row data object.
44
     *
45
     * @param DataObject $row
46
     *
47
     * @throws LocalizedException
48
     */
49
    protected function _prepareArrayRow(DataObject $row): void
50
    {
51
        $options = [];
52
53
        $field = $row->getField();
54
        if ($field !== null) {
55
            $options['option_'.$this->getFieldRenderer()->calcOptionHash($field)] = 'selected="selected"';
56
        }
57
58
        $row->setData('option_extra_attrs', $options);
59
    }
60
61
    /**
62
     * Render Options.
63
     *
64
     * @throws LocalizedException
65
     *
66
     * @return FieldColumn
67
     */
68
    private function getFieldRenderer()
69
    {
70
        if (!$this->fieldRenderer) {
71
            $this->fieldRenderer = $this->getLayout()->createBlock(
72
                FieldColumn::class,
73
                '',
74
                ['data' => ['is_render_to_js_template' => true]]
75
            );
76
        }
77
78
        return $this->fieldRenderer;
79
    }
80
}
81