Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the way to change form fields based on country #3

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions src/Api/Data/CountryFormModifierInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Elgentos\ZipcodeChecker\Api\Data;

use Elgentos\ZipcodeChecker\Enum\FormModeEnum;

interface CountryFormModifierInterface
{
public function isDefault(): bool;

public function getCountryCodes(): array;
public function getMode(): FormModeEnum;

public function build(FieldListModifierInterface $fieldListModifier): void;
}
9 changes: 0 additions & 9 deletions src/Enum/FormModeEnum.php

This file was deleted.

10 changes: 7 additions & 3 deletions src/Model/CountryFormModifier/DefaultCountryModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Elgentos\ZipcodeChecker\Model\CountryFormModifier;

use Elgentos\ZipcodeChecker\Api\Data\CountryFormModifierInterface;
use Elgentos\ZipcodeChecker\Enum\FormModeEnum;
use Elgentos\ZipcodeChecker\Api\Data\FieldListModifierInterface;

class DefaultCountryModifier implements CountryFormModifierInterface
{
Expand All @@ -17,8 +17,12 @@ public function getCountryCodes(): array
return [];
}

public function getMode(): FormModeEnum
public function build(FieldListModifierInterface $fieldListModifier): void
{
return FormModeEnum::SearchBasedOnInput;
$form = $fieldListModifier->getForm();

$form->getField('street')->hide();
$form->getField('postcode')->hide();
$form->getField('city')->hide();
}
}
18 changes: 15 additions & 3 deletions src/Model/CountryFormModifier/NLCountryModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Elgentos\ZipcodeChecker\Model\CountryFormModifier;

use Elgentos\ZipcodeChecker\Api\Data\CountryFormModifierInterface;
use Elgentos\ZipcodeChecker\Enum\FormModeEnum;
use Elgentos\ZipcodeChecker\Api\Data\FieldListModifierInterface;

class NLCountryModifier implements CountryFormModifierInterface
{
Expand All @@ -17,8 +17,20 @@ public function getCountryCodes(): array
return ['nl'];
}

public function getMode(): FormModeEnum
public function build(FieldListModifierInterface $fieldListModifier): void
{
return FormModeEnum::SearchBasedOnZipcodeHouseNumber;
$form = $fieldListModifier->getForm();

$form->removeField($form->getField('search'));

//$streetRelatives = $form->getField('street')->getRelatives();
//$form->getField('street')->hide();
//foreach ($streetRelatives as $relative) {
// $relative->show();
//}

//$form->getField('street')->show();
$form->getField('postcode')->show();
$form->getField('city')->hide();
}
}
67 changes: 40 additions & 27 deletions src/Model/HyvaCheckout/FieldListModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
namespace Elgentos\ZipcodeChecker\Model\HyvaCheckout;

use Elgentos\ZipcodeChecker\Api\Data\CountryFormModifierInterface;
use Elgentos\ZipcodeChecker\Enum\FormModeEnum;
use Elgentos\ZipcodeChecker\Api\Data\FieldListModifierInterface;
use Hyva\Checkout\Model\Form\EntityFormInterface;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

