FieldItemBase.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Entity\EntityInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\TypedData\DataDefinitionInterface;
  6. use Drupal\Core\TypedData\Plugin\DataType\Map;
  7. use Drupal\Core\TypedData\TypedDataInterface;
  8. /**
  9. * An entity field item.
  10. *
  11. * Entity field items making use of this base class have to implement
  12. * the static method propertyDefinitions().
  13. *
  14. * @see \Drupal\Core\Field\FieldItemInterface
  15. * @ingroup field_types
  16. */
  17. abstract class FieldItemBase extends Map implements FieldItemInterface {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function defaultStorageSettings() {
  22. return [];
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function defaultFieldSettings() {
  28. return [];
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static function mainPropertyName() {
  34. return 'value';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
  40. parent::__construct($definition, $name, $parent);
  41. // Initialize computed properties by default, such that they get cloned
  42. // with the whole item.
  43. foreach ($this->definition->getPropertyDefinitions() as $name => $definition) {
  44. if ($definition->isComputed()) {
  45. $this->properties[$name] = \Drupal::typedDataManager()->getPropertyInstance($this, $name);
  46. }
  47. }
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getEntity() {
  53. return $this->getParent()->getEntity();
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getLangcode() {
  59. return $this->getParent()->getLangcode();
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getFieldDefinition() {
  65. return $this->definition->getFieldDefinition();
  66. }
  67. /**
  68. * Returns the array of field settings.
  69. *
  70. * @return array
  71. * The array of settings.
  72. */
  73. protected function getSettings() {
  74. return $this->getFieldDefinition()->getSettings();
  75. }
  76. /**
  77. * Returns the value of a field setting.
  78. *
  79. * @param string $setting_name
  80. * The setting name.
  81. *
  82. * @return mixed
  83. * The setting value.
  84. */
  85. protected function getSetting($setting_name) {
  86. return $this->getFieldDefinition()->getSetting($setting_name);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function setValue($values, $notify = TRUE) {
  92. // Treat the values as property value of the first property, if no array is
  93. // given.
  94. if (isset($values) && !is_array($values)) {
  95. $keys = array_keys($this->definition->getPropertyDefinitions());
  96. $values = [$keys[0] => $values];
  97. }
  98. parent::setValue($values, $notify);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. *
  103. * Different to the parent Map class, we avoid creating property objects as
  104. * far as possible in order to optimize performance. Thus we just update
  105. * $this->values if no property object has been created yet.
  106. */
  107. protected function writePropertyValue($property_name, $value) {
  108. // For defined properties there is either a property object or a plain
  109. // value that needs to be updated.
  110. if (isset($this->properties[$property_name])) {
  111. $this->properties[$property_name]->setValue($value, FALSE);
  112. }
  113. // Allow setting plain values for not-defined properties also.
  114. else {
  115. $this->values[$property_name] = $value;
  116. }
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function __get($name) {
  122. // There is either a property object or a plain value - possibly for a
  123. // not-defined property. If we have a plain value, directly return it.
  124. if (isset($this->properties[$name])) {
  125. return $this->properties[$name]->getValue();
  126. }
  127. elseif (isset($this->values[$name])) {
  128. return $this->values[$name];
  129. }
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function __set($name, $value) {
  135. // Support setting values via property objects, but take care in as the
  136. // value of the 'entity' property is typed data also.
  137. if ($value instanceof TypedDataInterface && !($value instanceof EntityInterface)) {
  138. $value = $value->getValue();
  139. }
  140. $this->set($name, $value);
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function __isset($name) {
  146. if (isset($this->properties[$name])) {
  147. return $this->properties[$name]->getValue() !== NULL;
  148. }
  149. return isset($this->values[$name]);
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function __unset($name) {
  155. if ($this->definition->getPropertyDefinition($name)) {
  156. $this->set($name, NULL);
  157. }
  158. else {
  159. // Explicitly unset the property in $this->values if a non-defined
  160. // property is unset, such that its key is removed from $this->values.
  161. unset($this->values[$name]);
  162. }
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function view($display_options = []) {
  168. $view_builder = \Drupal::entityManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
  169. return $view_builder->viewFieldItem($this, $display_options);
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function preSave() {}
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function postSave($update) {}
  179. /**
  180. * {@inheritdoc}
  181. */
  182. public function delete() {}
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public static function generateSampleValue(FieldDefinitionInterface $field_definition) {}
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function deleteRevision() {}
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
  195. return [];
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  201. return [];
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public static function storageSettingsToConfigData(array $settings) {
  207. return $settings;
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public static function storageSettingsFromConfigData(array $settings) {
  213. return $settings;
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public static function fieldSettingsToConfigData(array $settings) {
  219. return $settings;
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public static function fieldSettingsFromConfigData(array $settings) {
  225. return $settings;
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
  231. return [];
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
  237. return [];
  238. }
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {
  243. return FALSE;
  244. }
  245. }