annotate core/lib/Drupal/Core/Field/BaseFieldDefinition.php @ 16:c2387f117808

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