custom/plugins/FourtwosixPhoneValidation/src/FourtwosixPhoneValidation.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixPhoneValidation;
  3. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressDefinition;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AndFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\System\Country\CountryDefinition;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. class FourtwosixPhoneValidation extends Plugin
  13. {
  14.     public function install(InstallContext $context): void
  15.     {
  16.         $countryRepository $this->container->get('country.repository');
  17.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  18.         if (is_null($customFieldSetRepository)) {
  19.             throw new \RuntimeException("country.repository is null");
  20.         }
  21.         if (is_null($countryRepository)) {
  22.             throw new \RuntimeException("custom_field_set.repository is null");
  23.         }
  24.         $criteria = new Criteria();
  25.         $criteria->addFilter(
  26.             new AndFilter([
  27.                 new EqualsFilter("name""fourtwosix_phone_validation"),
  28.                 new EqualsFilter("name""fourtwosix_customer_prefix"),
  29.             ])
  30.         );
  31.         $criteria->addFilter(
  32.             new AndFilter([
  33.                 new EqualsFilter("customFields.name""fourtwosix_phone_country_prefix"),
  34.                 new EqualsFilter("customFields.name""fourtwosix_customer_address_prefix"),
  35.             ])
  36.         );
  37.         /** @var EntitySearchResult $existSet */
  38.         $existSet $customFieldSetRepository->search($criteria$context->getContext())->count();
  39.         // exist already
  40.         if ($existSet) {
  41.             return;
  42.         }
  43.         // If it does not exist create customFields Set
  44.         $countryPrefix = [
  45.             'name' => 'fourtwosix_phone_validation',
  46.             'config' => [
  47.                 'label' => [
  48.                     'de-DE' => 'Länder-Telefonvorwahlen',
  49.                     'en-GB' => 'Phone prefixes per country',
  50.                     'it-IT' => 'Prefissi telefonici per nazione',
  51.                 ],
  52.             ],
  53.             "relations" => [
  54.                 [
  55.                     "entityName" => CountryDefinition::ENTITY_NAME,
  56.                 ],
  57.             ],
  58.             'customFields' => [
  59.                 [
  60.                     'name' => 'fourtwosix_phone_country_prefix',
  61.                     'type' => CustomFieldTypes::TEXT,
  62.                     'config' => [
  63.                         'label' => [
  64.                             'de-DE' => 'Ländercode',
  65.                             'en-GB' => 'Phone country prefixes',
  66.                             'it-IT' => 'Prefisso telefonico per nazione',
  67.                         ],
  68.                         'helpText' => [
  69.                             'de-DE' => 'Mehrere Werte durch Kommas getrennt eingeben',
  70.                             'en-GB' => 'If multiple values separate them with a comma',
  71.                             'it-IT' => 'Inserire valori multipli separati da virgola',
  72.                         ],
  73.                         'customFieldPosition' => 1,
  74.                     ],
  75.                 ],
  76.             ],
  77.         ];
  78.         $customerAddressPrefix = [
  79.             'name' => 'fourtwosix_customer_prefix',
  80.             'config' => [
  81.                 'label' => [
  82.                     'de-DE' => 'Präfix der Kundenadresse',
  83.                     'en-GB' => 'Customer address prefix',
  84.                     'it-IT' => 'Prefisso dell\'indirizzo del customer',
  85.                 ],
  86.             ],
  87.             "relations" => [
  88.                 [
  89.                     "entityName" => CustomerAddressDefinition::ENTITY_NAME,
  90.                 ],
  91.             ],
  92.             'customFields' => [
  93.                 [
  94.                     'name' => 'fourtwosix_customer_address_prefix',
  95.                     'type' => CustomFieldTypes::TEXT,
  96.                     "allowCustomerWrite" => true,
  97.                     // needed to be able to save custom fields via databag in route decoration
  98.                     'config' => [
  99.                         'label' => [
  100.                             'de-DE' => 'Präfix der Kundenadresse',
  101.                             'en-GB' => 'Customer address prefix',
  102.                             'it-IT' => 'Prefisso dell\'indirizzo del customer',
  103.                         ],
  104.                         'helpText' => [
  105.                             'de-DE' => 'Dient zur Speicherung von Daten über die Vorwahl von Telefonadressen',
  106.                             'en-GB' => 'Used to store data regarding phone address prefix',
  107.                             'it-IT' => 'Usato per salvare i dati relativi al prefisso del cliente',
  108.                         ],
  109.                         'customFieldPosition' => 1,
  110.                     ],
  111.                 ],
  112.             ],
  113.         ];
  114.         $customFieldSetRepository->create(
  115.             [$countryPrefix$customerAddressPrefix],
  116.             $context->getContext()
  117.         );
  118.     }
  119. }