protected function buildView(AddressInterface $address, AddressFormat $addressFormat)
{
$countries = $this->countryRepository->getList($this->locale);
$values = $this->getValues($address, $addressFormat);
$view = [];
$view['country'] = ['html_tag' => 'span', 'html_attributes' => ['class' => 'country'], 'value' => $countries[$address->getCountryCode()]];
foreach ($addressFormat->getUsedFields() as $field) {
// The constant is more suitable as a class than the value since
// it's snake_case and not camelCase.
$class = str_replace('_', '-', strtolower(AddressField::getKey($field)));
$view[$field] = ['html_tag' => 'span', 'html_attributes' => ['class' => $class], 'value' => $values[$field]];
}
return $view;
}
/** * {@inheritdoc} */ protected function buildView(AddressInterface $address, AddressFormatInterface $addressFormat) { $view = parent::buildView($address, $addressFormat); // Uppercase fields where required by the format. $uppercaseFields = $addressFormat->getUppercaseFields(); foreach ($uppercaseFields as $uppercaseField) { if (isset($view[$uppercaseField])) { $view[$uppercaseField]['value'] = mb_strtoupper($view[$uppercaseField]['value'], 'utf-8'); } } // Handle international mailing. if ($address->getCountryCode() != $this->originCountryCode) { // Prefix the postal code. $field = AddressField::POSTAL_CODE; if (isset($view[$field])) { $view[$field]['value'] = $addressFormat->getPostalCodePrefix() . $view[$field]['value']; } // Universal Postal Union says: "The name of the country of // destination shall be written preferably in the language of the // country of origin. To avoid any difficulty in the countries of // transit, it is desirable for the name of the country of // destination to be added in an internationally known language. $country = $view['country']['value']; $englishCountries = $this->countryRepository->getList('en'); $englishCountry = $englishCountries[$address->getCountryCode()]; if ($country != $englishCountry) { $country .= ' - ' . $englishCountry; } $view['country']['value'] = mb_strtoupper($country, 'utf-8'); } else { // The country is not written in case of domestic mailing. $view['country']['value'] = ''; } return $view; }