View source
<?php
namespace Drupal\languagefield\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\languagefield\Entity\CustomLanguageManager;
use Drupal\languagefield\Plugin\Field\FieldType\LanguageItem;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LanguageFormatter extends StringFormatter implements ContainerFactoryPluginInterface {
protected $moduleHandler;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $moduleHandler) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $entity_type_manager);
$this->moduleHandler = $moduleHandler;
}
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('entity_type.manager'), $container
->get('module_handler'));
}
public static function defaultSettings() {
$settings = parent::defaultSettings();
$settings['format'] = [
'name' => 'name',
];
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['format'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Display'),
'#description' => $this
->t('Select the elements you want to show. The elements will be concatenated when showing the field.'),
'#default_value' => $this
->getSetting('format'),
'#options' => LanguageItem::settingsOptions('formatter'),
];
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$settings = $this
->getSettings()['format'];
$options = LanguageItem::settingsOptions('formatter');
if (empty($settings)) {
$summary[] = $this
->t('** Not set **');
}
else {
foreach ($settings as $value) {
switch ($value) {
case '0':
break;
default:
$summary[] = isset($options[$value]) ? $options[$value] : '...';
break;
}
}
}
return $summary;
}
protected function viewValue(FieldItemInterface $item) {
$settings = $this
->getSettings();
$langcode = $item->value;
$language = CustomLanguageManager::createFromLangcode($langcode);
$language_translated_name = $language
->getName();
$markup = [];
if (!empty($settings['format']['iso'])) {
$markup[] = $langcode;
}
if (!empty($settings['format']['name'])) {
$markup[] = $language_translated_name;
}
if (!empty($settings['format']['name_native'])) {
$native_name = $item
->getNativeName();
$markup[] = empty($settings['format']['name']) ? $native_name : '(' . $native_name . ')';
}
$markup = empty($markup) ? $language_translated_name : implode(' ', $markup);
$result = [
'#type' => 'processed_text',
'#context' => [
'value' => $item->value,
],
'#format' => $item->format,
];
if (!empty($settings['format']['icon']) && $this->moduleHandler
->moduleExists('languageicons')) {
$result += [
'language' => $language,
'title' => $markup,
];
languageicons_link_add($result, $language_translated_name);
unset($result['language']);
unset($result['html']);
}
else {
$result += [
'#text' => $markup,
];
}
return $result;
}
}