annotate core/lib/Drupal/Core/Field/BaseFieldDefinition.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
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@0 235 return !empty($this->definition['revisionable']);
Chris@0 236 }
Chris@0 237
Chris@0 238 /**
Chris@0 239 * Sets whether the field is revisionable.
Chris@0 240 *
Chris@0 241 * @param bool $revisionable
Chris@0 242 * Whether the field is revisionable.
Chris@0 243 *
Chris@0 244 * @return $this
Chris@0 245 * The object itself for chaining.
Chris@0 246 */
Chris@0 247 public function setRevisionable($revisionable) {
Chris@0 248 $this->definition['revisionable'] = $revisionable;
Chris@0 249 return $this;
Chris@0 250 }
Chris@0 251
Chris@0 252 /**
Chris@0 253 * {@inheritdoc}
Chris@0 254 */
Chris@0 255 public function getCardinality() {
Chris@0 256 // @todo: Allow to control this.
Chris@0 257 return isset($this->definition['cardinality']) ? $this->definition['cardinality'] : 1;
Chris@0 258 }
Chris@0 259
Chris@0 260 /**
Chris@0 261 * Sets the maximum number of items allowed for the field.
Chris@0 262 *
Chris@0 263 * Possible values are positive integers or
Chris@0 264 * FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED.
Chris@0 265 *
Chris@0 266 * @param int $cardinality
Chris@0 267 * The field cardinality.
Chris@0 268 *
Chris@0 269 * @return $this
Chris@0 270 */
Chris@0 271 public function setCardinality($cardinality) {
Chris@0 272 $this->definition['cardinality'] = $cardinality;
Chris@0 273 return $this;
Chris@0 274 }
Chris@0 275
Chris@0 276 /**
Chris@0 277 * {@inheritdoc}
Chris@0 278 */
Chris@0 279 public function isMultiple() {
Chris@0 280 $cardinality = $this->getCardinality();
Chris@0 281 return ($cardinality == static::CARDINALITY_UNLIMITED) || ($cardinality > 1);
Chris@0 282 }
Chris@0 283
Chris@0 284 /**
Chris@0 285 * {@inheritdoc}
Chris@0 286 */
Chris@0 287 public function isQueryable() {
Chris@0 288 @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 289 return !$this->hasCustomStorage();
Chris@0 290 }
Chris@0 291
Chris@0 292 /**
Chris@0 293 * Sets whether the field is queryable.
Chris@0 294 *
Chris@0 295 * @param bool $queryable
Chris@0 296 * Whether the field is queryable.
Chris@0 297 *
Chris@0 298 * @return static
Chris@0 299 * The object itself for chaining.
Chris@0 300 *
Chris@0 301 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use
Chris@0 302 * \Drupal\Core\Field\BaseFieldDefinition::setCustomStorage() instead.
Chris@0 303 *
Chris@0 304 * @see https://www.drupal.org/node/2856563
Chris@0 305 */
Chris@0 306 public function setQueryable($queryable) {
Chris@0 307 @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 308 $this->definition['queryable'] = $queryable;
Chris@0 309 return $this;
Chris@0 310 }
Chris@0 311
Chris@0 312 /**
Chris@0 313 * Sets constraints for a given field item property.
Chris@0 314 *
Chris@0 315 * Note: this overwrites any existing property constraints. If you need to
Chris@0 316 * add to the existing constraints, use
Chris@0 317 * \Drupal\Core\Field\BaseFieldDefinition::addPropertyConstraints()
Chris@0 318 *
Chris@0 319 * @param string $name
Chris@0 320 * The name of the property to set constraints for.
Chris@0 321 * @param array $constraints
Chris@0 322 * The constraints to set.
Chris@0 323 *
Chris@0 324 * @return static
Chris@0 325 * The object itself for chaining.
Chris@0 326 */
Chris@0 327 public function setPropertyConstraints($name, array $constraints) {
Chris@0 328 $item_constraints = $this->getItemDefinition()->getConstraints();
Chris@0 329 $item_constraints['ComplexData'][$name] = $constraints;
Chris@0 330 $this->getItemDefinition()->setConstraints($item_constraints);
Chris@0 331 return $this;
Chris@0 332 }
Chris@0 333
Chris@0 334 /**
Chris@0 335 * Adds constraints for a given field item property.
Chris@0 336 *
Chris@0 337 * Adds a constraint to a property of a base field item. e.g.
Chris@0 338 * @code
Chris@0 339 * // Limit the field item's value property to the range 0 through 10.
Chris@0 340 * // e.g. $node->size->value.
Chris@0 341 * $field->addPropertyConstraints('value', [
Chris@0 342 * 'Range' => [
Chris@0 343 * 'min' => 0,
Chris@0 344 * 'max' => 10,
Chris@0 345 * ]
Chris@0 346 * ]);
Chris@0 347 * @endcode
Chris@0 348 *
Chris@0 349 * If you want to add a validation constraint that applies to the
Chris@0 350 * \Drupal\Core\Field\FieldItemList, use BaseFieldDefinition::addConstraint()
Chris@0 351 * instead.
Chris@0 352 *
Chris@0 353 * Note: passing a new set of options for an existing property constraint will
Chris@0 354 * overwrite with the new options.
Chris@0 355 *
Chris@0 356 * @param string $name
Chris@0 357 * The name of the property to set constraints for.
Chris@0 358 * @param array $constraints
Chris@0 359 * The constraints to set.
Chris@0 360 *
Chris@0 361 * @return static
Chris@0 362 * The object itself for chaining.
Chris@0 363 *
Chris@0 364 * @see \Drupal\Core\Field\BaseFieldDefinition::addConstraint()
Chris@0 365 */
Chris@0 366 public function addPropertyConstraints($name, array $constraints) {
Chris@0 367 $item_constraints = $this->getItemDefinition()->getConstraint('ComplexData') ?: [];
Chris@0 368 if (isset($item_constraints[$name])) {
Chris@0 369 // Add the new property constraints, overwriting as required.
Chris@0 370 $item_constraints[$name] = $constraints + $item_constraints[$name];
Chris@0 371 }
Chris@0 372 else {
Chris@0 373 $item_constraints[$name] = $constraints;
Chris@0 374 }
Chris@0 375 $this->getItemDefinition()->addConstraint('ComplexData', $item_constraints);
Chris@0 376 return $this;
Chris@0 377 }
Chris@0 378
Chris@0 379 /**
Chris@0 380 * Sets the display options for the field in forms or rendered entities.
Chris@0 381 *
Chris@0 382 * This enables generic rendering of the field with widgets / formatters,
Chris@0 383 * including automated support for "In place editing", and with optional
Chris@0 384 * configurability in the "Manage display" / "Manage form display" UI screens.
Chris@0 385 *
Chris@0 386 * Unless this method is called, the field remains invisible (or requires
Chris@0 387 * ad-hoc rendering logic).
Chris@0 388 *
Chris@0 389 * @param string $display_context
Chris@0 390 * The display context. Either 'view' or 'form'.
Chris@0 391 * @param array $options
Chris@0 392 * An array of display options. Refer to
Chris@0 393 * \Drupal\Core\Field\FieldDefinitionInterface::getDisplayOptions() for
Chris@0 394 * a list of supported keys. The options should include at least a 'weight',
Chris@0 395 * or specify 'type' = 'hidden'. The 'default_widget' / 'default_formatter'
Chris@0 396 * for the field type will be used if no 'type' is specified.
Chris@0 397 *
Chris@0 398 * @return static
Chris@0 399 * The object itself for chaining.
Chris@0 400 */
Chris@0 401 public function setDisplayOptions($display_context, array $options) {
Chris@0 402 $this->definition['display'][$display_context]['options'] = $options;
Chris@0 403 return $this;
Chris@0 404 }
Chris@0 405
Chris@0 406 /**
Chris@0 407 * Sets whether the display for the field can be configured.
Chris@0 408 *
Chris@0 409 * @param string $display_context
Chris@0 410 * The display context. Either 'view' or 'form'.
Chris@0 411 * @param bool $configurable
Chris@0 412 * Whether the display options can be configured (e.g., via the "Manage
Chris@0 413 * display" / "Manage form display" UI screens). If TRUE, the options
Chris@0 414 * specified via getDisplayOptions() act as defaults.
Chris@0 415 *
Chris@0 416 * @return static
Chris@0 417 * The object itself for chaining.
Chris@0 418 */
Chris@0 419 public function setDisplayConfigurable($display_context, $configurable) {
Chris@0 420 // If no explicit display options have been specified, default to 'hidden'.
Chris@0 421 if (empty($this->definition['display'][$display_context])) {
Chris@0 422 $this->definition['display'][$display_context]['options'] = ['region' => 'hidden'];
Chris@0 423 }
Chris@0 424 $this->definition['display'][$display_context]['configurable'] = $configurable;
Chris@0 425 return $this;
Chris@0 426 }
Chris@0 427
Chris@0 428 /**
Chris@0 429 * {@inheritdoc}
Chris@0 430 */
Chris@0 431 public function getDisplayOptions($display_context) {
Chris@0 432 return isset($this->definition['display'][$display_context]['options']) ? $this->definition['display'][$display_context]['options'] : NULL;
Chris@0 433 }
Chris@0 434
Chris@0 435 /**
Chris@0 436 * {@inheritdoc}
Chris@0 437 */
Chris@0 438 public function isDisplayConfigurable($display_context) {
Chris@0 439 return isset($this->definition['display'][$display_context]['configurable']) ? $this->definition['display'][$display_context]['configurable'] : FALSE;
Chris@0 440 }
Chris@0 441
Chris@0 442 /**
Chris@0 443 * {@inheritdoc}
Chris@0 444 */
Chris@0 445 public function getDefaultValueLiteral() {
Chris@0 446 return isset($this->definition['default_value']) ? $this->definition['default_value'] : [];
Chris@0 447 }
Chris@0 448
Chris@0 449 /**
Chris@0 450 * {@inheritdoc}
Chris@0 451 */
Chris@0 452 public function getDefaultValueCallback() {
Chris@0 453 return isset($this->definition['default_value_callback']) ? $this->definition['default_value_callback'] : NULL;
Chris@0 454 }
Chris@0 455
Chris@0 456 /**
Chris@0 457 * {@inheritdoc}
Chris@0 458 */
Chris@0 459 public function getDefaultValue(FieldableEntityInterface $entity) {
Chris@0 460 // Allow custom default values function.
Chris@0 461 if ($callback = $this->getDefaultValueCallback()) {
Chris@0 462 $value = call_user_func($callback, $entity, $this);
Chris@0 463 }
Chris@0 464 else {
Chris@0 465 $value = $this->getDefaultValueLiteral();
Chris@0 466 }
Chris@0 467 // Normalize into the "array keyed by delta" format.
Chris@0 468 if (isset($value) && !is_array($value)) {
Chris@0 469 $properties = $this->getPropertyNames();
Chris@0 470 $property = reset($properties);
Chris@0 471 $value = [
Chris@0 472 [$property => $value],
Chris@0 473 ];
Chris@0 474 }
Chris@0 475 // Allow the field type to process default values.
Chris@0 476 $field_item_list_class = $this->getClass();
Chris@0 477 return $field_item_list_class::processDefaultValue($value, $entity, $this);
Chris@0 478 }
Chris@0 479
Chris@0 480 /**
Chris@0 481 * {@inheritdoc}
Chris@0 482 */
Chris@0 483 public function setDefaultValue($value) {
Chris@0 484 if ($value === NULL) {
Chris@0 485 $value = [];
Chris@0 486 }
Chris@0 487 // Unless the value is an empty array, we may need to transform it.
Chris@0 488 if (!is_array($value) || !empty($value)) {
Chris@0 489 if (!is_array($value)) {
Chris@0 490 $value = [[$this->getMainPropertyName() => $value]];
Chris@0 491 }
Chris@0 492 elseif (is_array($value) && !is_numeric(array_keys($value)[0])) {
Chris@0 493 $value = [0 => $value];
Chris@0 494 }
Chris@0 495 }
Chris@0 496 $this->definition['default_value'] = $value;
Chris@0 497 return $this;
Chris@0 498 }
Chris@0 499
Chris@0 500 /**
Chris@0 501 * {@inheritdoc}
Chris@0 502 */
Chris@0 503 public function setDefaultValueCallback($callback) {
Chris@0 504 if (isset($callback) && !is_string($callback)) {
Chris@0 505 throw new \InvalidArgumentException('Default value callback must be a string, like "function_name" or "ClassName::methodName"');
Chris@0 506 }
Chris@0 507 $this->definition['default_value_callback'] = $callback;
Chris@0 508 return $this;
Chris@0 509 }
Chris@0 510
Chris@0 511 /**
Chris@0 512 * Returns the initial value for the field.
Chris@0 513 *
Chris@0 514 * @return array
Chris@0 515 * The initial value for the field, as a numerically indexed array of items,
Chris@0 516 * each item being a property/value array (array() for no default value).
Chris@0 517 */
Chris@0 518 public function getInitialValue() {
Chris@0 519 $value = isset($this->definition['initial_value']) ? $this->definition['initial_value'] : [];
Chris@0 520
Chris@0 521 // Normalize into the "array keyed by delta" format.
Chris@0 522 if (isset($value) && !is_array($value)) {
Chris@0 523 $value = [
Chris@0 524 [$this->getMainPropertyName() => $value],
Chris@0 525 ];
Chris@0 526 }
Chris@0 527
Chris@0 528 return $value;
Chris@0 529 }
Chris@0 530
Chris@0 531 /**
Chris@0 532 * Sets an initial value for the field.
Chris@0 533 *
Chris@0 534 * @param mixed $value
Chris@0 535 * The initial value for the field. This can be either:
Chris@0 536 * - a literal, in which case it will be assigned to the first property of
Chris@0 537 * the first item;
Chris@0 538 * - a numerically indexed array of items, each item being a property/value
Chris@0 539 * array;
Chris@0 540 * - a non-numerically indexed array, in which case the array is assumed to
Chris@0 541 * be a property/value array and used as the first item;
Chris@0 542 * - an empty array for no initial value.
Chris@0 543 *
Chris@0 544 * @return $this
Chris@0 545 */
Chris@0 546 public function setInitialValue($value) {
Chris@0 547 // @todo Implement initial value support for multi-value fields in
Chris@0 548 // https://www.drupal.org/node/2883851.
Chris@0 549 if ($this->isMultiple()) {
Chris@0 550 throw new FieldException('Multi-value fields can not have an initial value.');
Chris@0 551 }
Chris@0 552
Chris@0 553 if ($value === NULL) {
Chris@0 554 $value = [];
Chris@0 555 }
Chris@0 556 // Unless the value is an empty array, we may need to transform it.
Chris@0 557 if (!is_array($value) || !empty($value)) {
Chris@0 558 if (!is_array($value)) {
Chris@0 559 $value = [[$this->getMainPropertyName() => $value]];
Chris@0 560 }
Chris@0 561 elseif (is_array($value) && !is_numeric(array_keys($value)[0])) {
Chris@0 562 $value = [0 => $value];
Chris@0 563 }
Chris@0 564 }
Chris@0 565 $this->definition['initial_value'] = $value;
Chris@0 566
Chris@0 567 return $this;
Chris@0 568 }
Chris@0 569
Chris@0 570 /**
Chris@0 571 * Returns the name of the field that will be used for getting initial values.
Chris@0 572 *
Chris@0 573 * @return string|null
Chris@0 574 * The field name.
Chris@0 575 */
Chris@0 576 public function getInitialValueFromField() {
Chris@0 577 return isset($this->definition['initial_value_from_field']) ? $this->definition['initial_value_from_field'] : NULL;
Chris@0 578 }
Chris@0 579
Chris@0 580 /**
Chris@0 581 * Sets a field that will be used for getting initial values.
Chris@0 582 *
Chris@0 583 * @param string $field_name
Chris@0 584 * The name of the field that will be used for getting initial values.
Chris@0 585 *
Chris@0 586 * @return $this
Chris@0 587 */
Chris@0 588 public function setInitialValueFromField($field_name) {
Chris@0 589 $this->definition['initial_value_from_field'] = $field_name;
Chris@0 590
Chris@0 591 return $this;
Chris@0 592 }
Chris@0 593
Chris@0 594 /**
Chris@0 595 * {@inheritdoc}
Chris@0 596 */
Chris@0 597 public function getOptionsProvider($property_name, FieldableEntityInterface $entity) {
Chris@0 598 // If the field item class implements the interface, create an orphaned
Chris@0 599 // runtime item object, so that it can be used as the options provider
Chris@0 600 // without modifying the entity being worked on.
Chris@14 601 if (is_subclass_of($this->getItemDefinition()->getClass(), OptionsProviderInterface::class)) {
Chris@0 602 $items = $entity->get($this->getName());
Chris@0 603 return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0);
Chris@0 604 }
Chris@0 605 // @todo: Allow setting custom options provider, see
Chris@0 606 // https://www.drupal.org/node/2002138.
Chris@0 607 }
Chris@0 608
Chris@0 609 /**
Chris@0 610 * {@inheritdoc}
Chris@0 611 */
Chris@0 612 public function getPropertyDefinition($name) {
Chris@0 613 if (!isset($this->propertyDefinitions)) {
Chris@0 614 $this->getPropertyDefinitions();
Chris@0 615 }
Chris@0 616 if (isset($this->propertyDefinitions[$name])) {
Chris@0 617 return $this->propertyDefinitions[$name];
Chris@0 618 }
Chris@0 619 }
Chris@0 620
Chris@0 621 /**
Chris@0 622 * {@inheritdoc}
Chris@0 623 */
Chris@0 624 public function getPropertyDefinitions() {
Chris@0 625 if (!isset($this->propertyDefinitions)) {
Chris@14 626 $class = $this->getItemDefinition()->getClass();
Chris@0 627 $this->propertyDefinitions = $class::propertyDefinitions($this);
Chris@0 628 }
Chris@0 629 return $this->propertyDefinitions;
Chris@0 630 }
Chris@0 631
Chris@0 632 /**
Chris@0 633 * {@inheritdoc}
Chris@0 634 */
Chris@0 635 public function getPropertyNames() {
Chris@0 636 return array_keys($this->getPropertyDefinitions());
Chris@0 637 }
Chris@0 638
Chris@0 639 /**
Chris@0 640 * {@inheritdoc}
Chris@0 641 */
Chris@0 642 public function getMainPropertyName() {
Chris@14 643 $class = $this->getItemDefinition()->getClass();
Chris@0 644 return $class::mainPropertyName();
Chris@0 645 }
Chris@0 646
Chris@0 647 /**
Chris@0 648 * Helper to retrieve the field item class.
Chris@0 649 *
Chris@14 650 * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use
Chris@14 651 * \Drupal\Core\TypedData\ListDataDefinition::getClass() instead.
Chris@0 652 */
Chris@0 653 protected function getFieldItemClass() {
Chris@14 654 @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 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@14 807 public function getUniqueIdentifier() {
Chris@14 808 // If we have a specified target bundle, we're dealing with a bundle base
Chris@14 809 // field definition, so we need to include it in the unique identifier.
Chris@14 810 if ($this->getTargetBundle()) {
Chris@14 811 return $this->getTargetEntityTypeId() . '-' . $this->getTargetBundle() . '-' . $this->getName();
Chris@14 812 }
Chris@14 813
Chris@14 814 return $this->getUniqueStorageIdentifier();
Chris@14 815 }
Chris@14 816
Chris@14 817 /**
Chris@14 818 * {@inheritdoc}
Chris@14 819 */
Chris@14 820 public function isDeleted() {
Chris@14 821 return !empty($this->definition['deleted']);
Chris@14 822 }
Chris@14 823
Chris@14 824 /**
Chris@14 825 * Sets whether the field storage is deleted.
Chris@14 826 *
Chris@14 827 * @param bool $deleted
Chris@14 828 * Whether the field storage is deleted.
Chris@14 829 *
Chris@14 830 * @return $this
Chris@14 831 */
Chris@14 832 public function setDeleted($deleted) {
Chris@14 833 $this->definition['deleted'] = $deleted;
Chris@14 834 return $this;
Chris@14 835 }
Chris@14 836
Chris@14 837 /**
Chris@14 838 * {@inheritdoc}
Chris@14 839 */
Chris@0 840 public function getConfig($bundle) {
Chris@0 841 $override = BaseFieldOverride::loadByName($this->getTargetEntityTypeId(), $bundle, $this->getName());
Chris@0 842 if ($override) {
Chris@0 843 return $override;
Chris@0 844 }
Chris@0 845 return BaseFieldOverride::createFromBaseFieldDefinition($this, $bundle);
Chris@0 846 }
Chris@0 847
Chris@0 848 /**
Chris@0 849 * {@inheritdoc}
Chris@0 850 */
Chris@0 851 public function isStorageRequired() {
Chris@0 852 if (isset($this->definition['storage_required'])) {
Chris@0 853 return (bool) $this->definition['storage_required'];
Chris@0 854 }
Chris@0 855
Chris@0 856 // Default to the 'required' property of the base field.
Chris@0 857 return $this->isRequired();
Chris@0 858 }
Chris@0 859
Chris@0 860 /**
Chris@0 861 * Sets whether the field storage is required.
Chris@0 862 *
Chris@0 863 * @param bool $required
Chris@0 864 * Whether the field storage is required.
Chris@0 865 *
Chris@0 866 * @return static
Chris@0 867 * The object itself for chaining.
Chris@0 868 */
Chris@0 869 public function setStorageRequired($required) {
Chris@0 870 $this->definition['storage_required'] = $required;
Chris@0 871 return $this;
Chris@0 872 }
Chris@0 873
Chris@14 874 /**
Chris@14 875 * Magic method: Implements a deep clone.
Chris@14 876 */
Chris@14 877 public function __clone() {
Chris@14 878 parent::__clone();
Chris@14 879
Chris@14 880 // The itemDefinition (\Drupal\Core\Field\TypedData\FieldItemDataDefinition)
Chris@14 881 // has a property fieldDefinition, which is a recursive reference to the
Chris@14 882 // parent BaseFieldDefinition, therefore the reference to the old object has
Chris@14 883 // to be overwritten with a reference to the cloned one.
Chris@14 884 $this->itemDefinition->setFieldDefinition($this);
Chris@14 885 // Reset the static cache of the field property definitions in order to
Chris@14 886 // ensure that the clone will reference different field property definitions
Chris@14 887 // objects.
Chris@14 888 $this->propertyDefinitions = NULL;
Chris@14 889 }
Chris@14 890
Chris@14 891 /**
Chris@14 892 * {@inheritdoc}
Chris@14 893 */
Chris@14 894 public function isInternal() {
Chris@14 895 // All fields are not internal unless explicitly set.
Chris@14 896 return !empty($this->definition['internal']);
Chris@14 897 }
Chris@14 898
Chris@0 899 }