class FieldListModifier
class FieldListModifier implements FieldListModifierInterface
{
/** @var CountryFormModifierInterface[] $countryFormModifiers */
protected array $countryFormModifiers = [];
Expand All @@ -22,12 +25,17 @@ class FieldListModifier

protected CountryFormModifierInterface $countryFormModifier;

public function __construct(
private readonly Session $session,
) {
}

public function init (
EntityFormInterface $form,
array $countryFormModifiers = []
): void {
$this->form = $form;
$this->countryFormModifiers = $countryFormModifiers;
$this->form = $form;

$this->form->addField(
$this->form->createField(
Expand All @@ -49,30 +57,12 @@ public function boot(): void

public function build(): void
{
$this->countryFormModifier = $this->getCountryFormModifier();

if ($this->countryFormModifier->getMode() === FormModeEnum::SearchBasedOnInput) {
$this->buildSearchInputForm();
return;
}

$this->buildSearchZipcodeForm();
}

public function buildSearchInputForm(): void
{
$this->form->getField('street')->hide();
$this->form->getField('postcode')->hide();
$this->form->getField('city')->hide();
$this->dispatchCountryModifier('build');
}

public function buildSearchZipcodeForm(): void
public function getForm(): EntityFormInterface
{
$this->form->removeField($this->form->getField('search'));

$this->form->getField('street')->show();
$this->form->getField('postcode')->hide();
$this->form->getField('city')->hide();
return $this->form;
}

public function getCountryFormModifier(): ?CountryFormModifierInterface
Expand Down Expand Up @@ -101,10 +91,33 @@ function (array $carry, CountryFormModifierInterface $formModifier) {
return $result['specific'] ?? $result['default'];
}

/**
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function getCountryCode(): string
{
return strtolower(
$this->form->getField('country_id')->getValue()
);
$countryCode = $this->form->getField('country_id')->getValue();

if (! $countryCode) {
$countryCode = $this->session->getQuote()->getShippingAddress()->getCountryId();
}

return strtolower($countryCode);
}

protected function dispatchCountryModifier(string $method): void
{
$countryModifier = $this->getCountryFormModifier();

if (! $countryModifier) {
return;
}

if (! method_exists($countryModifier, $method)) {
return;
}

$countryModifier->{$method}($this);
}
}
7 changes: 2 additions & 5 deletions src/Model/HyvaCheckout/FormModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@

namespace Elgentos\ZipcodeChecker\Model\HyvaCheckout;

use Elgentos\ZipcodeChecker\Api\Data\CountryFormModifierInterface;
use Elgentos\ZipcodeChecker\Api\Data\FieldListModifierInterface;
use Hyva\Checkout\Model\Form\EntityFormInterface;
use Hyva\Checkout\Model\Form\EntityFormModifierInterface;

class FormModifier implements EntityFormModifierInterface
{
/**
* @param CountryFormModifierInterface[] $countryFormModifiers
*/
public function __construct(
public FieldListModifier $fieldListModifier,
public FieldListModifierInterface $fieldListModifier,
public array $countryFormModifiers = []
){
}
Expand Down
4 changes: 4 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Elgentos\ZipcodeChecker\Api\Data\AttributeArrayModifierInterface"
type="Elgentos\ZipcodeChecker\Model\Attributes\AttributeArrayModifier"/>

<preference for="Elgentos\ZipcodeChecker\Api\Data\ConfigArrayModifierInterface"
type="Elgentos\ZipcodeChecker\Model\Config\ConfigArrayModifier"/>

<preference for="Elgentos\ZipcodeChecker\Api\Data\FieldListModifierInterface"
type="Elgentos\ZipcodeChecker\Model\HyvaCheckout\FieldListModifier"/>

<type name="Magento\Framework\Console\CommandListInterface">
<arguments>
<argument name="commands" xsi:type="array">
Expand Down
54 changes: 43 additions & 11 deletions src/view/frontend/templates/form/field/search.phtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<?php

/**
* Copyright Elgentos BV. All rights reserved.
* https://www.elgentos.nl/
*/

declare(strict_types=1);

use Hyva\Checkout\Model\Form\EntityFieldInterface;
use Hyva\Theme\Model\ViewModelRegistry;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;
use Magewirephp\Magewire\Component\Form as MagewireFormComponent;

/** @var Template $block */
/** @var EntityFieldInterface $element */
/** @var ViewModelRegistry $viewModels */
Expand All @@ -10,21 +21,42 @@ declare(strict_types=1);

/** @Tailwind md:w-1/4 md:w-2/4 md:w-3/4 md:w-4/4 mb-2 */

use Hyva\Checkout\Model\Form\EntityFieldInterface;
use Hyva\Theme\Model\ViewModelRegistry;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;
use Magewirephp\Magewire\Component\Form as MagewireFormComponent;

$element = $block->getData('element');
$attributes = $element->getAttributes();
$renderer = $element->getRenderer();
?>
<?php if ($element->hasRelatives()): ?>
<div class="grid grid-cols-2 gap-y-2 gap-x-4 place-items-end">
<div class="w-full font-medium text-gray-700 relative <?= /* @noEscape */ $element->isRequired() ? 'required' : 'not-required' ?>">
<?= /* @noEscape */ $renderer->renderLabel($element) ?>
<?= /* @noEscape */ $renderer->renderBefore($element) ?>

<?php if ($element->hasRelatives()): ?>
<div class="space-y-2">
<?php endif ?>

<div class="flex items-center gap-4">
<input class="<?= $escaper->escapeHtmlAttr($element->renderClass(['block w-full form-input grow renderer-text'])) ?>"
<?php if ($element->hasAttributes()): ?>
<?= /* @noEscape */ $element->renderAttributes($escaper) ?>
<?php endif ?>
/>

<?= /* @noEscape */ $element->getRenderer()->renderTooltip($element) ?>
</div>

<?= /* @noEscape */ $element->getRenderer()->renderComment($element) ?>

<?php if ($element->hasRelatives()): ?>
<?php foreach ($element->getRelatives() as $relative): ?>
<?php if ($relative->isVisible()): ?>
<?= /* @noEscape */ $relative->render() ?>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>

<?php if ($element->hasRelatives()): ?>
</div>
<?php endif ?>

<div class="w-full font-medium text-gray-700 <?= /* @noEscape */ $element->isRequired() ? 'required' : 'not-required' ?>">
<h1>SEARCH</h1>
<?= /* @noEscape */ $element->getRenderer()->renderAfter($element) ?>
<?= /* @noEscape */ $renderer->renderAfter($element) ?>
</div>