3 namespace Drupal\Core\Field;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\TypedData\DataDefinitionInterface;
8 use Drupal\Core\TypedData\Plugin\DataType\Map;
9 use Drupal\Core\TypedData\TypedDataInterface;
12 * An entity field item.
14 * Entity field items making use of this base class have to implement
15 * the static method propertyDefinitions().
17 * @see \Drupal\Core\Field\FieldItemInterface
18 * @ingroup field_types
20 abstract class FieldItemBase extends Map implements FieldItemInterface {
25 public static function defaultStorageSettings() {
32 public static function defaultFieldSettings() {
39 public static function mainPropertyName() {
46 public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
47 parent::__construct($definition, $name, $parent);
48 // Initialize computed properties by default, such that they get cloned
49 // with the whole item.
50 foreach ($this->definition->getPropertyDefinitions() as $name => $definition) {
51 if ($definition->isComputed()) {
52 $this->properties[$name] = \Drupal::typedDataManager()->getPropertyInstance($this, $name);
60 public function getEntity() {
61 return $this->getParent()->getEntity();
67 public function getLangcode() {
68 return $this->getParent()->getLangcode();
74 public function getFieldDefinition() {
75 return $this->definition->getFieldDefinition();
79 * Returns the array of field settings.
82 * The array of settings.
84 protected function getSettings() {
85 return $this->getFieldDefinition()->getSettings();
89 * Returns the value of a field setting.
91 * @param string $setting_name
97 protected function getSetting($setting_name) {
98 return $this->getFieldDefinition()->getSetting($setting_name);
104 public function setValue($values, $notify = TRUE) {
105 // Treat the values as property value of the first property, if no array is
107 if (isset($values) && !is_array($values)) {
108 $keys = array_keys($this->definition->getPropertyDefinitions());
109 $values = [$keys[0] => $values];
111 parent::setValue($values, $notify);
117 * Different to the parent Map class, we avoid creating property objects as
118 * far as possible in order to optimize performance. Thus we just update
119 * $this->values if no property object has been created yet.
121 protected function writePropertyValue($property_name, $value) {
122 // For defined properties there is either a property object or a plain
123 // value that needs to be updated.
124 if (isset($this->properties[$property_name])) {
125 $this->properties[$property_name]->setValue($value, FALSE);
127 // Allow setting plain values for not-defined properties also.
129 $this->values[$property_name] = $value;
136 public function __get($name) {
137 // There is either a property object or a plain value - possibly for a
138 // not-defined property. If we have a plain value, directly return it.
139 if (isset($this->properties[$name])) {
140 return $this->properties[$name]->getValue();
142 elseif (isset($this->values[$name])) {
143 return $this->values[$name];
150 public function __set($name, $value) {
151 // Support setting values via property objects, but take care in as the
152 // value of the 'entity' property is typed data also.
153 if ($value instanceof TypedDataInterface && !($value instanceof EntityInterface)) {
154 $value = $value->getValue();
156 $this->set($name, $value);
162 public function __isset($name) {
163 if (isset($this->properties[$name])) {
164 return $this->properties[$name]->getValue() !== NULL;
166 return isset($this->values[$name]);
172 public function __unset($name) {
173 if ($this->definition->getPropertyDefinition($name)) {
174 $this->set($name, NULL);
177 // Explicitly unset the property in $this->values if a non-defined
178 // property is unset, such that its key is removed from $this->values.
179 unset($this->values[$name]);
186 public function view($display_options = []) {
187 $view_builder = \Drupal::entityManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
188 return $view_builder->viewFieldItem($this, $display_options);
194 public function preSave() { }
199 public function postSave($update) { }
204 public function delete() { }
209 public static function generateSampleValue(FieldDefinitionInterface $field_definition) { }
214 public function deleteRevision() { }
219 public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
226 public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
233 public static function storageSettingsToConfigData(array $settings) {
240 public static function storageSettingsFromConfigData(array $settings) {
247 public static function fieldSettingsToConfigData(array $settings) {
254 public static function fieldSettingsFromConfigData(array $settings) {
261 public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
268 public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
275 public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {