Loading src/Plugin/Field/FieldType/OrderProfile.php +15 −1 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\commerce_api\Plugin\Field\FieldType; use Drupal\commerce_api\TypedData\AddressDataDefinition; use Drupal\commerce_api\TypedData\TaxNumberDataDefinition; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\Core\Entity\ContentEntityType; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\TypedData\EntityDataDefinition; use Drupal\Core\Field\FieldItemBase; Loading Loading @@ -37,9 +38,19 @@ final class OrderProfile extends FieldItemBase { ->setInternal(TRUE); $profile_type = $field_definition->getSetting('profile_bundle') ?: 'customer'; $entity_type = \Drupal::entityTypeManager()->getDefinition('profile'); assert($entity_type instanceof ContentEntityType); $entity_field_manager = \Drupal::getContainer()->get('entity_field.manager'); assert($entity_field_manager instanceof EntityFieldManagerInterface); $fields = $entity_field_manager->getFieldDefinitions('profile', $profile_type); // Exclude keys, revision keys, and other base fields. $excluded_fields = array_merge( array_values($entity_type->getKeys()), array_values($entity_type->getRevisionMetadataKeys()), ['is_default', 'data', 'created', 'changed'], ); foreach ($fields as $field) { if ($field->getType() === 'address') { $data_definition = AddressDataDefinition::create('address') Loading @@ -49,9 +60,12 @@ final class OrderProfile extends FieldItemBase { $data_definition = TaxNumberDataDefinition::create('tax_number') ->setLabel(t('Tax number')); } else { elseif (in_array($field->getName(), $excluded_fields, TRUE)) { continue; } else { $data_definition = $field->getItemDefinition(); } $properties[$field->getName()] = $data_definition; } Loading src/Plugin/Field/FieldType/OrderProfileItemList.php +2 −2 Original line number Diff line number Diff line Loading @@ -19,9 +19,9 @@ final class OrderProfileItemList extends FieldItemList { $value = [ 'entity' => $profile, ]; $supported_field_types = ['address', 'commerce_tax_number']; $property_definitions = $this->getDataDefinition()->getPropertyDefinitions(); foreach ($profile->getFieldDefinitions() as $field_name => $field_definition) { if (!in_array($field_definition->getType(), $supported_field_types, TRUE)) { if (!isset($property_definitions[$field_name])) { continue; } if ($profile->get($field_name)->isEmpty()) { Loading tests/src/Kernel/Field/OrderProfileFieldTest.php +61 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,8 @@ use Drupal\commerce_order\Entity\Order; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\commerce_shipping\Entity\ShipmentType; use Drupal\commerce_tax\Plugin\Commerce\TaxNumberType\VerificationResult; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\jsonapi\JsonApiResource\ResourceObject; use Drupal\jsonapi\Normalizer\Value\CacheableNormalization; use Drupal\profile\Entity\Profile; Loading Loading @@ -210,10 +212,67 @@ final class OrderProfileFieldTest extends KernelTestBase { ], array_filter($billing_information->tax_number)); } /** * Test with a custom field. */ public function testWithCustomField() { $field_storage = FieldStorageConfig::create([ 'field_name' => 'mobile_test', 'entity_type' => 'profile', 'type' => 'string', ]); $field_storage->save(); $field = FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => 'customer', 'label' => 'Mobile phone', ]); $field->save(); $order = $this->createOrder(); $profile = Profile::create([ 'type' => 'customer', 'uid' => 0, 'address' => [ 'country_code' => 'US', 'postal_code' => '53177', 'locality' => 'Milwaukee', 'address_line1' => 'Pabst Blue Ribbon Dr', 'administrative_area' => 'WI', 'given_name' => 'Frederick', 'family_name' => 'Pabst', ], 'mobile_test' => '+3361111111', ]); assert($profile instanceof ProfileInterface); $order->setBillingProfile($profile); $billing_information = $order->get('billing_information')->first(); assert($billing_information instanceof OrderProfile); $this->assertEquals('+3361111111', $billing_information->get('mobile_test')->value); $billing_information->mobile_test = '1111'; $this->assertEquals('1111', $billing_information->get('mobile_test')->value); } /** * Tests normalization of the field. */ public function testNormalization() { $field_storage = FieldStorageConfig::create([ 'field_name' => 'mobile_test', 'entity_type' => 'profile', 'type' => 'string', ]); $field_storage->save(); $field = FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => 'customer', 'label' => 'Mobile phone', ]); $field->save(); $order = $this->createOrder(); $profile = Profile::create([ 'type' => 'customer', Loading @@ -227,6 +286,7 @@ final class OrderProfileFieldTest extends KernelTestBase { 'given_name' => 'Frederick', 'family_name' => 'Pabst', ], 'mobile_test' => '+3361111111', ]); assert($profile instanceof ProfileInterface); $order->setBillingProfile($profile); Loading @@ -252,6 +312,7 @@ final class OrderProfileFieldTest extends KernelTestBase { 'given_name' => 'Frederick', 'family_name' => 'Pabst', ], 'mobile_test' => '+3361111111', ], $normalization); } Loading tests/src/Kernel/Resource/Checkout/CheckoutResourceTest.php +6 −0 Original line number Diff line number Diff line Loading @@ -189,6 +189,7 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'country_code' => 'US', 'postal_code' => '94043', ], 'mobile_test' => NULL, ], ], [ Loading Loading @@ -232,6 +233,7 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'administrative_area' => 'CA', 'postal_code' => '11111', ], 'mobile_test' => NULL, ], ], [ Loading Loading @@ -273,6 +275,7 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'country_code' => 'US', 'postal_code' => '94043', ], 'mobile_test' => NULL, ], 'shipping_method' => '2--default', 'order_total' => [ Loading Loading @@ -350,6 +353,7 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'given_name' => 'Bryan', 'family_name' => 'Centarro', ], 'mobile_test' => '+3361111111', ], 'payment_instrument' => [ 'payment_gateway_id' => 'onsite', Loading @@ -375,12 +379,14 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'given_name' => 'Bryan', 'family_name' => 'Centarro', ], 'mobile_test' => '+3361111111', ], 'shipping_information' => [ 'address' => [ 'country_code' => 'US', 'postal_code' => '94043', ], 'mobile_test' => NULL, ], 'payment_instrument' => [ 'payment_gateway_id' => 'onsite', Loading tests/src/Kernel/Resource/Checkout/CheckoutResourceTestBase.php +15 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,8 @@ use Drupal\commerce_shipping\Entity\ShippingMethod; use Drupal\Component\Serialization\Json; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceModifierInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\Tests\commerce_api\Kernel\KernelTestBase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; Loading Loading @@ -52,6 +54,19 @@ abstract class CheckoutResourceTestBase extends KernelTestBase implements Servic protected function setUp(): void { parent::setUp(); $field_storage = FieldStorageConfig::create([ 'field_name' => 'mobile_test', 'entity_type' => 'profile', 'type' => 'string', ]); $field_storage->save(); $field = FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => 'customer', 'label' => 'Mobile phone', ]); $field->save(); $this->installEntitySchema('commerce_payment_method'); $this->installEntitySchema('commerce_shipment'); $this->installEntitySchema('commerce_shipping_method'); Loading Loading
src/Plugin/Field/FieldType/OrderProfile.php +15 −1 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\commerce_api\Plugin\Field\FieldType; use Drupal\commerce_api\TypedData\AddressDataDefinition; use Drupal\commerce_api\TypedData\TaxNumberDataDefinition; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\Core\Entity\ContentEntityType; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\TypedData\EntityDataDefinition; use Drupal\Core\Field\FieldItemBase; Loading Loading @@ -37,9 +38,19 @@ final class OrderProfile extends FieldItemBase { ->setInternal(TRUE); $profile_type = $field_definition->getSetting('profile_bundle') ?: 'customer'; $entity_type = \Drupal::entityTypeManager()->getDefinition('profile'); assert($entity_type instanceof ContentEntityType); $entity_field_manager = \Drupal::getContainer()->get('entity_field.manager'); assert($entity_field_manager instanceof EntityFieldManagerInterface); $fields = $entity_field_manager->getFieldDefinitions('profile', $profile_type); // Exclude keys, revision keys, and other base fields. $excluded_fields = array_merge( array_values($entity_type->getKeys()), array_values($entity_type->getRevisionMetadataKeys()), ['is_default', 'data', 'created', 'changed'], ); foreach ($fields as $field) { if ($field->getType() === 'address') { $data_definition = AddressDataDefinition::create('address') Loading @@ -49,9 +60,12 @@ final class OrderProfile extends FieldItemBase { $data_definition = TaxNumberDataDefinition::create('tax_number') ->setLabel(t('Tax number')); } else { elseif (in_array($field->getName(), $excluded_fields, TRUE)) { continue; } else { $data_definition = $field->getItemDefinition(); } $properties[$field->getName()] = $data_definition; } Loading
src/Plugin/Field/FieldType/OrderProfileItemList.php +2 −2 Original line number Diff line number Diff line Loading @@ -19,9 +19,9 @@ final class OrderProfileItemList extends FieldItemList { $value = [ 'entity' => $profile, ]; $supported_field_types = ['address', 'commerce_tax_number']; $property_definitions = $this->getDataDefinition()->getPropertyDefinitions(); foreach ($profile->getFieldDefinitions() as $field_name => $field_definition) { if (!in_array($field_definition->getType(), $supported_field_types, TRUE)) { if (!isset($property_definitions[$field_name])) { continue; } if ($profile->get($field_name)->isEmpty()) { Loading
tests/src/Kernel/Field/OrderProfileFieldTest.php +61 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,8 @@ use Drupal\commerce_order\Entity\Order; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\commerce_shipping\Entity\ShipmentType; use Drupal\commerce_tax\Plugin\Commerce\TaxNumberType\VerificationResult; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\jsonapi\JsonApiResource\ResourceObject; use Drupal\jsonapi\Normalizer\Value\CacheableNormalization; use Drupal\profile\Entity\Profile; Loading Loading @@ -210,10 +212,67 @@ final class OrderProfileFieldTest extends KernelTestBase { ], array_filter($billing_information->tax_number)); } /** * Test with a custom field. */ public function testWithCustomField() { $field_storage = FieldStorageConfig::create([ 'field_name' => 'mobile_test', 'entity_type' => 'profile', 'type' => 'string', ]); $field_storage->save(); $field = FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => 'customer', 'label' => 'Mobile phone', ]); $field->save(); $order = $this->createOrder(); $profile = Profile::create([ 'type' => 'customer', 'uid' => 0, 'address' => [ 'country_code' => 'US', 'postal_code' => '53177', 'locality' => 'Milwaukee', 'address_line1' => 'Pabst Blue Ribbon Dr', 'administrative_area' => 'WI', 'given_name' => 'Frederick', 'family_name' => 'Pabst', ], 'mobile_test' => '+3361111111', ]); assert($profile instanceof ProfileInterface); $order->setBillingProfile($profile); $billing_information = $order->get('billing_information')->first(); assert($billing_information instanceof OrderProfile); $this->assertEquals('+3361111111', $billing_information->get('mobile_test')->value); $billing_information->mobile_test = '1111'; $this->assertEquals('1111', $billing_information->get('mobile_test')->value); } /** * Tests normalization of the field. */ public function testNormalization() { $field_storage = FieldStorageConfig::create([ 'field_name' => 'mobile_test', 'entity_type' => 'profile', 'type' => 'string', ]); $field_storage->save(); $field = FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => 'customer', 'label' => 'Mobile phone', ]); $field->save(); $order = $this->createOrder(); $profile = Profile::create([ 'type' => 'customer', Loading @@ -227,6 +286,7 @@ final class OrderProfileFieldTest extends KernelTestBase { 'given_name' => 'Frederick', 'family_name' => 'Pabst', ], 'mobile_test' => '+3361111111', ]); assert($profile instanceof ProfileInterface); $order->setBillingProfile($profile); Loading @@ -252,6 +312,7 @@ final class OrderProfileFieldTest extends KernelTestBase { 'given_name' => 'Frederick', 'family_name' => 'Pabst', ], 'mobile_test' => '+3361111111', ], $normalization); } Loading
tests/src/Kernel/Resource/Checkout/CheckoutResourceTest.php +6 −0 Original line number Diff line number Diff line Loading @@ -189,6 +189,7 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'country_code' => 'US', 'postal_code' => '94043', ], 'mobile_test' => NULL, ], ], [ Loading Loading @@ -232,6 +233,7 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'administrative_area' => 'CA', 'postal_code' => '11111', ], 'mobile_test' => NULL, ], ], [ Loading Loading @@ -273,6 +275,7 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'country_code' => 'US', 'postal_code' => '94043', ], 'mobile_test' => NULL, ], 'shipping_method' => '2--default', 'order_total' => [ Loading Loading @@ -350,6 +353,7 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'given_name' => 'Bryan', 'family_name' => 'Centarro', ], 'mobile_test' => '+3361111111', ], 'payment_instrument' => [ 'payment_gateway_id' => 'onsite', Loading @@ -375,12 +379,14 @@ final class CheckoutResourceTest extends CheckoutResourceTestBase { 'given_name' => 'Bryan', 'family_name' => 'Centarro', ], 'mobile_test' => '+3361111111', ], 'shipping_information' => [ 'address' => [ 'country_code' => 'US', 'postal_code' => '94043', ], 'mobile_test' => NULL, ], 'payment_instrument' => [ 'payment_gateway_id' => 'onsite', Loading
tests/src/Kernel/Resource/Checkout/CheckoutResourceTestBase.php +15 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,8 @@ use Drupal\commerce_shipping\Entity\ShippingMethod; use Drupal\Component\Serialization\Json; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceModifierInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\Tests\commerce_api\Kernel\KernelTestBase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; Loading Loading @@ -52,6 +54,19 @@ abstract class CheckoutResourceTestBase extends KernelTestBase implements Servic protected function setUp(): void { parent::setUp(); $field_storage = FieldStorageConfig::create([ 'field_name' => 'mobile_test', 'entity_type' => 'profile', 'type' => 'string', ]); $field_storage->save(); $field = FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => 'customer', 'label' => 'Mobile phone', ]); $field->save(); $this->installEntitySchema('commerce_payment_method'); $this->installEntitySchema('commerce_shipment'); $this->installEntitySchema('commerce_shipping_method'); Loading