|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Flekto\Postcode\Data\Form\Element; |
| 4 | + |
| 5 | +use Magento\Framework\Data\Form\Element\AbstractElement; |
| 6 | +use Magento\Framework\Data\Form\Element\CollectionFactory; |
| 7 | +use Magento\Framework\Data\Form\Element\Factory; |
| 8 | +use Magento\Framework\Escaper; |
| 9 | +use Magento\Framework\View\LayoutInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Add custom element for address autocomplete controls |
| 13 | + */ |
| 14 | +class AddressAutofill extends AbstractElement |
| 15 | +{ |
| 16 | + /** @var LayoutInterface */ |
| 17 | + protected $_layout; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param Factory $factoryElement |
| 21 | + * @param CollectionFactory $factoryCollection |
| 22 | + * @param Escaper $escaper |
| 23 | + * @param LayoutInterface $layout |
| 24 | + * @param array $data |
| 25 | + */ |
| 26 | + public function __construct( |
| 27 | + Factory $factoryElement, |
| 28 | + CollectionFactory $factoryCollection, |
| 29 | + Escaper $escaper, |
| 30 | + LayoutInterface $layout, |
| 31 | + $data = [] |
| 32 | + ) { |
| 33 | + parent::__construct($factoryElement, $factoryCollection, $escaper, $data); |
| 34 | + $this->setType('postcode-eu-address-autofill'); |
| 35 | + $this->_layout = $layout; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Get element HTML |
| 40 | + * |
| 41 | + * @return string |
| 42 | + */ |
| 43 | + public function getElementHtml(): string |
| 44 | + { |
| 45 | + $block = $this->_layout->createBlock( |
| 46 | + \Magento\Backend\Block\Template::class, |
| 47 | + $this->getId(), |
| 48 | + [ |
| 49 | + 'data' => [ |
| 50 | + 'template' => 'Flekto_Postcode::form/element/address-autofill.phtml', |
| 51 | + 'jsLayout' => $this->getJsLayout(), |
| 52 | + ], |
| 53 | + ], |
| 54 | + ); |
| 55 | + |
| 56 | + return $block->toHtml(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Get JS layout |
| 61 | + * |
| 62 | + * @return array |
| 63 | + */ |
| 64 | + public function getJsLayout(): array |
| 65 | + { |
| 66 | + return [ |
| 67 | + 'components' => [ |
| 68 | + $this->getId() => [ |
| 69 | + 'component' => 'Flekto_Postcode/js/view/address/autofill', |
| 70 | + 'config' => [ |
| 71 | + 'settings' => $this->getData('settings'), |
| 72 | + 'htmlIdPrefix' => $this->getData('htmlIdPrefix'), |
| 73 | + 'addressType' => $this->getData('addressType'), |
| 74 | + 'countryCode' => $this->getData('countryCode'), |
| 75 | + 'visible' => $this->getData('visible'), |
| 76 | + ], |
| 77 | + 'children' => [ |
| 78 | + 'address_autofill_nl' => [ |
| 79 | + 'component' => 'Flekto_Postcode/js/view/form/sales/order_create/address-autofill-nl', |
| 80 | + 'config' => [ |
| 81 | + 'componentDisabled' => $this->getData('isNlComponentDisabled'), |
| 82 | + ], |
| 83 | + 'children' => [ |
| 84 | + 'postcode' => [ |
| 85 | + 'component' => 'Magento_Ui/js/form/element/abstract', |
| 86 | + 'label' => __('Zip/Postal Code'), |
| 87 | + 'config' => [ |
| 88 | + 'template' => 'ui/form/field', |
| 89 | + 'elementTmpl' => 'Flekto_Postcode/form/element/address-autofill-field', |
| 90 | + 'placeholder' => '1234 AB', |
| 91 | + 'imports' => [ |
| 92 | + 'visible' => '${ $.parentName }:visible', |
| 93 | + ], |
| 94 | + ], |
| 95 | + ], |
| 96 | + 'house_number' => [ |
| 97 | + 'component' => 'Magento_Ui/js/form/element/abstract', |
| 98 | + 'label' => __('House number and addition'), |
| 99 | + 'additionalClasses' => [ |
| 100 | + 'address-autofill-nl-house-number' => true, |
| 101 | + ], |
| 102 | + 'config' => [ |
| 103 | + 'template' => 'ui/form/field', |
| 104 | + 'elementTmpl' => 'Flekto_Postcode/form/element/address-autofill-field', |
| 105 | + 'imports' => [ |
| 106 | + 'visible' => '${ $.parentName }:visible', |
| 107 | + ], |
| 108 | + ], |
| 109 | + ], |
| 110 | + 'house_number_select' => [ |
| 111 | + 'component' => 'Magento_Ui/js/form/element/select', |
| 112 | + 'label' => __('Which house number do you mean?'), |
| 113 | + 'config' => [ |
| 114 | + 'caption' => __('- Select house number -'), |
| 115 | + 'template' => 'ui/form/field', |
| 116 | + 'visible' => false, |
| 117 | + ], |
| 118 | + ], |
| 119 | + ], |
| 120 | + ], |
| 121 | + 'address_autofill_intl' => [ |
| 122 | + 'component' => 'Flekto_Postcode/js/view/form/sales/order_create/address-autofill-intl', |
| 123 | + 'label' => __('Find an address'), |
| 124 | + 'placeholder' => __('City, street or postcode') |
| 125 | + ], |
| 126 | + 'address_autofill_error' => [ |
| 127 | + 'component' => 'Magento_Ui/js/form/components/html', |
| 128 | + 'config' => [ |
| 129 | + 'visible' => false, |
| 130 | + 'listens' => [ |
| 131 | + '${$.parentName}:error' => 'content', |
| 132 | + 'content' => 'visible', |
| 133 | + ], |
| 134 | + 'additionalClasses' => [ |
| 135 | + 'admin__field-note' => true, |
| 136 | + 'address-autofill-warning' => true, |
| 137 | + ], |
| 138 | + ], |
| 139 | + ], |
| 140 | + ], |
| 141 | + ], |
| 142 | + ], |
| 143 | + ]; |
| 144 | + } |
| 145 | +} |
0 commit comments