LanguageItem.php 4.56 KB
Newer Older
1 2
<?php

3
namespace Drupal\Core\Field\Plugin\Field\FieldType;
4

5
use Drupal\Core\Field\FieldDefinitionInterface;
6
use Drupal\Core\Field\FieldStorageDefinitionInterface;
7
use Drupal\Core\Field\FieldItemBase;
8
use Drupal\Core\Language\LanguageInterface;
9
use Drupal\Core\Session\AccountInterface;
10
use Drupal\Core\TypedData\DataDefinition;
11
use Drupal\Core\TypedData\DataReferenceDefinition;
12
use Drupal\Core\TypedData\OptionsProviderInterface;
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 22
 *   default_widget = "language_select",
 *   default_formatter = "language",
23
 *   no_ui = TRUE,
24 25
 *   constraints = {
 *     "ComplexData" = {
26
 *       "value" = {
27
 *         "Length" = {"max" = 12}
28
 *       }
29 30 31
 *     }
 *   }
 * )
32
 */
33
class LanguageItem extends FieldItemBase implements OptionsProviderInterface {
34 35

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

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

    return $properties;
51 52
  }

53 54 55
  /**
   * {@inheritdoc}
   */
56
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
57 58 59
    return [
      'columns' => [
        'value' => [
60
          'type' => 'varchar_ascii',
61
          'length' => 12,
62 63 64
        ],
      ],
    ];
65 66
  }

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

  /**
   * {@inheritdoc}
   */
  public function applyDefaultValue($notify = TRUE) {
89 90
    // Default to the site's default language. When language module is enabled,
    // this behavior is configurable, see language_field_info_alter().
91
    $this->setValue(['value' => \Drupal::languageManager()->getDefaultLanguage()->getId()], $notify);
92
    return $this;
93
  }
94 95 96 97

  /**
   * {@inheritdoc}
   */
98
  public function onChange($property_name, $notify = TRUE) {
99 100
    // Make sure that the value and the language property stay in sync.
    if ($property_name == 'value') {
101
      $this->writePropertyValue('language', $this->value);
102 103
    }
    elseif ($property_name == 'language') {
104
      $this->writePropertyValue('value', $this->get('language')->getTargetIdentifier());
105
    }
106
    parent::onChange($property_name, $notify);
107
  }
108

109 110 111 112 113 114 115
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    // Defer to the callback in the item definition as it can be overridden.
    $constraint = $field_definition->getItemDefinition()->getConstraint('ComplexData');
    if (isset($constraint['value']['AllowedValues']['callback'])) {
116
      $languages = call_user_func($constraint['value']['AllowedValues']['callback']);
117 118
    }
    else {
119
      $languages = array_keys(\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL));
120 121 122 123 124
    }
    $values['value'] = $languages[array_rand($languages)];
    return $values;
  }

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  /**
   * {@inheritdoc}
   */
  public function getPossibleValues(AccountInterface $account = NULL) {
    return array_keys(\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL));
  }

  /**
   * {@inheritdoc}
   */
  public function getPossibleOptions(AccountInterface $account = NULL) {
    $languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
    return array_map(function (LanguageInterface $language) {
      return $language->getName();
    }, $languages);
  }

  /**
   * {@inheritdoc}
   */
  public function getSettableValues(AccountInterface $account = NULL) {
    return $this->getPossibleValues($account);
  }

  /**
   * {@inheritdoc}
   */
  public function getSettableOptions(AccountInterface $account = NULL) {
    return $this->getPossibleValues($account);
  }

156
}