<?php declare(strict_types=1);
namespace FourtwosixPhoneValidation;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AndFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\System\Country\CountryDefinition;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class FourtwosixPhoneValidation extends Plugin
{
public function install(InstallContext $context): void
{
$countryRepository = $this->container->get('country.repository');
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
if (is_null($customFieldSetRepository)) {
throw new \RuntimeException("country.repository is null");
}
if (is_null($countryRepository)) {
throw new \RuntimeException("custom_field_set.repository is null");
}
$criteria = new Criteria();
$criteria->addFilter(
new AndFilter([
new EqualsFilter("name", "fourtwosix_phone_validation"),
new EqualsFilter("name", "fourtwosix_customer_prefix"),
])
);
$criteria->addFilter(
new AndFilter([
new EqualsFilter("customFields.name", "fourtwosix_phone_country_prefix"),
new EqualsFilter("customFields.name", "fourtwosix_customer_address_prefix"),
])
);
/** @var EntitySearchResult $existSet */
$existSet = $customFieldSetRepository->search($criteria, $context->getContext())->count();
// exist already
if ($existSet) {
return;
}
// If it does not exist create customFields Set
$countryPrefix = [
'name' => 'fourtwosix_phone_validation',
'config' => [
'label' => [
'de-DE' => 'Länder-Telefonvorwahlen',
'en-GB' => 'Phone prefixes per country',
'it-IT' => 'Prefissi telefonici per nazione',
],
],
"relations" => [
[
"entityName" => CountryDefinition::ENTITY_NAME,
],
],
'customFields' => [
[
'name' => 'fourtwosix_phone_country_prefix',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'Ländercode',
'en-GB' => 'Phone country prefixes',
'it-IT' => 'Prefisso telefonico per nazione',
],
'helpText' => [
'de-DE' => 'Mehrere Werte durch Kommas getrennt eingeben',
'en-GB' => 'If multiple values separate them with a comma',
'it-IT' => 'Inserire valori multipli separati da virgola',
],
'customFieldPosition' => 1,
],
],
],
];
$customerAddressPrefix = [
'name' => 'fourtwosix_customer_prefix',
'config' => [
'label' => [
'de-DE' => 'Präfix der Kundenadresse',
'en-GB' => 'Customer address prefix',
'it-IT' => 'Prefisso dell\'indirizzo del customer',
],
],
"relations" => [
[
"entityName" => CustomerAddressDefinition::ENTITY_NAME,
],
],
'customFields' => [
[
'name' => 'fourtwosix_customer_address_prefix',
'type' => CustomFieldTypes::TEXT,
"allowCustomerWrite" => true,
// needed to be able to save custom fields via databag in route decoration
'config' => [
'label' => [
'de-DE' => 'Präfix der Kundenadresse',
'en-GB' => 'Customer address prefix',
'it-IT' => 'Prefisso dell\'indirizzo del customer',
],
'helpText' => [
'de-DE' => 'Dient zur Speicherung von Daten über die Vorwahl von Telefonadressen',
'en-GB' => 'Used to store data regarding phone address prefix',
'it-IT' => 'Usato per salvare i dati relativi al prefisso del cliente',
],
'customFieldPosition' => 1,
],
],
],
];
$customFieldSetRepository->create(
[$countryPrefix, $customerAddressPrefix],
$context->getContext()
);
}
}