annotate core/lib/Drupal/Core/Field/BaseFieldDefinition.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Field;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\UnchangingCacheableDependencyTrait;
Chris@0 6 use Drupal\Core\Entity\FieldableEntityInterface;
Chris@0 7 use Drupal\Core\Field\Entity\BaseFieldOverride;
Chris@0 8 use Drupal\Core\Field\TypedData\FieldItemDataDefinition;
Chris@0 9 use Drupal\Core\TypedData\ListDataDefinition;
Chris@0 10 use Drupal\Core\TypedData\OptionsProviderInterface;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * A class for defining entity fields.
Chris@0 14 */
Chris@0 15 class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionInterface, FieldStorageDefinitionInterface, RequiredFieldStorageDefinitionInterface {
Chris@0 16
Chris@0 17 use UnchangingCacheableDependencyTrait;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The field type.
Chris@0 21 *
Chris@0 22 * @var string
Chris@0 23 */
Chris@0 24 protected $type;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * An array of field property definitions.
Chris@0 28 *
Chris@0 29 * @var \Drupal\Core\TypedData\DataDefinitionInterface[]
Chris@0 30 *
Chris@0 31 * @see \Drupal\Core\TypedData\ComplexDataDefinitionInterface::getPropertyDefinitions()
Chris@0 32 */
Chris@0 33 protected $propertyDefinitions;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * The field schema.
Chris@0 37 *
Chris@0 38 * @var array
Chris@0 39 */
Chris@0 40 protected $schema;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * @var array
Chris@0 44 */
Chris@0 45 protected $indexes = [];
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Creates a new field definition.
Chris@0 49 *
Chris@0 50 * @param string $type
Chris@0 51 * The type of the field.
Chris@0 52 *
Chris@0 53 * @return static
Chris@0 54 * A new field definition object.
Chris@0 55 */
Chris@0 56 public static function create($type) {
Chris@0 57 $field_definition = new static([]);
Chris@0 58 $field_definition->type = $type;
Chris@0 59 $field_definition->itemDefinition = FieldItemDataDefinition::create($field_definition);
Chris@0 60 // Create a definition for the items, and initialize it with the default
Chris@0 61 // settings for the field type.
Chris@0 62 // @todo Cleanup in https://www.drupal.org/node/2116341.
Chris@0 63 $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
Chris@0 64 $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type);
Chris@0 65 $field_definition->itemDefinition->setSettings($default_settings);
Chris@0 66 return $field_definition;
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Creates a new field definition based upon a field storage definition.
Chris@0 71 *
Chris@0 72 * In cases where one needs a field storage definitions to act like full
Chris@0 73 * field definitions, this creates a new field definition based upon the
Chris@0 74 * (limited) information available. That way it is possible to use the field
Chris@0 75 * definition in places where a full field definition is required; e.g., with
Chris@0 76 * widgets or formatters.
Chris@0 77 *
Chris@0 78 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
Chris@0 79 * The field storage definition to base the new field definition upon.
Chris@0 80 *
Chris@0 81 * @return $this
Chris@0 82 */
Chris@0 83 public static function createFromFieldStorageDefinition(FieldStorageDefinitionInterface $definition) {
Chris@0 84 return static::create($definition->getType())
Chris@0 85 ->setCardinality($definition->getCardinality())
Chris@0 86 ->setConstraints($definition->getConstraints())
Chris@0 87 ->setCustomStorage($definition->hasCustomStorage())
Chris@0 88 ->setDescription($definition->getDescription())
Chris@0 89 ->setLabel($definition->getLabel())
Chris@0 90 ->setName($definition->getName())
Chris@0 91 ->setProvider($definition->getProvider())
Chris@0 92 ->setRevisionable($definition->isRevisionable())
Chris@0 93 ->setSettings($definition->getSettings())
Chris@0 94 ->setTargetEntityTypeId($definition->getTargetEntityTypeId())
Chris@0 95 ->setTranslatable($definition->isTranslatable());
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * {@inheritdoc}
Chris@0 100 */
Chris@0 101 public static function createFromItemType($item_type) {
Chris@0 102 // The data type of a field item is in the form of "field_item:$field_type".
Chris@0 103 $parts = explode(':', $item_type, 2);
Chris@0 104 return static::create($parts[1]);
Chris@0 105 }
Chris@0 106
Chris@0 107 /**
Chris@0 108 * {@inheritdoc}
Chris@0 109 */
Chris@0 110 public function getName() {
Chris@0 111 return $this->definition['field_name'];
Chris@0 112 }
Chris@0 113
Chris@0 114 /**
Chris@0 115 * Sets the field name.
Chris@0 116 *
Chris@0 117 * @param string $name
Chris@0 118 * The field name to set.
Chris@0 119 *
Chris@0 120 * @return static
Chris@0 121 * The object itself for chaining.
Chris@0 122 */
Chris@0 123 public function setName($name) {
Chris@0 124 $this->definition['field_name'] = $name;
Chris@0 125 return $this;
Chris@0 126 }
Chris@0 127
Chris@0 128 /**
Chris@0 129 * {@inheritdoc}
Chris@0 130 */
Chris@0 131 public function getType() {
Chris@0 132 return $this->type;
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * {@inheritdoc}
Chris@0 137 */
Chris@0 138 public function getSettings() {
Chris@0 139 return $this->getItemDefinition()->getSettings();
Chris@0 140 }
Chris@0 141
Chris@0 142 /**
Chris@0 143 * {@inheritdoc}
Chris@0 144 *
Chris@0 145 * Note that the method does not unset existing settings not specified in the
Chris@0 146 * incoming $settings array.
Chris@0 147 *
Chris@0 148 * For example:
Chris@0 149 * @code
Chris@0 150 * // Given these are the default settings.
Chris@0 151 * $field_definition->getSettings() === [
Chris@0 152 * 'fruit' => 'apple',
Chris@0 153 * 'season' => 'summer',
Chris@0 154 * ];
Chris@0 155 * // Change only the 'fruit' setting.
Chris@0 156 * $field_definition->setSettings(['fruit' => 'banana']);
Chris@0 157 * // The 'season' setting persists unchanged.
Chris@0 158 * $field_definition->getSettings() === [
Chris@0 159 * 'fruit' => 'banana',
Chris@0 160 * 'season' => 'summer',
Chris@0 161 * ];
Chris@0 162 * @endcode
Chris@0 163 *
Chris@0 164 * For clarity, it is preferred to use setSetting() if not all available
Chris@0 165 * settings are supplied.
Chris@0 166 */
Chris@0 167 public function setSettings(array $settings) {
Chris@0 168 // Assign settings individually, in order to keep the current values
Chris@0 169 // of settings not specified in $settings.
Chris@0 170 foreach ($settings as $setting_name => $setting) {
Chris@0 171 $this->getItemDefinition()->setSetting($setting_name, $setting);
Chris@0 172 }
Chris@0 173 return $this;
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * {@inheritdoc}
Chris@0 178 */
Chris@0 179 public function getSetting($setting_name) {
Chris@0 180 return $this->getItemDefinition()->getSetting($setting_name);
Chris@0 181 }
Chris@0 182
Chris@0 183 /**
Chris@0 184 * {@inheritdoc}
Chris@0 185 */
Chris@0 186 public function setSetting($setting_name, $value) {
Chris@0 187 $this->getItemDefinition()->setSetting($setting_name, $value);
Chris@0 188 return $this;
Chris@0 189 }
Chris@0 190
Chris@0 191 /**
Chris@0 192 * {@inheritdoc}
Chris@0 193 */
Chris@0 194 public function getProvider() {
Chris@0 195 return isset($this->definition['provider']) ? $this->definition['provider'] : NULL;
Chris@0 196 }
Chris@0 197
Chris@0 198 /**
Chris@0 199 * Sets the name of the provider of this field.
Chris@0 200 *
Chris@0 201 * @param string $provider
Chris@0 202 * The provider name to set.
Chris@0 203 *
Chris@0 204 * @return $this
Chris@0 205 */
Chris@0 206 public function setProvider($provider) {
Chris@0 207 $this->definition['provider'] = $provider;
Chris@0 208 return $this;
Chris@0 209 }
Chris@0 210
Chris@0 211 /**
Chris@0 212 * {@inheritdoc}
Chris@0 213 */
Chris@0 214 public function isTranslatable() {
Chris@0 215 return !empty($this->definition['translatable']);
Chris@0 216 }
Chris@0 217
Chris@0 218 /**
Chris@0 219 * Sets whether the field is translatable.
Chris@0 220 *
Chris@0 221 * @param bool $translatable
Chris@0 222 * Whether the field is translatable.
Chris@0 223 *
Chris@0 224 * @return $this
Chris@0 225 * The object itself for chaining.
Chris@0 226 */
Chris@0 227 public function setTranslatable($translatable) {
Chris@0 228 $this->definition['translatable'] = $translatable;
Chris@0 229 return $this;
Chris@0 230 }
Chris@0 231
Chris@0 232 /**
Chris@0 233 * {@inheritdoc}
Chris@0 234 */
Chris@0 235 public function isRevisionable() {
Chris@0 236 return !empty($this->definition['revisionable']);
Chris@0 237 }
Chris@0 238
Chris@0 239 /**
Chris@0 240 * Sets whether the field is revisionable.
Chris@0 241 *
Chris@0 242 * @param bool $revisionable
Chris@0 243 * Whether the field is revisionable.
Chris@0 244 *
Chris@0 245 * @return $this
Chris@0 246 * The object itself for chaining.
Chris@0 247 */
Chris@0 248 public function setRevisionable($revisionable) {
Chris@0 249 $this->definition['revisionable'] = $revisionable;
Chris@0 250 return $this;
Chris@0 251 }
Chris@0 252
Chris@0 253 /**
Chris@0 254 * {@inheritdoc}
Chris@0 255 */
Chris@0 256 public function getCardinality() {
Chris@0 257 // @todo: Allow to control this.
Chris@0 258 return isset($this->definition['cardinality']) ? $this->definition['cardinality'] : 1;
Chris@0 259 }
Chris@0 260
Chris@0 261 /**
Chris@0 262 * Sets the maximum number of items allowed for the field.
Chris@0 263 *
Chris@0 264 * Possible values are positive integers or
Chris@0 265 * FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED.
Chris@0 266 *
Chris@0 267 * @param int $cardinality
Chris@0 268 * The field cardinality.
Chris@0 269 *
Chris@0 270 * @return $this
Chris@0 271 */
Chris@0 272 public function setCardinality($cardinality) {
Chris@0 273 $this->definition['cardinality'] = $cardinality;
Chris@0 274 return $this;
Chris@0 275 }
Chris@0 276
Chris@0 277 /**
Chris@0 278 * {@inheritdoc}
Chris@0 279 */
Chris@0 280 public function isMultiple() {
Chris@0 281 $cardinality = $this->getCardinality();
Chris@0 282 return ($cardinality == static::CARDINALITY_UNLIMITED) || ($cardinality > 1);
Chris@0 283 }
Chris@0 284
Chris@0 285 /**
Chris@0 286 * {@inheritdoc}
Chris@0 287 */
Chris@0 288 public function isQueryable() {
Chris@0 289 @trigger_error('BaseFieldDefinition::isQueryable() is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\Field\BaseFieldDefinition::hasCustomStorage(). See https://www.drupal.org/node/2856563.', E_USER_DEPRECATED);
Chris@0 290 return !$this->hasCustomStorage();
Chris@0 291 }
Chris@0 292
Chris@0 293 /**
Chris@0 294 * Sets whether the field is queryable.
Chris@0 295 *
Chris@0 296 * @param bool $queryable
Chris@0 297 * Whether the field is queryable.
Chris@0 298 *
Chris@0 299 * @return static
Chris@0 300 * The object itself for chaining.
Chris@0 301 *
Chris@0 302 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use
Chris@0 303 * \Drupal\Core\Field\BaseFieldDefinition::setCustomStorage() instead.
Chris@0 304 *
Chris@0 305 * @see https://www.drupal.org/node/2856563
Chris@0 306 */
Chris@0 307 public function setQueryable($queryable) {
Chris@0 308 @trigger_error('BaseFieldDefinition::setQueryable() is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\Field\BaseFieldDefinition::setCustomStorage(). See https://www.drupal.org/node/2856563.', E_USER_DEPRECATED);
Chris@0 309 $this->definition['queryable'] = $queryable;
Chris@0 310 return $this;
Chris@0 311 }
Chris@0 312
Chris@0 313 /**
Chris@0 314 * Sets constraints for a given field item property.
Chris@0 315 *
Chris@0 316 * Note: this overwrites any existing property constraints. If you need to
Chris@0 317 * add to the existing constraints, use
Chris@0 318 * \Drupal\Core\Field\BaseFieldDefinition::addPropertyConstraints()
Chris@0 319 *
Chris@0 320 * @param string $name
Chris@0 321 * The name of the property to set constraints for.
Chris@0 322 * @param array $constraints
Chris@0 323 * The constraints to set.
Chris@0 324 *
Chris@0 325 * @return static
Chris@0 326 * The object itself for chaining.
Chris@0 327 */
Chris@0 328 public function setPropertyConstraints($name, array $constraints) {
Chris@0 329 $item_constraints = $this->getItemDefinition()->getConstraints();
Chris@0 330 $item_constraints['ComplexData'][$name] = $constraints;
Chris@0 331 $this->getItemDefinition()->setConstraints($item_constraints);
Chris@0 332 return $this;
Chris@0 333 }
Chris@0 334
Chris@0 335 /**
Chris@0 336 * Adds constraints for a given field item property.
Chris@0 337 *
Chris@0 338 * Adds a constraint to a property of a base field item. e.g.
Chris@0 339 * @code
Chris@0 340 * // Limit the field item's value property to the range 0 through 10.
Chris@0 341 * // e.g. $node->size->value.
Chris@0 342 * $field->addPropertyConstraints('value', [
Chris@0 343 * 'Range' => [
Chris@0 344 * 'min' => 0,
Chris@0 345 * 'max' => 10,
Chris@0 346 * ]
Chris@0 347 * ]);
Chris@0 348 * @endcode
Chris@0 349 *
Chris@0 350 * If you want to add a validation constraint that applies to the
Chris@0 351 * \Drupal\Core\Field\FieldItemList, use BaseFieldDefinition::addConstraint()
Chris@0 352 * instead.
Chris@0 353 *
Chris@0 354 * Note: passing a new set of options for an existing property constraint will
Chris@0 355 * overwrite with the new options.
Chris@0 356 *
Chris@0 357 * @param string $name
Chris@0 358 * The name of the property to set constraints for.
Chris@0 359 * @param array $constraints
Chris@0 360 * The constraints to set.
Chris@0 361 *
Chris@0 362 * @return static
Chris@0 363 * The object itself for chaining.
Chris@0 364 *
Chris@0 365 * @see \Drupal\Core\Field\BaseFieldDefinition::addConstraint()
Chris@0 366 */
Chris@0 367 public function addPropertyConstraints($name, array $constraints) {
Chris@0 368 $item_constraints = $this->getItemDefinition()->getConstraint('ComplexData') ?: [];
Chris@0 369 if (isset($item_constraints[$name])) {
Chris@0 370 // Add the new property constraints, overwriting as required.
Chris@0 371 $item_constraints[$name] = $constraints + $item_constraints[$name];
Chris@0 372 }
Chris@0 373 else {
Chris@0 374 $item_constraints[$name] = $constraints;
Chris@0 375 }
Chris@0 376 $this->getItemDefinition()->addConstraint('ComplexData', $item_constraints);
Chris@0 377 return $this;
Chris@0 378 }
Chris@0 379
Chris@0 380 /**
Chris@0 381 * Sets the display options for the field in forms or rendered entities.
Chris@0 382 *
Chris@0 383 * This enables generic rendering of the field with widgets / formatters,
Chris@0 384 * including automated support for "In place editing", and with optional
Chris@0 385 * configurability in the "Manage display" / "Manage form display" UI screens.
Chris@0 386 *
Chris@0 387 * Unless this method is called, the field remains invisible (or requires
Chris@0 388 * ad-hoc rendering logic).
Chris@0 389 *
Chris@0 390 * @param string $display_context
Chris@0 391 * The display context. Either 'view' or 'form'.
Chris@0 392 * @param array $options
Chris@0 393 * An array of display options. Refer to
Chris@0 394 * \Drupal\Core\Field\FieldDefinitionInterface::getDisplayOptions() for
Chris@0 395 * a list of supported keys. The options should include at least a 'weight',
Chris@0 396 * or specify 'type' = 'hidden'. The 'default_widget' / 'default_formatter'
Chris@0 397 * for the field type will be used if no 'type' is specified.
Chris@0 398 *
Chris@0 399 * @return static
Chris@0 400 * The object itself for chaining.
Chris@0 401 */
Chris@0 402 public function setDisplayOptions($display_context, array $options) {
Chris@0 403 $this->definition['display'][$display_context]['options'] = $options;
Chris@0 404 return $this;
Chris@0 405 }
Chris@0 406
Chris@0 407 /**
Chris@0 408 * Sets whether the display for the field can be configured.
Chris@0 409 *
Chris@0 410 * @param string $display_context
Chris@0 411 * The display context. Either 'view' or 'form'.
Chris@0 412 * @param bool $configurable
Chris@0 413 * Whether the display options can be configured (e.g., via the "Manage
Chris@0 414 * display" / "Manage form display" UI screens). If TRUE, the options
Chris@0 415 * specified via getDisplayOptions() act as defaults.
Chris@0 416 *
Chris@0 417 * @return static
Chris@0 418 * The object itself for chaining.
Chris@0 419 */
Chris@0 420 public function setDisplayConfigurable($display_context, $configurable) {
Chris@0 421 // If no explicit display options have been specified, default to 'hidden'.
Chris@0 422 if (empty($this->definition['display'][$display_context])) {
Chris@0 423 $this->definition['display'][$display_context]['options'] = ['region' => 'hidden'];
Chris@0 424 }
Chris@0 425 $this->definition['display'][$display_context]['configurable'] = $configurable;
Chris@0 426 return $this;
Chris@0 427 }
Chris@0 428
Chris@0 429 /**
Chris@0 430 * {@inheritdoc}
Chris@0 431 */
Chris@0 432 public function getDisplayOptions($display_context) {
Chris@0 433 return isset($this->definition['display'][$display_context]['options']) ? $this->definition['display'][$display_context]['options'] : NULL;
Chris@0 434 }
Chris@0 435
Chris@0 436 /**
Chris@0 437 * {@inheritdoc}
Chris@0 438 */
Chris@0 439 public function isDisplayConfigurable($display_context) {
Chris@0 440 return isset($this->definition['display'][$display_context]['configurable']) ? $this->definition['display'][$display_context]['configurable'] : FALSE;
Chris@0 441 }
Chris@0 442
Chris@0 443 /**
Chris@0 444 * {@inheritdoc}
Chris@0 445 */
Chris@0 446 public function getDefaultValueLiteral() {
Chris@0 447 return isset($this->definition['default_value']) ? $this->definition['default_value'] : [];
Chris@0 448 }
Chris@0 449
Chris@0 450 /**
Chris@0 451 * {@inheritdoc}
Chris@0 452 */
Chris@0 453 public function getDefaultValueCallback() {
Chris@0 454 return isset($this->definition['default_value_callback']) ? $this->definition['default_value_callback'] : NULL;
Chris@0 455 }
Chris@0 456
Chris@0 457 /**
Chris@0 458 * {@inheritdoc}
Chris@0 459 */
Chris@0 460 public function getDefaultValue(FieldableEntityInterface $entity) {
Chris@0 461 // Allow custom default values function.
Chris@0 462 if ($callback = $this->getDefaultValueCallback()) {
Chris@0 463 $value = call_user_func($callback, $entity, $this);
Chris@0 464 }
Chris@0 465 else {
Chris@0 466 $value = $this->getDefaultValueLiteral();
Chris@0 467 }
Chris@0 468 // Normalize into the "array keyed by delta" format.
Chris@0 469 if (isset($value) && !is_array($value)) {
Chris@0 470 $properties = $this->getPropertyNames();
Chris@0 471 $property = reset($properties);
Chris@0 472 $value = [
Chris@0 473 [$property => $value],
Chris@0 474 ];
Chris@0 475 }
Chris@0 476 // Allow the field type to process default values.
Chris@0 477 $field_item_list_class = $this->getClass();
Chris@0 478 return $field_item_list_class::processDefaultValue($value, $entity, $this);
Chris@0 479 }
Chris@0 480
Chris@0 481 /**
Chris@0 482 * {@inheritdoc}
Chris@0 483 */
Chris@0 484 public function setDefaultValue($value) {
Chris@0 485 if ($value === NULL) {
Chris@0 486 $value = [];
Chris@0 487 }
Chris@0 488 // Unless the value is an empty array, we may need to transform it.
Chris@0 489 if (!is_array($value) || !empty($value)) {
Chris@0 490 if (!is_array($value)) {
Chris@0 491 $value = [[$this->getMainPropertyName() => $value]];
Chris@0 492 }
Chris@0 493 elseif (is_array($value) && !is_numeric(array_keys($value)[0])) {
Chris@0 494 $value = [0 => $value];
Chris@0 495 }
Chris@0 496 }
Chris@0 497 $this->definition['default_value'] = $value;
Chris@0 498 return $this;
Chris@0 499 }
Chris@0 500
Chris@0 501 /**
Chris@0 502 * {@inheritdoc}
Chris@0 503 */
Chris@0 504 public function setDefaultValueCallback($callback) {
Chris@0 505 if (isset($callback) && !is_string($callback)) {
Chris@0 506 throw new \InvalidArgumentException('Default value callback must be a string, like "function_name" or "ClassName::methodName"');
Chris@0 507 }
Chris@0 508 $this->definition['default_value_callback'] = $callback;
Chris@0 509 return $this;
Chris@0 510 }
Chris@0 511
Chris@0 512 /**
Chris@0 513 * Returns the initial value for the field.
Chris@0 514 *
Chris@0 515 * @return array
Chris@0 516 * The initial value for the field, as a numerically indexed array of items,
Chris@0 517 * each item being a property/value array (array() for no default value).
Chris@0 518 */
Chris@0 519 public function getInitialValue() {
Chris@0 520 $value = isset($this->definition['initial_value']) ? $this->definition['initial_value'] : [];
Chris@0 521
Chris@0 522 // Normalize into the "array keyed by delta" format.
Chris@0 523 if (isset($value) && !is_array($value)) {
Chris@0 524 $value = [
Chris@0 525 [$this->getMainPropertyName() => $value],
Chris@0 526 ];
Chris@0 527 }
Chris@0 528
Chris@0 529 return $value;
Chris@0 530 }
Chris@0 531
Chris@0 532 /**
Chris@0 533 * Sets an initial value for the field.
Chris@0 534 *
Chris@0 535 * @param mixed $value
Chris@0 536 * The initial value for the field. This can be either:
Chris@0 537 * - a literal, in which case it will be assigned to the first property of
Chris@0 538 * the first item;
Chris@0 539 * - a numerically indexed array of items, each item being a property/value
Chris@0 540 * array;
Chris@0 541 * - a non-numerically indexed array, in which case the array is assumed to
Chris@0 542 * be a property/value array and used as the first item;
Chris@0 543 * - an empty array for no initial value.
Chris@0 544 *
Chris@0 545 * @return $this
Chris@0 546 */
Chris@0 547 public function setInitialValue($value) {
Chris@0 548 // @todo Implement initial value support for multi-value fields in
Chris@0 549 // https://www.drupal.org/node/2883851.
Chris@0 550 if ($this->isMultiple()) {
Chris@0 551 throw new FieldException('Multi-value fields can not have an initial value.');
Chris@0 552 }
Chris@0 553
Chris@0 554 if ($value === NULL) {
Chris@0 555 $value = [];
Chris@0 556 }
Chris@0 557 // Unless the value is an empty array, we may need to transform it.
Chris@0 558 if (!is_array($value) || !empty($value)) {
Chris@0 559 if (!is_array($value)) {
Chris@0 560 $value = [[$this->getMainPropertyName() => $value]];
Chris@0 561 }
Chris@0 562 elseif (is_array($value) && !is_numeric(array_keys($value)[0])) {
Chris@0 563 $value = [0 => $value];
Chris@0 564 }
Chris@0 565 }
Chris@0 566 $this->definition['initial_value'] = $value;
Chris@0 567
Chris@0 568 return $this;
Chris@0 569 }
Chris@0 570
Chris@0 571 /**
Chris@0 572 * Returns the name of the field that will be used for getting initial values.
Chris@0 573 *
Chris@0 574 * @return string|null
Chris@0 575 * The field name.
Chris@0 576 */
Chris@0 577 public function getInitialValueFromField() {
Chris@0 578 return isset($this->definition['initial_value_from_field']) ? $this->definition['initial_value_from_field'] : NULL;
Chris@0 579 }
Chris@0 580
Chris@0 581 /**
Chris@0 582 * Sets a field that will be used for getting initial values.
Chris@0 583 *
Chris@0 584 * @param string $field_name
Chris@0 585 * The name of the field that will be used for getting initial values.
Chris@0 586 *
Chris@0 587 * @return $this
Chris@0 588 */
Chris@0 589 public function setInitialValueFromField($field_name) {
Chris@0 590 $this->definition['initial_value_from_field'] = $field_name;
Chris@0 591
Chris@0 592 return $this;
Chris@0 593 }
Chris@0 594
Chris@0 595 /**
Chris@0 596 * {@inheritdoc}
Chris@0 597 */
Chris@0 598 public function getOptionsProvider($property_name, FieldableEntityInterface $entity) {
Chris@0 599 // If the field item class implements the interface, create an orphaned
Chris@0 600 // runtime item object, so that it can be used as the options provider
Chris@0 601 // without modifying the entity being worked on.
Chris@0 602 if (is_subclass_of($this->getFieldItemClass(), OptionsProviderInterface::class)) {
Chris@0 603 $items = $entity->get($this->getName());
Chris@0 604 return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0);
Chris@0 605 }
Chris@0 606 // @todo: Allow setting custom options provider, see
Chris@0 607 // https://www.drupal.org/node/2002138.
Chris@0 608 }
Chris@0 609
Chris@0 610 /**
Chris@0 611 * {@inheritdoc}
Chris@0 612 */
Chris@0 613 public function getPropertyDefinition($name) {
Chris@0 614 if (!isset($this->propertyDefinitions)) {
Chris@0 615 $this->getPropertyDefinitions();
Chris@0 616 }
Chris@0 617 if (isset($this->propertyDefinitions[$name])) {
Chris@0 618 return $this->propertyDefinitions[$name];
Chris@0 619 }
Chris@0 620 }
Chris@0 621
Chris@0 622 /**
Chris@0 623 * {@inheritdoc}
Chris@0 624 */
Chris@0 625 public function getPropertyDefinitions() {
Chris@0 626 if (!isset($this->propertyDefinitions)) {
Chris@0 627 $class = $this->getFieldItemClass();
Chris@0 628 $this->propertyDefinitions = $class::propertyDefinitions($this);
Chris@0 629 }
Chris@0 630 return $this->propertyDefinitions;
Chris@0 631 }
Chris@0 632
Chris@0 633 /**
Chris@0 634 * {@inheritdoc}
Chris@0 635 */
Chris@0 636 public function getPropertyNames() {
Chris@0 637 return array_keys($this->getPropertyDefinitions());
Chris@0 638 }
Chris@0 639
Chris@0 640 /**
Chris@0 641 * {@inheritdoc}
Chris@0 642 */
Chris@0 643 public function getMainPropertyName() {
Chris@0 644 $class = $this->getFieldItemClass();
Chris@0 645 return $class::mainPropertyName();
Chris@0 646 }
Chris@0 647
Chris@0 648 /**
Chris@0 649 * Helper to retrieve the field item class.
Chris@0 650 *
Chris@0 651 * @todo: Remove once getClass() adds in defaults. See
Chris@0 652 * https://www.drupal.org/node/2116341.
Chris@0 653 */
Chris@0 654 protected function getFieldItemClass() {
Chris@0 655 if ($class = $this->getItemDefinition()->getClass()) {
Chris@0 656 return $class;
Chris@0 657 }
Chris@0 658 else {
Chris@0 659 $type_definition = \Drupal::typedDataManager()
Chris@0 660 ->getDefinition($this->getItemDefinition()->getDataType());
Chris@0 661 return $type_definition['class'];
Chris@0 662 }
Chris@0 663 }
Chris@0 664
Chris@0 665 /**
Chris@0 666 * {@inheritdoc}
Chris@0 667 */
Chris@0 668 public function __sleep() {
Chris@0 669 // Do not serialize the statically cached property definitions.
Chris@0 670 $vars = get_object_vars($this);
Chris@0 671 unset($vars['propertyDefinitions'], $vars['typedDataManager']);
Chris@0 672 return array_keys($vars);
Chris@0 673 }
Chris@0 674
Chris@0 675 /**
Chris@0 676 * {@inheritdoc}
Chris@0 677 */
Chris@0 678 public function getTargetEntityTypeId() {
Chris@0 679 return isset($this->definition['entity_type']) ? $this->definition['entity_type'] : NULL;
Chris@0 680 }
Chris@0 681
Chris@0 682 /**
Chris@0 683 * Sets the ID of the type of the entity this field is attached to.
Chris@0 684 *
Chris@0 685 * @param string $entity_type_id
Chris@0 686 * The name of the target entity type to set.
Chris@0 687 *
Chris@0 688 * @return $this
Chris@0 689 */
Chris@0 690 public function setTargetEntityTypeId($entity_type_id) {
Chris@0 691 $this->definition['entity_type'] = $entity_type_id;
Chris@0 692 return $this;
Chris@0 693 }
Chris@0 694
Chris@0 695 /**
Chris@0 696 * {@inheritdoc}
Chris@0 697 */
Chris@0 698 public function getTargetBundle() {
Chris@0 699 return isset($this->definition['bundle']) ? $this->definition['bundle'] : NULL;
Chris@0 700 }
Chris@0 701
Chris@0 702 /**
Chris@0 703 * Sets the bundle this field is defined for.
Chris@0 704 *
Chris@0 705 * @param string|null $bundle
Chris@0 706 * The bundle, or NULL if the field is not bundle-specific.
Chris@0 707 *
Chris@0 708 * @return $this
Chris@0 709 */
Chris@0 710 public function setTargetBundle($bundle) {
Chris@0 711 $this->definition['bundle'] = $bundle;
Chris@0 712 return $this;
Chris@0 713 }
Chris@0 714
Chris@0 715 /**
Chris@0 716 * {@inheritdoc}
Chris@0 717 */
Chris@0 718 public function getSchema() {
Chris@0 719 if (!isset($this->schema)) {
Chris@0 720 // Get the schema from the field item class.
Chris@0 721 $definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->getType());
Chris@0 722 $class = $definition['class'];
Chris@0 723 $schema = $class::schema($this);
Chris@0 724 // Fill in default values.
Chris@0 725 $schema += [
Chris@0 726 'columns' => [],
Chris@0 727 'unique keys' => [],
Chris@0 728 'indexes' => [],
Chris@0 729 'foreign keys' => [],
Chris@0 730 ];
Chris@0 731
Chris@0 732 // Merge custom indexes with those specified by the field type. Custom
Chris@0 733 // indexes prevail.
Chris@0 734 $schema['indexes'] = $this->indexes + $schema['indexes'];
Chris@0 735
Chris@0 736 $this->schema = $schema;
Chris@0 737 }
Chris@0 738
Chris@0 739 return $this->schema;
Chris@0 740 }
Chris@0 741
Chris@0 742 /**
Chris@0 743 * {@inheritdoc}
Chris@0 744 */
Chris@0 745 public function getColumns() {
Chris@0 746 $schema = $this->getSchema();
Chris@0 747 // A typical use case for the method is to iterate on the columns, while
Chris@0 748 // some other use cases rely on identifying the first column with the key()
Chris@0 749 // function. Since the schema is persisted in the Field object, we take care
Chris@0 750 // of resetting the array pointer so that the former does not interfere with
Chris@0 751 // the latter.
Chris@0 752 reset($schema['columns']);
Chris@0 753 return $schema['columns'];
Chris@0 754 }
Chris@0 755
Chris@0 756 /**
Chris@0 757 * {@inheritdoc}
Chris@0 758 */
Chris@0 759 public function hasCustomStorage() {
Chris@0 760 return !empty($this->definition['custom_storage']) || $this->isComputed();
Chris@0 761 }
Chris@0 762
Chris@0 763 /**
Chris@0 764 * {@inheritdoc}
Chris@0 765 */
Chris@0 766 public function isBaseField() {
Chris@0 767 return TRUE;
Chris@0 768 }
Chris@0 769
Chris@0 770 /**
Chris@0 771 * Sets the storage behavior for this field.
Chris@0 772 *
Chris@0 773 * @param bool $custom_storage
Chris@0 774 * Pass FALSE if the storage takes care of storing the field,
Chris@0 775 * TRUE otherwise.
Chris@0 776 *
Chris@0 777 * @return $this
Chris@0 778 *
Chris@0 779 * @throws \LogicException
Chris@0 780 * Thrown if custom storage is to be set to FALSE for a computed field.
Chris@0 781 */
Chris@0 782 public function setCustomStorage($custom_storage) {
Chris@0 783 if (!$custom_storage && $this->isComputed()) {
Chris@0 784 throw new \LogicException("Entity storage cannot store a computed field.");
Chris@0 785 }
Chris@0 786 $this->definition['custom_storage'] = $custom_storage;
Chris@0 787 return $this;
Chris@0 788 }
Chris@0 789
Chris@0 790 /**
Chris@0 791 * {@inheritdoc}
Chris@0 792 */
Chris@0 793 public function getFieldStorageDefinition() {
Chris@0 794 return $this;
Chris@0 795 }
Chris@0 796
Chris@0 797 /**
Chris@0 798 * {@inheritdoc}
Chris@0 799 */
Chris@0 800 public function getUniqueStorageIdentifier() {
Chris@0 801 return $this->getTargetEntityTypeId() . '-' . $this->getName();
Chris@0 802 }
Chris@0 803
Chris@0 804 /**
Chris@0 805 * {@inheritdoc}
Chris@0 806 */
Chris@0 807 public function getConfig($bundle) {
Chris@0 808 $override = BaseFieldOverride::loadByName($this->getTargetEntityTypeId(), $bundle, $this->getName());
Chris@0 809 if ($override) {
Chris@0 810 return $override;
Chris@0 811 }
Chris@0 812 return BaseFieldOverride::createFromBaseFieldDefinition($this, $bundle);
Chris@0 813 }
Chris@0 814
Chris@0 815 /**
Chris@0 816 * {@inheritdoc}
Chris@0 817 */
Chris@0 818 public function isStorageRequired() {
Chris@0 819 if (isset($this->definition['storage_required'])) {
Chris@0 820 return (bool) $this->definition['storage_required'];
Chris@0 821 }
Chris@0 822
Chris@0 823 // Default to the 'required' property of the base field.
Chris@0 824 return $this->isRequired();
Chris@0 825 }
Chris@0 826
Chris@0 827 /**
Chris@0 828 * Sets whether the field storage is required.
Chris@0 829 *
Chris@0 830 * @param bool $required
Chris@0 831 * Whether the field storage is required.
Chris@0 832 *
Chris@0 833 * @return static
Chris@0 834 * The object itself for chaining.
Chris@0 835 */
Chris@0 836 public function setStorageRequired($required) {
Chris@0 837 $this->definition['storage_required'] = $required;
Chris@0 838 return $this;
Chris@0 839 }
Chris@0 840
Chris@0 841 }