Skip to content
Snippets Groups Projects
Commit bcd2d69a authored by Dimitris Bozelos's avatar Dimitris Bozelos
Browse files

Issue #3358050 Added field transformer for exporting address fields

parent db56c98e
Branches
Tags
No related merge requests found
<?php
namespace Drupal\acumatica\Plugin\EntitySync\FieldTransformer;
use Drupal\entity_sync\FieldTransformer\PluginBase;
use Drupal\Core\Entity\ContentEntityInterface;
/**
* Transformer that imports/exports the value of address fields.
*
* It converts the properties of Drupal `address` fields provided by the
* `address` module to an array containing the properties as expected by
* Acumatica address objects.
*
* Currently only exports are supported, support for imports should be added
* here as well.
*
* The property mapping is as follows (Drupal/Acumatica):
* - country_code/country
* - administrative_area/state
* - locality/city
* - postal_code/postal_code
* - address_line1/address_line1
* - address_line2/address_line2
*
* The following properties are ignored in exports because they are not
* supported by Acumatica address fields:
* - dependent_locality
* - sorting_code
* - organization
* - given_name
* - additional_name
* - family_name
*
* To further convert the array into an address object, for example, if required
* to be sent as an object referenced in another object's field e.g. a
* contact's address, further transform the array in a pipeline with the
* `array_to_object` field transformer.
*
* @EntitySyncFieldTransformer(
* id = "acumatica_address"
* )
*/
class Address extends PluginBase {
/**
* {@inheritdoc}
*
* The address must be passed as an array containing the properties, as
* returned by the field API's `getValue` method.
*/
protected function transformExportedValue(
$address,
ContentEntityInterface $local_entity,
$remote_entity_id,
array $field_info,
array $context
) {
if ($address === NULL) {
return NULL;
}
return [
'country' => $this->transformProperty($address, 'country_code'),
'state' => $this->transformProperty($address, 'administrative_area'),
'city' => $this->transformProperty($address, 'locality'),
'postal_code' => $this->transformProperty($address, 'postal_code'),
'address_line1' => $this->transformProperty($address, 'address_line1'),
'address_line2' => $this->transformProperty($address, 'address_line2'),
];
}
/**
* Transforms a Drupal address property to an Acumatica field property.
*
* @param array $address
* The address field property array.
* @param string $property
* The property to transform.
*
* @return array
* The property transformed into an Acumatica property array.
*/
protected function transformProperty(
array $address,
string $property
) {
if ($address[$property] === NULL || $address[$property] === '') {
return NULL;
}
return ['value' => $address[$property]];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment