43bbaa5309efb480a4ed003ba30227b2288f4227
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldFormatter / LanguageFormatter.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FieldItemInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Plugin implementation of the 'language' formatter.
15  *
16  * @FieldFormatter(
17  *   id = "language",
18  *   label = @Translation("Language"),
19  *   field_types = {
20  *     "language"
21  *   }
22  * )
23  */
24 class LanguageFormatter extends StringFormatter {
25
26   /**
27    * The language manager.
28    *
29    * @var \Drupal\Core\Language\LanguageManagerInterface
30    */
31   protected $languageManager;
32
33   /**
34    * Constructs a LanguageFormatter instance.
35    *
36    * @param string $plugin_id
37    *   The plugin_id for the formatter.
38    * @param mixed $plugin_definition
39    *   The plugin implementation definition.
40    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
41    *   The definition of the field to which the formatter is associated.
42    * @param array $settings
43    *   The formatter settings.
44    * @param string $label
45    *   The formatter label display setting.
46    * @param string $view_mode
47    *   The view mode.
48    * @param array $third_party_settings
49    *   Any third party settings settings.
50    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
51    *   The entity manager.
52    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
53    *   The language manager.
54    */
55   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
56     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $entity_manager);
57
58     $this->languageManager = $language_manager;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
65     return new static(
66       $plugin_id,
67       $plugin_definition,
68       $configuration['field_definition'],
69       $configuration['settings'],
70       $configuration['label'],
71       $configuration['view_mode'],
72       $configuration['third_party_settings'],
73       $container->get('entity.manager'),
74       $container->get('language_manager')
75     );
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public static function defaultSettings() {
82     $settings = parent::defaultSettings();
83     $settings['native_language'] = FALSE;
84     return $settings;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function settingsForm(array $form, FormStateInterface $form_state) {
91     $form = parent::settingsForm($form, $form_state);
92     $form['native_language'] = [
93       '#title' => $this->t('Display in native language'),
94       '#type' => 'checkbox',
95       '#default_value' => $this->getSetting('native_language'),
96     ];
97     return $form;
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function settingsSummary() {
104     $summary = parent::settingsSummary();
105     if ($this->getSetting('native_language')) {
106       $summary[] = $this->t('Displayed in native language');
107     }
108     return $summary;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   protected function viewValue(FieldItemInterface $item) {
115     // The 'languages' cache context is not necessary because the language is
116     // either displayed in its configured form (loaded directly from config
117     // storage by LanguageManager::getLanguages()) or in its native language
118     // name. That only depends on formatter settings and no language condition.
119     $languages = $this->getSetting('native_language') ? $this->languageManager->getNativeLanguages(LanguageInterface::STATE_ALL) : $this->languageManager->getLanguages(LanguageInterface::STATE_ALL);
120     return [
121       '#plain_text' => $item->language && isset($languages[$item->language->getId()]) ? $languages[$item->language->getId()]->getName() : ''
122     ];
123   }
124
125 }