You are here

public function AddressEventSubscriber::getInitialValues in Quick Node Clone 8

Generate a set of initial values.

Return value

array The initial values.

1 call to AddressEventSubscriber::getInitialValues()
AddressEventSubscriber::onInitialValues in src/EventSubscriber/AddressEventSubscriber.php
Alters the initial values.

File

src/EventSubscriber/AddressEventSubscriber.php, line 79

Class

AddressEventSubscriber
Support for cloning address data.

Namespace

Drupal\quick_node_clone\EventSubscriber

Code

public function getInitialValues($event) {
  $tempstore = $this->privateTempStoreFactory
    ->get('quick_node_clone');
  if ($tempstore
    ->get('address_initial_value_delta') == NULL) {
    $tempstore
      ->set('address_initial_value_delta', 0);
  }
  $node = $this->quickNodeCloneNodeFinder
    ->findNodeFromCurrentPath();
  if ($node == NULL) {
    return [];
  }
  $address = [];
  $delta = $tempstore
    ->get('address_initial_value_delta');
  foreach ($node
    ->getFieldDefinitions() as $field_definition) {
    $field_storage_definition = $field_definition
      ->getFieldStorageDefinition();
    $field_name = $field_storage_definition
      ->getName();
    if ($field_storage_definition
      ->getType() == "address") {
      if (!$node
        ->get($field_name)
        ->isEmpty()) {
        foreach ($node
          ->get($field_name) as $key => $value) {
          if ($key == $delta) {
            $address = [
              'country_code' => $value
                ->getCountryCode(),
              'postal_code' => $value
                ->getPostalCode(),
              'administrative_area' => $value
                ->getAdministrativeArea(),
              'locality' => $value
                ->getLocality(),
              'dependent_locality' => $value
                ->getDependentLocality(),
              'sorting_code' => $value
                ->getSortingCode(),
              'address_line1' => $value
                ->getAddressLine1(),
              'address_line2' => $value
                ->getAddressLine2(),
              'organization' => $value
                ->getOrganization(),
              'additional_name' => $value
                ->getAdditionalName(),
              'given_name' => $value
                ->getGivenName(),
              'family_name' => $value
                ->getFamilyName(),
            ];
          }
        }
      }
    }
  }
  $delta++;
  $tempstore
    ->set('address_initial_value_delta', $delta);
  return $address;
}