LanguageItem.php 3.18 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\FieldStorageDefinitionInterface;
11
use Drupal\Core\Field\FieldItemBase;
12
use Drupal\Core\Language\LanguageInterface;
13
use Drupal\Core\TypedData\DataDefinition;
14
use Drupal\Core\TypedData\DataReferenceDefinition;
15 16

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

  /**
34
   * {@inheritdoc}
35
   */
36
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
37 38
    $properties['value'] = DataDefinition::create('string')
      ->setLabel(t('Language code'));
39

40 41 42
    $properties['language'] = DataReferenceDefinition::create('language')
      ->setLabel(t('Language object'))
      ->setDescription(t('The referenced language'))
43
      // The language object is retrieved via the language code.
44 45 46 47
      ->setComputed(TRUE)
      ->setReadOnly(FALSE);

    return $properties;
48 49
  }

50 51 52
  /**
   * {@inheritdoc}
   */
53
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
54 55 56 57 58 59 60 61 62 63 64
    return array(
      'columns' => array(
        'value' => array(
          'type' => 'varchar',
          'length' => 12,
          'not null' => FALSE,
        ),
      ),
    );
  }

65
  /**
66
   * {@inheritdoc}
67
   */
68 69
  public function setValue($values, $notify = TRUE) {
    // Treat the values as property value of the language property, if no array
70
    // is given as this handles language codes and objects.
71
    if (isset($values) && !is_array($values)) {
72 73 74 75
      // 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);
76 77 78 79
      // If notify was FALSE, ensure the value property gets synched.
      if (!$notify) {
        $this->set('value', $this->properties['language']->getTargetIdentifier(), FALSE);
      }
80
    }
81 82 83 84 85 86
    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);
87
    }
88 89 90 91 92 93 94
  }

  /**
   * {@inheritdoc}
   */
  public function applyDefaultValue($notify = TRUE) {
    // Default to LANGCODE_NOT_SPECIFIED.
95
    $this->setValue(array('value' => LanguageInterface::LANGCODE_NOT_SPECIFIED), $notify);
96
    return $this;
97
  }
98 99 100 101 102 103 104 105 106 107 108 109 110 111

  /**
   * {@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);
  }
112
}