View source
<?php
namespace Drupal\address\Plugin\Field\FieldFormatter;
use CommerceGuys\Addressing\AddressFormat\AddressField;
use CommerceGuys\Addressing\AddressFormat\AddressFormat;
use CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use CommerceGuys\Addressing\Locale;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface;
use Drupal\address\AddressInterface;
use Drupal\address\FieldHelper;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Security\TrustedCallbackInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AddressDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface, TrustedCallbackInterface {
protected $addressFormatRepository;
protected $countryRepository;
protected $subdivisionRepository;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AddressFormatRepositoryInterface $address_format_repository, CountryRepositoryInterface $country_repository, SubdivisionRepositoryInterface $subdivision_repository) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->addressFormatRepository = $address_format_repository;
$this->countryRepository = $country_repository;
$this->subdivisionRepository = $subdivision_repository;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('address.address_format_repository'), $container
->get('address.country_repository'), $container
->get('address.subdivision_repository'));
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#prefix' => '<p class="address" translate="no">',
'#suffix' => '</p>',
'#post_render' => [
[
get_class($this),
'postRender',
],
],
'#cache' => [
'contexts' => [
'languages:' . LanguageInterface::TYPE_INTERFACE,
],
],
];
$elements[$delta] += $this
->viewElement($item, $langcode);
}
return $elements;
}
protected function viewElement(AddressInterface $address, $langcode) {
$country_code = $address
->getCountryCode();
$countries = $this->countryRepository
->getList();
$address_format = $this->addressFormatRepository
->get($country_code);
$values = $this
->getValues($address, $address_format);
$element = [
'#address_format' => $address_format,
'#locale' => $address
->getLocale(),
];
$element['country'] = [
'#type' => 'html_tag',
'#tag' => 'span',
'#attributes' => [
'class' => [
'country',
],
],
'#value' => Html::escape($countries[$country_code]),
'#placeholder' => '%country',
];
foreach ($address_format
->getUsedFields() as $field) {
$property = FieldHelper::getPropertyName($field);
$class = str_replace('_', '-', $property);
$element[$property] = [
'#type' => 'html_tag',
'#tag' => 'span',
'#attributes' => [
'class' => [
$class,
],
],
'#value' => Html::escape($values[$field]),
'#placeholder' => '%' . $field,
];
}
return $element;
}
public static function postRender($content, array $element) {
$address_format = $element['#address_format'];
$locale = $element['#locale'];
if (Locale::matchCandidates($address_format
->getLocale(), $locale)) {
$format_string = '%country' . "\n" . $address_format
->getLocalFormat();
}
else {
$format_string = $address_format
->getFormat() . "\n" . '%country';
}
$replacements = [];
foreach (Element::getVisibleChildren($element) as $key) {
$child = $element[$key];
if (isset($child['#placeholder'])) {
$replacements[$child['#placeholder']] = $child['#value'] ? $child['#markup'] : '';
}
}
$content = self::replacePlaceholders($format_string, $replacements);
$content = nl2br($content, FALSE);
return $content;
}
public static function replacePlaceholders($string, array $replacements) {
$replacements = array_map('trim', $replacements);
$string = strtr($string, $replacements);
$lines = explode("\n", $string);
foreach ($lines as $index => $line) {
$line = trim(preg_replace('/^[-,]+/', '', $line, 1));
$line = preg_replace('/\\s\\s+/', ' ', $line);
$lines[$index] = $line;
}
$lines = array_filter($lines);
return implode("\n", $lines);
}
protected function getValues(AddressInterface $address, AddressFormat $address_format) {
$values = [];
foreach (AddressField::getAll() as $field) {
$getter = 'get' . ucfirst($field);
$values[$field] = $address
->{$getter}();
}
$original_values = [];
$subdivision_fields = $address_format
->getUsedSubdivisionFields();
$parents = [];
foreach ($subdivision_fields as $index => $field) {
if (empty($values[$field])) {
break;
}
$parents[] = $index ? $original_values[$subdivision_fields[$index - 1]] : $address
->getCountryCode();
$subdivision = $this->subdivisionRepository
->get($values[$field], $parents);
if (!$subdivision) {
break;
}
$original_values[$field] = $values[$field];
$use_local_name = Locale::matchCandidates($address
->getLocale(), $subdivision
->getLocale());
$values[$field] = $use_local_name ? $subdivision
->getLocalCode() : $subdivision
->getCode();
if (!$subdivision
->hasChildren()) {
break;
}
}
return $values;
}
public static function trustedCallbacks() {
return [
'postRender',
];
}
}