/** * Gets the address values used for rendering. * * @param \Drupal\address\AddressInterface $address * The address. * @param \Drupal\address\Entity\AddressFormatInterface $address_format * The address format. * * @return array * The values, keyed by address field. */ protected function getValues(AddressInterface $address, AddressFormatInterface $address_format) { $values = []; foreach (AddressField::getAll() as $field) { $getter = 'get' . ucfirst($field); $values[$field] = $address->{$getter}(); } foreach ($address_format->getUsedSubdivisionFields() as $field) { $value = $values[$field]; // The template needs access to both the subdivision code and name. $values[$field] = ['code' => '', 'name' => $value]; if (empty($value)) { // This level is empty, so there can be no sublevels. break; } $subdivision = $this->subdivisionRepository->get($value, $address->getLocale()); if (!$subdivision) { // This level has no predefined subdivisions, stop. break; } // Replace the subdivision values with the predefined ones. $values[$field] = ['code' => $subdivision->getCode(), 'name' => $subdivision->getName()]; if (!$subdivision->hasChildren()) { // The current subdivision has no children, stop. break; } } return $values; }
/** * Gets the address values used for rendering. * * @param \Drupal\address\AddressInterface $address * The address. * @param \Drupal\address\Entity\AddressFormatInterface $address_format * The address format. * * @return array * The values, keyed by address field. */ protected function getValues(AddressInterface $address, AddressFormatInterface $address_format) { $values = []; foreach (AddressField::getAll() as $field) { $getter = 'get' . ucfirst($field); $values[$field] = $address->{$getter}(); } // Replace the subdivision values with the names of any predefined ones. foreach ($address_format->getUsedSubdivisionFields() as $field) { if (empty($values[$field])) { // This level is empty, so there can be no sublevels. break; } $subdivision = $this->subdivisionRepository->get($values[$field], $address->getLocale()); if (!$subdivision) { // This level has no predefined subdivisions, stop. break; } $values[$field] = $subdivision->getCode(); if (!$subdivision->hasChildren()) { // The current subdivision has no children, stop. break; } } return $values; }
/** * Processes the subdivision elements, adding predefined values where found. * * @param array $element * The existing form element array. * @param array $values * An array of address values, keyed by property name. * @param \Drupal\address\Entity\AddressFormatInterface $address_format * The address format. * * @return array * The processed form element array. */ protected function processSubdivisionElements(array $element, array $values, AddressFormatInterface $address_format) { $depth = $this->subdivisionRepository->getDepth($values['country_code']); if ($depth === 0) { // No predefined data found. return $element; } $subdivision_properties = []; foreach ($address_format->getUsedSubdivisionFields() as $field) { $subdivision_properties[] = FieldHelper::getPropertyName($field); } // Load and insert the subdivisions for each parent id. $currentDepth = 1; foreach ($subdivision_properties as $index => $property) { if (!isset($element[$property]) || !Element::isVisibleElement($element[$property])) { break; } $parent_property = $index ? $subdivision_properties[$index - 1] : NULL; if ($parent_property && empty($values[$parent_property])) { break; } $parent_id = $parent_property ? $values[$parent_property] : NULL; $subdivisions = $this->subdivisionRepository->getList($values['country_code'], $parent_id); if (empty($subdivisions)) { break; } $element[$property]['#type'] = 'select'; $element[$property]['#options'] = $subdivisions; $element[$property]['#empty_value'] = ''; unset($element[$property]['#size']); if ($currentDepth < $depth) { $element[$property]['#ajax'] = ['callback' => [get_class($this), 'ajaxRefresh'], 'wrapper' => $element['#wrapper_id']]; } $currentDepth++; } return $element; }