LanguageItem.php in Drupal 10
File
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php
View source
<?php
namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataReferenceDefinition;
use Drupal\Core\TypedData\OptionsProviderInterface;
class LanguageItem extends FieldItemBase implements OptionsProviderInterface {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Language code'))
->setRequired(TRUE);
$properties['language'] = DataReferenceDefinition::create('language')
->setLabel(t('Language object'))
->setDescription(t('The referenced language'))
->setComputed(TRUE)
->setReadOnly(FALSE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar_ascii',
'length' => 12,
],
],
];
}
public function setValue($values, $notify = TRUE) {
if (isset($values) && !is_array($values)) {
$this
->set('language', $values, $notify);
}
else {
if (isset($values['value']) && !isset($values['language'])) {
$values['language'] = $values['value'];
}
parent::setValue($values, $notify);
}
}
public function applyDefaultValue($notify = TRUE) {
$this
->setValue([
'value' => \Drupal::languageManager()
->getDefaultLanguage()
->getId(),
], $notify);
return $this;
}
public function onChange($property_name, $notify = TRUE) {
if ($property_name == 'value') {
$this
->writePropertyValue('language', $this->value);
}
elseif ($property_name == 'language') {
$this
->writePropertyValue('value', $this
->get('language')
->getTargetIdentifier());
}
parent::onChange($property_name, $notify);
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$constraint = $field_definition
->getItemDefinition()
->getConstraint('ComplexData');
if (isset($constraint['value']['AllowedValues']['callback'])) {
$languages = call_user_func($constraint['value']['AllowedValues']['callback']);
}
else {
$languages = array_keys(\Drupal::languageManager()
->getLanguages(LanguageInterface::STATE_ALL));
}
$values['value'] = $languages[array_rand($languages)];
return $values;
}
public function getPossibleValues(AccountInterface $account = NULL) {
return array_keys(\Drupal::languageManager()
->getLanguages(LanguageInterface::STATE_ALL));
}
public function getPossibleOptions(AccountInterface $account = NULL) {
$languages = \Drupal::languageManager()
->getLanguages(LanguageInterface::STATE_ALL);
return array_map(function (LanguageInterface $language) {
return $language
->getName();
}, $languages);
}
public function getSettableValues(AccountInterface $account = NULL) {
return $this
->getPossibleValues($account);
}
public function getSettableOptions(AccountInterface $account = NULL) {
return $this
->getPossibleValues($account);
}
}