5 * Contains Drupal\behat_test\Plugin\Field\FieldFormatter\AddressFieldFormatter.
8 namespace Drupal\behat_test\Plugin\Field\FieldFormatter;
10 use Drupal\Core\Field\FieldDefinitionInterface;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\Field\FormatterBase;
13 use Drupal\Core\Locale\CountryManagerInterface;
14 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * Plugin implementation of the 'behat_test_address_field' formatter.
21 * id = "behat_test_address_field",
22 * label = @Translation("Address field formatter"),
23 * module = "behat_test",
25 * "behat_test_address_field"
29 class AddressFieldFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
32 * The country manager.
34 * @var \Drupal\Core\Locale\CountryManagerInterface;
36 protected $countryManager;
39 * Constructs an AddressFieldFormatter object.
41 * @param string $plugin_id
42 * The plugin_id for the formatter.
43 * @param mixed $plugin_definition
44 * The plugin implementation definition.
45 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
46 * The definition of the field to which the formatter is associated.
47 * @param array $settings
48 * The formatter settings.
49 * @param string $label
50 * The formatter label display setting.
51 * @param string $view_mode
53 * @param array $third_party_settings
54 * Any third party settings.
55 * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
56 * The country manager.
58 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, CountryManagerInterface $country_manager) {
59 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
61 $this->countryManager = $country_manager;
67 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
71 $configuration['field_definition'],
72 $configuration['settings'],
73 $configuration['label'],
74 $configuration['view_mode'],
75 $configuration['third_party_settings'],
76 $container->get('country_manager')
83 public function viewElements(FieldItemListInterface $items, $language) {
86 foreach ($items as $delta => $item) {
87 foreach (['country', 'locality', 'thoroughfare', 'postal_code'] as $key) {
89 // Replace the country code with the country name.
90 if ($key === 'country') {
91 $countries = $this->countryManager->getList();
92 $value = !empty($countries[$value]) ? $countries[$value] : '';
95 $elements[$delta][$key] = [
96 '#type' => 'html_tag',