LanguageFormatter.php 4.1 KB

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