/** * Gets the field labels suitable for the given address format. * * Intended to be shown to the end user, they sometimes use a more familiar * term than the field name (Company instead of Organization, Contact name * instead of Recipient, etc). * * @param \Drupal\address\Entity\AddressFormatInterface $address_format * The address format. * * @return string[] * An array of labels, keyed by field. */ public static function getFieldLabels(AddressFormatInterface $address_format) { $administrative_area_type = $address_format->getAdministrativeAreaType(); $locality_type = $address_format->getLocalityType(); $dependent_locality_type = $address_format->getDependentLocalityType(); $postal_code_type = $address_format->getPostalCodeType(); return [AddressField::ADMINISTRATIVE_AREA => self::getAdministrativeAreaLabel($administrative_area_type), AddressField::LOCALITY => self::getLocalityLabel($locality_type), AddressField::DEPENDENT_LOCALITY => self::getDependentLocalityLabel($dependent_locality_type), AddressField::POSTAL_CODE => self::getPostalCodeLabel($postal_code_type), AddressField::SORTING_CODE => t('Cedex'), AddressField::ADDRESS_LINE1 => t('Street address'), AddressField::ADDRESS_LINE2 => t('Street address line 2'), AddressField::ORGANIZATION => t('Company'), AddressField::RECIPIENT => t('Contact name')]; }
/** * 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; }
/** * Builds the postal code form elements. * * @param array $form * The form. * @param array $values * The form values. * @param \Drupal\address\Entity\AddressFormatInterface $address_format * The address format for the selected country. * * @return array * The form with the added postal code elements. */ protected function buildPostalCodeElements(array $form, array $values, AddressFormatInterface $address_format) { if (!in_array(AddressField::POSTAL_CODE, $address_format->getUsedFields())) { // The address format doesn't use a postal code field. return $form; } $form['included_postal_codes'] = ['#type' => 'textfield', '#title' => $this->t('Included postal codes'), '#description' => $this->t('A regular expression ("/(35|38)[0-9]{3}/") or comma-separated list, including ranges ("98, 100:200")'), '#default_value' => $values['included_postal_codes']]; $form['excluded_postal_codes'] = ['#type' => 'textfield', '#title' => $this->t('Excluded postal codes'), '#description' => $this->t('A regular expression ("/(35|38)[0-9]{3}/") or comma-separated list, including ranges ("98, 100:200")'), '#default_value' => $values['excluded_postal_codes']]; return $form; }
/** * 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; }
/** * 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; }