AddressExport.php in Entity Export CSV 8
File
src/Plugin/FieldTypeExport/AddressExport.php
View source
<?php
namespace Drupal\entity_export_csv\Plugin\FieldTypeExport;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\entity_export_csv\Plugin\FieldTypeExportBase;
use Drupal\Core\Field\FieldItemInterface;
class AddressExport extends FieldTypeExportBase {
public function getSummary() {
return [
'message' => [
'#markup' => $this
->t('Address field type exporter.'),
],
];
}
public function massageExportPropertyValue(FieldItemInterface $field_item, $property_name, FieldDefinitionInterface $field_definition, $options = []) {
return parent::massageExportPropertyValue($field_item, $property_name, $field_definition, $options);
}
public function getFieldProperties(FieldDefinitionInterface $definition) {
$properties = parent::getFieldProperties($definition);
$properties = array_reverse($properties);
$properties = $this
->moveKeyBefore($properties, 'address_line2', 'address_line1');
$properties = $this
->moveKeyBefore($properties, 'administrative_area', 'sorting_code');
$properties = $this
->moveKeyBefore($properties, 'locality', 'postal_code');
$this->properties = $properties;
return $this->properties;
}
protected function moveKeyBefore(array $array, $find, $move) {
if (!isset($array[$find], $array[$move])) {
return $array;
}
$element = [
$move => $array[$move],
];
$start = array_splice($array, 0, array_search($find, array_keys($array)));
unset($start[$move]);
return $start + $element + $array;
}
}