LanguageItem.php 3.06 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\Plugin\Field\FieldType\LanguageItem.
6
7
 */

8
namespace Drupal\Core\Field\Plugin\Field\FieldType;
9

10
use Drupal\Core\Field\FieldItemBase;
11
use Drupal\Core\Language\Language;
12
use Drupal\Core\TypedData\DataDefinition;
13
14

/**
15
 * Defines the 'language' entity field item.
16
 *
17
18
19
 * @FieldType(
 *   id = "language",
 *   label = @Translation("Language"),
20
 *   description = @Translation("An entity field referencing a language."),
21
 *   configurable = FALSE,
22
23
24
25
26
27
 *   constraints = {
 *     "ComplexData" = {
 *       "value" = {"Length" = {"max" = 12}}
 *     }
 *   }
 * )
28
29
30
31
 */
class LanguageItem extends FieldItemBase {

  /**
32
   * Definitions of the contained properties.
33
   *
34
   * @see LanguageItem::getPropertyDefinitions()
35
36
37
38
39
40
   *
   * @var array
   */
  static $propertyDefinitions;

  /**
41
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
42
43
   */
  public function getPropertyDefinitions() {
44
    if (!isset(static::$propertyDefinitions)) {
45
46
47
48
49
50
51
52
53
      static::$propertyDefinitions['value'] = DataDefinition::create('string')
        ->setLabel(t('Language code'));

      static::$propertyDefinitions['language'] = DataDefinition::create('language_reference')
        ->setLabel(t('Language object'))
        ->setDescription(t('The referenced language'))
      // The language object is retrieved via the language code.
        ->setComputed(TRUE)
        ->setReadOnly(FALSE);
54
    }
55
    return static::$propertyDefinitions;
56
57
58
  }

  /**
59
   * {@inheritdoc}
60
   */
61
62
  public function setValue($values, $notify = TRUE) {
    // Treat the values as property value of the language property, if no array
63
    // is given as this handles language codes and objects.
64
    if (isset($values) && !is_array($values)) {
65
66
67
68
      // Directly update the property instead of invoking the parent, so that
      // the language property can take care of updating the language code
      // property.
      $this->properties['language']->setValue($values, $notify);
69
70
71
72
      // If notify was FALSE, ensure the value property gets synched.
      if (!$notify) {
        $this->set('value', $this->properties['language']->getTargetIdentifier(), FALSE);
      }
73
    }
74
75
76
77
78
79
    else {
      // Make sure that the 'language' property gets set as 'value'.
      if (isset($values['value']) && !isset($values['language'])) {
        $values['language'] = $values['value'];
      }
      parent::setValue($values, $notify);
80
    }
81
82
83
84
85
86
87
88
89
  }

  /**
   * {@inheritdoc}
   */
  public function applyDefaultValue($notify = TRUE) {
    // Default to LANGCODE_NOT_SPECIFIED.
    $this->setValue(array('value' => Language::LANGCODE_NOT_SPECIFIED), $notify);
    return $this;
90
  }
91
92
93
94
95
96
97
98
99
100
101
102
103
104

  /**
   * {@inheritdoc}
   */
  public function onChange($property_name) {
    // Make sure that the value and the language property stay in sync.
    if ($property_name == 'value') {
      $this->properties['language']->setValue($this->value, FALSE);
    }
    elseif ($property_name == 'language') {
      $this->set('value', $this->properties['language']->getTargetIdentifier(), FALSE);
    }
    parent::onChange($property_name);
  }
105
}