LanguageSelect.php in Webform 6.x
File
src/Plugin/WebformElement/LanguageSelect.php
View source
<?php
namespace Drupal\webform\Plugin\WebformElement;
use Drupal\webform\Plugin\WebformElementBase;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LanguageSelect extends WebformElementBase {
protected $languageManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->languageManager = $container
->get('language_manager');
return $instance;
}
protected function defineDefaultProperties() {
$properties = parent::defineDefaultProperties();
unset($properties['format_items'], $properties['format_items_html'], $properties['format_items_text']);
return $properties;
}
protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
$language = $this->languageManager
->getLanguage($value);
$format = $this
->getItemFormat($element);
switch ($format) {
case 'langcode':
return $language
->getId();
case 'language':
return $language
->getName();
case 'text':
default:
return sprintf('%s (%s)', $language
->getName(), $language
->getId());
}
}
public function getItemDefaultFormat() {
return 'text';
}
public function getItemFormats() {
return parent::getItemFormats() + [
'text' => $this
->t('Text'),
'langcode' => $this
->t('Langcode'),
'language' => $this
->t('Language'),
];
}
}