Security update for permissions_by_term
[yaffs-website] / vendor / drupal / drupal-extension / fixtures / drupal8 / modules / behat_test / src / Plugin / Field / FieldFormatter / AddressFieldFormatter.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\behat_test\Plugin\Field\FieldFormatter\AddressFieldFormatter.
6  */
7
8 namespace Drupal\behat_test\Plugin\Field\FieldFormatter;
9
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;
16
17 /**
18  * Plugin implementation of the 'behat_test_address_field' formatter.
19  *
20  * @FieldFormatter(
21  *   id = "behat_test_address_field",
22  *   label = @Translation("Address field formatter"),
23  *   module = "behat_test",
24  *   field_types = {
25  *     "behat_test_address_field"
26  *   }
27  * )
28  */
29 class AddressFieldFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
30
31   /**
32    * The country manager.
33    *
34    * @var \Drupal\Core\Locale\CountryManagerInterface;
35    */
36   protected $countryManager;
37
38   /**
39    * Constructs an AddressFieldFormatter object.
40    *
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
52    *   The 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.
57    */
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);
60
61     $this->countryManager = $country_manager;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
68     return new static(
69       $plugin_id,
70       $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')
77     );
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function viewElements(FieldItemListInterface $items, $language) {
84     $elements = [];
85
86     foreach ($items as $delta => $item) {
87       foreach (['country', 'locality', 'thoroughfare', 'postal_code'] as $key) {
88         $value = $item->$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] : '';
93         }
94
95         $elements[$delta][$key] = [
96           '#type' => 'html_tag',
97           '#tag' => 'p',
98           '#value' => $value,
99         ];
100       }
101     }
102
103     return $elements;
104   }
105
106 }