Issue #2851736 by borisson_, fago, pritish.kumar, Yogesh Pawar, amateescu, alexpott: Language reference fields do not implement OptionsProviderInterface

merge-requests/1654/head
Nathaniel Catchpole 2018-04-20 15:48:58 +01:00
parent 9b44e4079d
commit d11bd5e7c4
3 changed files with 46 additions and 18 deletions

View File

@ -6,8 +6,10 @@ 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;
/**
* Defines the 'language' entity field item.
@ -22,17 +24,13 @@ use Drupal\Core\TypedData\DataReferenceDefinition;
* constraints = {
* "ComplexData" = {
* "value" = {
* "Length" = {"max" = 12},
* "AllowedValues" = {"callback" = "\Drupal\Core\Field\Plugin\Field\FieldType\LanguageItem::getAllowedLanguageCodes" }
* "Length" = {"max" = 12}
* }
* }
* }
* )
*
* @todo Define the AllowedValues constraint via an options provider once
* https://www.drupal.org/node/2329937 is completed.
*/
class LanguageItem extends FieldItemBase {
class LanguageItem extends FieldItemBase implements OptionsProviderInterface {
/**
* {@inheritdoc}
@ -52,16 +50,6 @@ class LanguageItem extends FieldItemBase {
return $properties;
}
/**
* Defines allowed language codes for the field's AllowedValues constraint.
*
* @return string[]
* The allowed values.
*/
public static function getAllowedLanguageCodes() {
return array_keys(\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL));
}
/**
* {@inheritdoc}
*/
@ -128,10 +116,41 @@ class LanguageItem extends FieldItemBase {
$languages = call_user_func($constraint['value']['AllowedValues']['callback']);
}
else {
$languages = static::getAllowedLanguageCodes();
$languages = array_keys(\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL));
}
$values['value'] = $languages[array_rand($languages)];
return $values;
}
/**
* {@inheritdoc}
*/
public function getPossibleValues(AccountInterface $account = NULL) {
return array_keys(\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL));
}
/**
* {@inheritdoc}
*/
public function getPossibleOptions(AccountInterface $account = NULL) {
$languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
return array_map(function (LanguageInterface $language) {
return $language->getName();
}, $languages);
}
/**
* {@inheritdoc}
*/
public function getSettableValues(AccountInterface $account = NULL) {
return $this->getPossibleValues($account);
}
/**
* {@inheritdoc}
*/
public function getSettableOptions(AccountInterface $account = NULL) {
return $this->getPossibleValues($account);
}
}

View File

@ -130,3 +130,12 @@ function system_post_update_change_delete_action_plugins() {
}
}
}
/**
* Force cache clear for language item callback.
*
* @see https://www.drupal.org/node/2851736
*/
function system_post_update_language_item_callback() {
// Empty post-update hook.
}

View File

@ -215,7 +215,7 @@ class UserValidationTest extends KernelTestBase {
protected function assertAllowedValuesViolation(EntityInterface $entity, $field_name) {
$violations = $entity->validate();
$this->assertEqual(count($violations), 1, "Allowed values violation for $field_name found.");
$this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value");
$this->assertEqual($violations[0]->getPropertyPath(), $field_name === 'langcode' ? "$field_name.0" : "$field_name.0.value");
$this->assertEqual($violations[0]->getMessage(), t('The value you selected is not a valid choice.'));
}