Commit d11bd5e7 authored by catch's avatar catch

Issue #2851736 by borisson_, fago, pritish.kumar, Yogesh Pawar, amateescu,...

Issue #2851736 by borisson_, fago, pritish.kumar, Yogesh Pawar, amateescu, alexpott: Language reference fields do not implement OptionsProviderInterface
parent 9b44e407
......@@ -6,8 +6,10 @@
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 @@
* 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 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
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 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
$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);
}
}
......@@ -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.
}
......@@ -215,7 +215,7 @@ protected function assertLengthViolation(EntityInterface $entity, $field_name, $
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.'));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment