View source
<?php
namespace Drupal\languagefield\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\LanguageItem as LanguageItemBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\OptionsProviderInterface;
use Drupal\Core\Url;
use Drupal\languagefield\Entity\CustomLanguageManager;
class LanguageItem extends LanguageItemBase implements OptionsProviderInterface {
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar_ascii',
'length' => $field_definition
->getSetting('maxlength'),
'not null' => FALSE,
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
public static function defaultStorageSettings() {
$defaultStorageSettings = [
'maxlength' => CustomLanguageManager::LANGUAGEFIELD_LANGCODE_MAXLENGTH,
'language_range' => [
CustomLanguageManager::LANGUAGEFIELD_LANGUAGES_PREDEFINED => CustomLanguageManager::LANGUAGEFIELD_LANGUAGES_PREDEFINED,
],
'included_languages' => [],
'excluded_languages' => [],
'groups' => '',
'allowed_values_function' => 'languagefield_allowed_values',
] + parent::defaultStorageSettings();
return $defaultStorageSettings;
}
public static function settingsOptions($usage = 'formatter') {
$options = [];
$t = \Drupal::translation();
if (\Drupal::moduleHandler()
->moduleExists('languageicons')) {
if ($usage != 'widget') {
$options += [
'icon' => $t
->translate('Language icon'),
];
}
}
$options += [
'iso' => $t
->translate('ISO 639-code'),
'name' => $t
->translate('Name'),
'name_native' => $t
->translate('Native name'),
];
return $options;
}
public static function getLanguageConfigurationValues($code) {
switch ($code) {
case LanguageInterface::LANGCODE_SITE_DEFAULT:
$language = \Drupal::languageManager()
->getDefaultLanguage();
$value = $language
->getId();
break;
case LanguageInterface::LANGCODE_NOT_SPECIFIED:
$value = LanguageInterface::LANGCODE_NOT_SPECIFIED;
break;
case 'current_interface':
$language = \Drupal::languageManager()
->getCurrentLanguage();
$value = $language
->getId();
break;
case 'authors_default':
$user = \Drupal::currentUser();
$language_code = $user
->getPreferredLangcode();
$language = !empty($language_code) ? \Drupal::languageManager()
->getLanguage($language_code) : \Drupal::languageManager()
->getCurrentLanguage();
$value = $language
->getId();
break;
default:
$value = $code;
}
return $value;
}
public function applyDefaultValue($notify = TRUE) {
$this
->setValue(NULL, $notify);
return $this;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = parent::storageSettingsForm($form, $form_state, $has_data);
$settings = $this
->getFieldDefinition()
->getFieldStorageDefinition()
->getSettings();
$languages = $this
->getPossibleOptions();
$url_1 = \Drupal::moduleHandler()
->moduleExists('language') ? Url::fromRoute('entity.configurable_language.collection', [], [])
->toString() : '';
$url_2 = Url::fromRoute('languagefield.custom_language.collection', [], [])
->toString();
$element['language_range'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Enabled languages'),
'#description' => $this
->t("Installed languages can be maintained on the\n <a href=':url_1'>Languages</a> page, when Language module is installed. Custom languages can\n be maintained on the <a href=':url_2'>Custom Languages</a> page. (Options marked with '*' are\n typically used as default value in a hidden widget.)", [
':url_1' => $url_1,
':url_2' => $url_2,
]),
'#required' => TRUE,
'#default_value' => $settings['language_range'],
'#options' => [
CustomLanguageManager::LANGUAGEFIELD_LANGUAGES_PREDEFINED => $this
->t('All predefined languages'),
LanguageInterface::STATE_CONFIGURABLE => $this
->t('All installed (enabled) languages (from /admin/config/regional/language)'),
CustomLanguageManager::LANGUAGEFIELD_LANGUAGES_CUSTOM => $this
->t('All custom languages (from /admin/config/regional/custom_language)'),
LanguageInterface::STATE_LOCKED => $this
->t('All locked languages'),
LanguageInterface::LANGCODE_SITE_DEFAULT => $this
->t("Site's default language (@language)", [
'@language' => \Drupal::languageManager()
->getDefaultLanguage()
->getName(),
]),
LanguageInterface::LANGCODE_NOT_SPECIFIED => $this
->t('Language neutral'),
'current_interface' => $this
->t('Current interface language') . '*',
'authors_default' => $this
->t("Author's preferred language") . '*',
],
];
$element['included_languages'] = [
'#type' => 'select',
'#title' => $this
->t('Restrict by language'),
'#default_value' => $settings['included_languages'],
'#options' => [
'' => $this
->t('- None -'),
] + $languages,
'#description' => $this
->t('If no languages are selected, this filter will not be used.'),
'#multiple' => TRUE,
'#size' => 10,
];
$element['excluded_languages'] = [
'#type' => 'select',
'#title' => $this
->t('Excluded languages'),
'#default_value' => $settings['excluded_languages'],
'#options' => [
'' => $this
->t('- None -'),
] + $languages,
'#description' => $this
->t('This removes individual languages from the list.'),
'#multiple' => TRUE,
'#size' => 10,
];
$element['groups'] = [
'#type' => 'textarea',
'#title' => $this
->t('Language groups'),
'#default_value' => $settings['groups'],
'#description' => $this
->t("Provides a simple way to group common languages. If no groups are provided, no groupings will be used. Enter in the following format:<br/><code>cn,en,ep,ru<br/>African languages|bs,br<br/>Asian languages|cn,km,fil,ja</code>"),
'#multiple' => TRUE,
'#size' => 10,
];
return $element;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
public function getConstraints() {
$constraints = [];
$max_length = $this
->getSetting('max_length');
if ($max_length) {
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints[] = $constraint_manager
->create('ComplexData', [
'value' => [
'Length' => [
'max' => $max_length,
'maxMessage' => $this
->t('%name: may not be longer than @max characters.', [
'%name' => $this
->getFieldDefinition()
->getLabel(),
'@max' => $max_length,
]),
],
],
]);
}
return $constraints;
}
public function getPossibleOptions(AccountInterface $account = NULL, $format = 'en') {
static $possible_options = [];
$field_name = $this
->getFieldDefinition()
->getName();
if (isset($possible_options[$field_name])) {
return $possible_options[$field_name];
}
$languages = \Drupal::languageManager()
->getStandardLanguageList();
$languages += CustomLanguageManager::getCustomLanguageList();
foreach ($languages as $langcode => $language_names) {
$language_name = '';
switch ($format) {
case 'en':
$language_name .= $this
->t($language_names[0]);
break;
case 'loc':
$language_name .= $language_names[1];
break;
case 'both':
$language_name .= $this
->t($language_names[0]);
if (mb_strlen($language_names[1])) {
$language_name .= ' (' . $language_names[1] . ')';
}
$language_name .= ' [' . $langcode . ']';
break;
}
$possible_options[$field_name][$langcode] = $language_name;
}
asort($possible_options[$field_name]);
return $possible_options[$field_name];
}
public function getPossibleValues(AccountInterface $account = NULL) {
$options = $this
->getPossibleOptions($account);
return array_keys($options);
}
public function getSettableValues(AccountInterface $account = NULL) {
$options = $this
->getSettableOptions($account);
return array_keys($options);
}
public function getSettableOptions(AccountInterface $account = NULL) {
static $settable_options;
$field_name = $this
->getFieldDefinition()
->getName();
if (!isset($settable_options[$field_name])) {
$settings = $this
->getFieldDefinition()
->getSettings();
$settable_options[$field_name] = CustomLanguageManager::allowedValues($settings);
}
return $settable_options[$field_name];
}
public function getNativeName() {
$value = $this->value;
switch ($value) {
case 'und':
$name = '';
break;
default:
$standard_languages = \Drupal::languageManager()
->getStandardLanguageList();
$standard_languages += CustomLanguageManager::getCustomLanguageList();
$name = $standard_languages[$value][1];
break;
}
return $name;
}
}