Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Field/BaseFieldDefinition.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | c2387f117808 |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
13 * A class for defining entity fields. | 13 * A class for defining entity fields. |
14 */ | 14 */ |
15 class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionInterface, FieldStorageDefinitionInterface, RequiredFieldStorageDefinitionInterface { | 15 class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionInterface, FieldStorageDefinitionInterface, RequiredFieldStorageDefinitionInterface { |
16 | 16 |
17 use UnchangingCacheableDependencyTrait; | 17 use UnchangingCacheableDependencyTrait; |
18 use FieldInputValueNormalizerTrait; | |
18 | 19 |
19 /** | 20 /** |
20 * The field type. | 21 * The field type. |
21 * | 22 * |
22 * @var string | 23 * @var string |
468 $value = call_user_func($callback, $entity, $this); | 469 $value = call_user_func($callback, $entity, $this); |
469 } | 470 } |
470 else { | 471 else { |
471 $value = $this->getDefaultValueLiteral(); | 472 $value = $this->getDefaultValueLiteral(); |
472 } | 473 } |
473 // Normalize into the "array keyed by delta" format. | 474 $value = $this->normalizeValue($value, $this->getMainPropertyName()); |
474 if (isset($value) && !is_array($value)) { | |
475 $properties = $this->getPropertyNames(); | |
476 $property = reset($properties); | |
477 $value = [ | |
478 [$property => $value], | |
479 ]; | |
480 } | |
481 // Allow the field type to process default values. | 475 // Allow the field type to process default values. |
482 $field_item_list_class = $this->getClass(); | 476 $field_item_list_class = $this->getClass(); |
483 return $field_item_list_class::processDefaultValue($value, $entity, $this); | 477 return $field_item_list_class::processDefaultValue($value, $entity, $this); |
484 } | 478 } |
485 | 479 |
520 * @return array | 514 * @return array |
521 * The initial value for the field, as a numerically indexed array of items, | 515 * The initial value for the field, as a numerically indexed array of items, |
522 * each item being a property/value array (array() for no default value). | 516 * each item being a property/value array (array() for no default value). |
523 */ | 517 */ |
524 public function getInitialValue() { | 518 public function getInitialValue() { |
525 $value = isset($this->definition['initial_value']) ? $this->definition['initial_value'] : []; | 519 return $this->normalizeValue($this->definition['initial_value'], $this->getMainPropertyName()); |
526 | |
527 // Normalize into the "array keyed by delta" format. | |
528 if (isset($value) && !is_array($value)) { | |
529 $value = [ | |
530 [$this->getMainPropertyName() => $value], | |
531 ]; | |
532 } | |
533 | |
534 return $value; | |
535 } | 520 } |
536 | 521 |
537 /** | 522 /** |
538 * Sets an initial value for the field. | 523 * Sets an initial value for the field. |
539 * | 524 * |
554 // https://www.drupal.org/node/2883851. | 539 // https://www.drupal.org/node/2883851. |
555 if ($this->isMultiple()) { | 540 if ($this->isMultiple()) { |
556 throw new FieldException('Multi-value fields can not have an initial value.'); | 541 throw new FieldException('Multi-value fields can not have an initial value.'); |
557 } | 542 } |
558 | 543 |
559 if ($value === NULL) { | 544 $this->definition['initial_value'] = $this->normalizeValue($value, $this->getMainPropertyName()); |
560 $value = []; | |
561 } | |
562 // Unless the value is an empty array, we may need to transform it. | |
563 if (!is_array($value) || !empty($value)) { | |
564 if (!is_array($value)) { | |
565 $value = [[$this->getMainPropertyName() => $value]]; | |
566 } | |
567 elseif (is_array($value) && !is_numeric(array_keys($value)[0])) { | |
568 $value = [0 => $value]; | |
569 } | |
570 } | |
571 $this->definition['initial_value'] = $value; | |
572 | |
573 return $this; | 545 return $this; |
574 } | 546 } |
575 | 547 |
576 /** | 548 /** |
577 * Returns the name of the field that will be used for getting initial values. | 549 * Returns the name of the field that will be used for getting initial values. |