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