diff --git a/Block/Adminhtml/Form/Field/Address.php b/Block/Adminhtml/Form/Field/Address.php new file mode 100644 index 0000000..6b184d3 --- /dev/null +++ b/Block/Adminhtml/Form/Field/Address.php @@ -0,0 +1,87 @@ +scopeConfig = $context->getScopeConfig(); + parent::__construct($context,$data); + } + /** + * Set "name" for element + * + * @param $value + * @return $this + */ + public function setInputId($value) + { + return $this->setId($value); + } + + + /** + * Render block HTML + * + * @return string + */ + public function _toHtml(): string + { + if (!$this->getOptions()) { + $this->setOptions($this->getSourceOptions()); + } + return parent::_toHtml(); + } + + private function getSourceOptions(): array + { + $extrafields = false; + if($this->scopeConfig->getValue('postcodenl_api/general/address_fields_extra')) $extrafields = explode("\n", $this->scopeConfig->getValue('postcodenl_api/general/address_fields_extra')); + $defaultFields = [ + 'firstname', + 'lastname', + 'postcode_nl', + 'street', + 'postcode', + 'city', + 'country_id', + 'region_id', + 'telephone', + 'company', + 'vat_id' + ]; + $ar = []; + foreach($defaultFields as $field){ + $ar[] = ['label' => str_replace('_', ' ', str_replace('_id', '', ucfirst($field))), 'value' => $field]; + } + if($extrafields){ + foreach($extrafields as $field){ + $ar[] = ['label' => str_replace('_', ' ', str_replace('_id', '', ucfirst($field))), 'value' => $field]; + } + } + return $ar; + } +} + diff --git a/Block/Adminhtml/Form/Field/AddressColumns.php b/Block/Adminhtml/Form/Field/AddressColumns.php new file mode 100644 index 0000000..3d5ba3c --- /dev/null +++ b/Block/Adminhtml/Form/Field/AddressColumns.php @@ -0,0 +1,83 @@ +addColumn('addressfield', ['label' => __('Address Field'), 'renderer' => $this->getAddresFieldRenderer()]); + $this->addColumn('position', ['label' => __('Position'), 'renderer' => $this->getPositionRenderer(), 'class' => 'required-entry']); + + $this->_addAfter = false; + $this->_addButtonLabel = __('Add'); + } + + /** + * Prepare existing row data object + * + * @param DataObject $row + * @throws LocalizedException + */ + protected function _prepareArrayRow(DataObject $row): void + { + $options = []; + + /*$product = $row->getProduct(); + if ($product !== null) { + $options['option_' . $this->productRenderer()->calcOptionHash($product)] = 'selected="selected"'; + }*/ + + $address = $row->getAddress(); + if ($address !== null) { + $options['option_' . $this->getAddresFieldRenderer()->calcOptionHash($address)] = 'selected="selected"'; + } + + $position = $row->getPosition(); + if ($position !== null) { + $options['option_' . $this->getPositionRenderer()->calcOptionHash($position)] = 'selected="selected"'; + } + + $row->setData('option_extra_attrs', $options); + } + + + private function getAddresFieldRenderer(){ + if (!$this->addresFieldRenderer) { + $this->addresFieldRenderer = $this->getLayout()->createBlock( + \Flekto\Postcode\Block\Adminhtml\Form\Field\Address::class, + '', + ['data' => ['is_render_to_js_template' => true]] + ); + } + return $this->addresFieldRenderer; + } + + private function getPositionRenderer(){ + if (!$this->positionRenderer) { + $this->positionRenderer = $this->getLayout()->createBlock( + \Flekto\Postcode\Block\Adminhtml\Form\Field\Position::class, + '', + ['data' => ['is_render_to_js_template' => true]] + ); + } + return $this->positionRenderer; + } +} + diff --git a/Block/Adminhtml/Form/Field/Position.php b/Block/Adminhtml/Form/Field/Position.php new file mode 100644 index 0000000..86f3991 --- /dev/null +++ b/Block/Adminhtml/Form/Field/Position.php @@ -0,0 +1,68 @@ + element + * + * @param string $value + * @return $this + */ + public function setInputName($value) + { + return $this->setName($value); + } + + /** + * Set "id" for