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