Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Block/Adminhtml/Form/Field/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Flekto\Postcode\Block\Adminhtml\Form\Field;

use Magento\Framework\View\Element\Html\Select;

class Address extends Select
{
private $scopeConfig;
public function __construct(
\Magento\Framework\View\Element\Context $context,
array $data = []
) {
$this->scopeConfig = $context->getScopeConfig();
parent::__construct($context,$data);
}
/**
* Set "name" for <select> element
*
* @param string $value
* @return $this
*/
public function setInputName($value)
{
return $this->setName($value);
}

/**
* Set "id" for <select> 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;
}
}

83 changes: 83 additions & 0 deletions Block/Adminhtml/Form/Field/AddressColumns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Flekto\Postcode\Block\Adminhtml\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;


class AddressColumns extends AbstractFieldArray
{
private $addresFieldRenderer;
private $positionRenderer;

/**
* Prepare rendering the new field by adding all the needed columns
*/
protected function _prepareToRender()
{
$this->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;
}
}

68 changes: 68 additions & 0 deletions Block/Adminhtml/Form/Field/Position.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Flekto\Postcode\Block\Adminhtml\Form\Field;

class Position extends \Magento\Framework\View\Element\Html\Select
{


public function __construct(
\Magento\Framework\View\Element\Context $context,
array $data = []
) {
parent::__construct($context,$data);
}
/**
* Set "name" for <select> element
*
* @param string $value
* @return $this
*/
public function setInputName($value)
{
return $this->setName($value);
}

/**
* Set "id" for <select> 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
{
$ar = [];
$i=1;
while($i<=20){
$ar[] = [
'label' => $i*10,
'value' => $i*10
];
$i++;
}
return $ar;
}
}
35 changes: 19 additions & 16 deletions Block/Onepage/LayoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Exception\LocalizedException;
use Flekto\Postcode\Helper\StoreConfigHelper;
use Magento\Framework\Serialize\SerializerInterface;

class LayoutProcessor extends AbstractBlock implements LayoutProcessorInterface
{
Expand All @@ -16,6 +17,7 @@ class LayoutProcessor extends AbstractBlock implements LayoutProcessorInterface
* @var array
*/
protected $jsLayout;
protected $serializer;

/**
* @var StoreConfigHelper
Expand All @@ -31,10 +33,11 @@ class LayoutProcessor extends AbstractBlock implements LayoutProcessorInterface
* @param array $data (default: [])
* @return void
*/
public function __construct(Context $context, StoreConfigHelper $storeConfigHelper, array $data = [])
public function __construct(Context $context, StoreConfigHelper $storeConfigHelper, SerializerInterface $serializer, array $data = [])
{
$this->storeConfigHelper = $storeConfigHelper;

$this->scopeConfig = $context->getScopeConfig();
$this->serializer = $serializer;
parent::__construct($context, $data);
}

Expand Down Expand Up @@ -236,26 +239,26 @@ private function _changeAddressFieldsPosition($addressFields): array
if (!$this->storeConfigHelper->isSetFlag('change_fields_position')) {
return $addressFields;
}
$fieldsPositions = $this->serializer->unserialize($this->scopeConfig->getValue('postcodenl_api/general/address_fields_positions'));

$fieldToSortOrder = [
'country_id' => '900',
'address_autofill_intl' => '910',
'address_autofill_nl' => '920',
'address_autofill_formatted_output' => '930',
'address_autofill_bypass' => '935',
'street' => '940',
'postcode' => '950',
'city' => '960',
'region' => '970',
'region_id' => '975',
$flektoFields = [
'address_autofill_intl',
'address_autofill_nl',
'address_autofill_formatted_output',
'address_autofill_bypass'
];

foreach ($fieldToSortOrder as $name => $sortOrder) {
if (isset($addressFields[$name])) {
$addressFields[$name]['sortOrder'] = $sortOrder;
foreach($fieldsPositions as $data){
$name = $data['addressfield'];
if($name == 'postcode_nl'){
foreach($flektoFields as $i => $field){
if(isset($addressFields[$field])) $addressFields[$field]['sortOrder'] = $data['position']+$i;
}
}
if(isset($addressFields[$name])) $addressFields[$name]['sortOrder'] = $data['position'];
}


return $addressFields;
}
}
16 changes: 16 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@
<comment/>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="address_fields_extra" type="textarea" sortOrder="90" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Address extra fields</label>
<comment>The fields must be saved in order to be used for sorting bellow. Use one field per line</comment>
<depends>
<field id="change_fields_position">1</field> <!-- Show this field if 'change_fields_position' is set to "Yes" -->
</depends>
</field>
<field id="address_fields_positions" type="select" sortOrder="100" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Address fields positions</label>
<comment/>
<frontend_model>Flekto\Postcode\Block\Adminhtml\Form\Field\AddressColumns</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<depends>
<field id="change_fields_position">1</field> <!-- Show this field if 'change_fields_position' is set to "Yes" -->
</depends>
</field>
</group>

<group id="advanced_config" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label">
Expand Down