Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / language / src / DefaultLanguageItem.php
1 <?php
2
3 namespace Drupal\language;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Field\Plugin\Field\FieldType\LanguageItem;
7 use Drupal\Core\Language\Language;
8
9 /**
10  * Alternative plugin implementation of the 'language' field type.
11  *
12  * Replaces the Core 'language' entity field type implementation, changes the
13  * default values used.
14  *
15  * Required settings are:
16  *  - target_type: The entity type to reference.
17  *
18  * @see language_field_info_alter().
19  */
20 class DefaultLanguageItem extends LanguageItem {
21
22   /**
23    * {@inheritdoc}
24    */
25   public function applyDefaultValue($notify = TRUE) {
26     // Default to LANGCODE_NOT_SPECIFIED.
27     $langcode = Language::LANGCODE_NOT_SPECIFIED;
28     if ($entity = $this->getEntity()) {
29       $langcode = $this->getDefaultLangcode($entity);
30     }
31     // Always notify otherwise default langcode will not be set correctly.
32     $this->setValue(['value' => $langcode], TRUE);
33     return $this;
34   }
35
36   /**
37    * Provides default language code of given entity.
38    *
39    * @param \Drupal\Core\Entity\EntityInterface $entity
40    *   The entity whose language code to be loaded.
41    *
42    * @return string
43    *   A string language code.
44    */
45   public function getDefaultLangcode(EntityInterface $entity) {
46     return language_get_default_langcode($entity->getEntityTypeId(), $entity->bundle());
47   }
48
49 }