Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldType / LanguageItem.php
index 1755ba63f886651df8b58e1f9a674c935aed2394..5ca1449f526316d5dbac40cd8f58fe1f55c0b3d6 100644 (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);
+  }
+
 }