FieldItemBase.php in Drupal 10
File
core/lib/Drupal/Core/Field/FieldItemBase.php
View source
<?php
namespace Drupal\Core\Field;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\Plugin\DataType\Map;
use Drupal\Core\TypedData\TypedDataInterface;
abstract class FieldItemBase extends Map implements FieldItemInterface {
public static function defaultStorageSettings() {
return [];
}
public static function defaultFieldSettings() {
return [];
}
public static function mainPropertyName() {
return 'value';
}
public function __construct(ComplexDataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
foreach ($this->definition
->getPropertyDefinitions() as $name => $definition) {
if ($definition
->isComputed()) {
$this->properties[$name] = \Drupal::typedDataManager()
->getPropertyInstance($this, $name);
}
}
}
public function getEntity() {
return $this
->getParent()
->getEntity();
}
public function getLangcode() {
return $this
->getParent()
->getLangcode();
}
public function getFieldDefinition() {
return $this->definition
->getFieldDefinition();
}
protected function getSettings() {
return $this
->getFieldDefinition()
->getSettings();
}
protected function getSetting($setting_name) {
return $this
->getFieldDefinition()
->getSetting($setting_name);
}
public function setValue($values, $notify = TRUE) {
if (isset($values) && !is_array($values)) {
$keys = array_keys($this->definition
->getPropertyDefinitions());
$values = [
$keys[0] => $values,
];
}
parent::setValue($values, $notify);
}
protected function writePropertyValue($property_name, $value) {
if (isset($this->properties[$property_name])) {
$this->properties[$property_name]
->setValue($value, FALSE);
}
else {
$this->values[$property_name] = $value;
}
}
public function __get($name) {
if (isset($this->properties[$name])) {
return $this->properties[$name]
->getValue();
}
elseif (isset($this->values[$name])) {
return $this->values[$name];
}
}
public function __set($name, $value) {
if ($value instanceof TypedDataInterface && !$value instanceof EntityInterface) {
$value = $value
->getValue();
}
$this
->set($name, $value);
}
public function __isset($name) {
if (isset($this->properties[$name])) {
return $this->properties[$name]
->getValue() !== NULL;
}
return isset($this->values[$name]);
}
public function __unset($name) {
if ($this->definition
->getPropertyDefinition($name)) {
$this
->set($name, NULL);
}
else {
unset($this->values[$name]);
}
}
public function view($display_options = []) {
$view_builder = \Drupal::entityTypeManager()
->getViewBuilder($this
->getEntity()
->getEntityTypeId());
return $view_builder
->viewFieldItem($this, $display_options);
}
public function preSave() {
}
public function postSave($update) {
}
public function delete() {
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
}
public function deleteRevision() {
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
return [];
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
return [];
}
public static function storageSettingsToConfigData(array $settings) {
return $settings;
}
public static function storageSettingsFromConfigData(array $settings) {
return $settings;
}
public static function fieldSettingsToConfigData(array $settings) {
return $settings;
}
public static function fieldSettingsFromConfigData(array $settings) {
return $settings;
}
public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
return [];
}
public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
return [];
}
public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {
return FALSE;
}
}