+ // Define the initial values, if any.
+ $initial_value = $initial_value_from_field = [];
+ $storage_definition_is_new = empty($this->loadFieldSchemaData($storage_definition));
+ if ($storage_definition_is_new && $storage_definition instanceof BaseFieldDefinition && $table_mapping->allowsSharedTableStorage($storage_definition)) {
+ if (($initial_storage_value = $storage_definition->getInitialValue()) && !empty($initial_storage_value)) {
+ // We only support initial values for fields that are stored in shared
+ // tables (i.e. single-value fields).
+ // @todo Implement initial value support for multi-value fields in
+ // https://www.drupal.org/node/2883851.
+ $initial_value = reset($initial_storage_value);
+ }
+
+ if ($initial_value_field_name = $storage_definition->getInitialValueFromField()) {
+ // Check that the field used for populating initial values is valid. We
+ // must use the last installed version of that, as the new field might
+ // be created in an update function and the storage definition of the
+ // "from" field might get changed later.
+ $last_installed_storage_definitions = $this->entityManager->getLastInstalledFieldStorageDefinitions($this->entityType->id());
+ if (!isset($last_installed_storage_definitions[$initial_value_field_name])) {
+ throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: The field $initial_value_field_name does not exist.");
+ }
+
+ if ($storage_definition->getType() !== $last_installed_storage_definitions[$initial_value_field_name]->getType()) {
+ throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: The field types do not match.");
+ }
+
+ if (!$table_mapping->allowsSharedTableStorage($last_installed_storage_definitions[$initial_value_field_name])) {
+ throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: Both fields have to be stored in the shared entity tables.");
+ }
+
+ $initial_value_from_field = $table_mapping->getColumnNames($initial_value_field_name);
+ }
+ }
+