diff --git a/core/modules/field/config/schema/field.schema.yml b/core/modules/field/config/schema/field.schema.yml index 8d207cc499aa76f8f53f41bdd50ababe840b318f..7c09ccea46a3d3d6818e5846999936d36d2dbf78 100644 --- a/core/modules/field/config/schema/field.schema.yml +++ b/core/modules/field/config/schema/field.schema.yml @@ -73,6 +73,9 @@ field.instance.*.*.*: field_uuid: type: string label: 'Field UUID' + field_name: + type: string + label: 'Field name' bundle: type: string label: 'Bundle' diff --git a/core/modules/field/field.install b/core/modules/field/field.install index a94e8a6828bd6245ce6bc95811f4565c7132d36a..83fc267bcaaac49e7c4cfa613386b9bd443cd325 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -76,6 +76,7 @@ function _update_8003_field_create_instance(array $field_config, array &$instanc 'required' => FALSE, 'uuid' => $uuid->generate(), 'field_uuid' => $field_config['uuid'], + 'field_name' => $field_config['name'], 'field_type' => $field_config['type'], 'default_value' => array(), 'default_value_function' => '', @@ -380,6 +381,7 @@ function field_update_8003() { $field_data[$record['entity_type'] . ':' . $record['id']] = array( 'uuid' => $config['uuid'], 'type' => $record['type'], + 'name' => $record['field_name'], ); } @@ -393,6 +395,7 @@ function field_update_8003() { 'id' => $record['entity_type'] . '.' . $record['bundle'] . '.' . $record['field_name'], 'uuid' => $uuid->generate(), 'field_uuid' => $field_data[$field_data_key]['uuid'], + 'field_name' => $field_data[$field_data_key]['name'], 'field_type' => $field_data[$field_data_key]['type'], 'entity_type' => $record['entity_type'], 'bundle' => $record['bundle'], diff --git a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php index 23c7d188a32e2d3ca63b36231c4640deabeb48aa..c29167cf2e848bac05bd0bc27e497363cb75b093 100644 --- a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php @@ -53,6 +53,13 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface { */ public $uuid; + /** + * The name of the field attached to the bundle by this instance. + * + * @var string + */ + public $field_name; + /** * The UUID of the field attached to the bundle by this instance. * @@ -223,15 +230,15 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface { * class; see the class property documentation for details. Some array * elements have special meanings and a few are required; these special * elements are: - * - field_name: optional. The name of the field this is an instance of. - * - field_uuid: optional. Either field_uuid or field_name is required - * to build field instance. field_name will gain higher priority. - * If field_name is not provided, field_uuid will be checked then. + * - field_name: The name of the field this is an instance of. This only + * supports non-deleted fields. + * - field_uuid: (optional) The uuid of the field this is an instance of. + * If present, this has priority over the 'field_name' value. * - entity_type: required. * - bundle: required. * * In most cases, Field instance entities are created via - * entity_create('field_instance', $values)), where $values is the same + * entity_create('field_instance', $values), where $values is the same * parameter as in this constructor. * * @see entity_create() @@ -239,28 +246,30 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface { * @ingroup field_crud */ public function __construct(array $values, $entity_type = 'field_instance') { - // Accept incoming 'field_name' instead of 'field_uuid', for easier DX on - // creation of new instances. - if (isset($values['field_name']) && isset($values['entity_type']) && !isset($values['field_uuid'])) { - $field = field_info_field($values['entity_type'], $values['field_name']); + // Field instances configuration is stored with a 'field_uuid' property + // unambiguously identifying the field. + if (isset($values['field_uuid'])) { + $field = field_info_field_by_id($values['field_uuid']); if (!$field) { - throw new FieldException(format_string('Attempt to create an instance of field @field_name that does not exist on entity type @entity_type.', array('@field_name' => $values['field_name'], '@entity_type' => $values['entity_type']))); + throw new FieldException(format_string('Attempt to create an instance of unknown field @uuid', array('@uuid' => $values['field_uuid']))); } - $values['field_uuid'] = $field->uuid; + $values['field_name'] = $field->getFieldName(); } - elseif (isset($values['field_uuid'])) { - $field = field_info_field_by_id($values['field_uuid']); + // Alternatively, accept incoming 'field_name' instead of 'field_uuid', for + // easier DX on creation of new instances (either through programmatic + // creation / or through import of default config files). + elseif (isset($values['field_name']) && isset($values['entity_type'])) { + $field = field_info_field($values['entity_type'], $values['field_name']); if (!$field) { - throw new FieldException(format_string('Attempt to create an instance of unknown field @uuid', array('@uuid' => $values['field_uuid']))); + throw new FieldException(format_string('Attempt to create an instance of field @field_name that does not exist on entity type @entity_type.', array('@field_name' => $values['field_name'], '@entity_type' => $values['entity_type']))); } + $values['field_uuid'] = $field->uuid(); } else { throw new FieldException('Attempt to create an instance of an unspecified field.'); } - // At this point, we should have a 'field_uuid' and a Field. Ditch the - // 'field_name' property if it was provided, and assign the $field property. - unset($values['field_name']); + // At this point, we have a Field we can assign. $this->field = $field; // Discard the 'field_type' entry that is added in config records to ease @@ -300,6 +309,7 @@ public function getExportProperties() { 'status', 'langcode', 'field_uuid', + 'field_name', 'entity_type', 'bundle', 'label', @@ -340,8 +350,6 @@ public function preSave(EntityStorageControllerInterface $storage_controller) { if ($prior_instance = $storage_controller->load($this->id())) { throw new FieldException(format_string('Attempt to create an instance of field %name on bundle @bundle that already has an instance of that field.', array('%name' => $this->field->name, '@bundle' => $this->bundle))); } - // Set the field UUID. - $this->field_uuid = $this->field->uuid; // Set the default instance settings. $this->settings += $field_type_manager->getDefaultInstanceSettings($this->field->type); // Notify the entity storage controller. @@ -411,9 +419,10 @@ public static function postDelete(EntityStorageControllerInterface $storage_cont // Delete fields that have no more instances. $fields_to_delete = array(); foreach ($instances as $instance) { - if (!$instance->deleted && empty($instance->noFieldDelete) && count($instance->field->getBundles()) == 0) { + $field = $instance->getField(); + if (!$instance->deleted && empty($instance->noFieldDelete) && count($field->getBundles()) == 0) { // Key by field UUID to avoid deleting the same field twice. - $fields_to_delete[$instance->field_uuid] = $instance->getField(); + $fields_to_delete[$instance->field_uuid] = $field; } } if ($fields_to_delete) { diff --git a/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.entity_test.field_test_import_staging.yml b/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.entity_test.field_test_import_staging.yml index d4a2a7a23ada5aaaaef41edce766b6fb1773f85c..0f96fc387476b21215fe25235de78b82b7dcd566 100644 --- a/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.entity_test.field_test_import_staging.yml +++ b/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.entity_test.field_test_import_staging.yml @@ -2,6 +2,7 @@ id: entity_test.entity_test.field_test_import_staging uuid: ea711065-6940-47cd-813d-618f64095481 langcode: und field_uuid: 0bf654cc-f14a-4881-b94c-76959e47466b +field_name: field_test_import_staging entity_type: entity_test bundle: entity_test label: 'Import from staging' diff --git a/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.test_bundle.field_test_import_staging_2.yml b/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.test_bundle.field_test_import_staging_2.yml index 37546763ffa09356313c619ec64dc2acb2e0dea9..d412b3289efaddcd0b29633fba92dd8a11baffbb 100644 --- a/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.test_bundle.field_test_import_staging_2.yml +++ b/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.test_bundle.field_test_import_staging_2.yml @@ -2,6 +2,7 @@ id: entity_test.test_bundle.field_test_import_staging_2 uuid: f07794a2-d7cc-45b6-b40d-13cf021b5552 langcode: und field_uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00 +field_name: field_test_import_staging_2 entity_type: entity_test bundle: test_bundle label: 'Test import field 2 on test bundle' diff --git a/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.test_bundle_2.field_test_import_staging_2.yml b/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.test_bundle_2.field_test_import_staging_2.yml index 0321af9f7327d17d88205250c85ab46ac8f8d4c5..675e7c09acbd6929e2cecab8accb9ff66bfd6524 100644 --- a/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.test_bundle_2.field_test_import_staging_2.yml +++ b/core/modules/field/tests/modules/field_test_config/staging/field.instance.entity_test.test_bundle_2.field_test_import_staging_2.yml @@ -2,6 +2,7 @@ id: entity_test.test_bundle_2.field_test_import_staging_2 uuid: 49d6dd19-5097-443d-8f00-fc79525bebce langcode: und field_uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00 +field_name: field_test_import_staging_2 entity_type: entity_test bundle: test_bundle_2 label: 'Test import field 2 on test bundle 2' diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php index c593b4b3bb92e52066b85a1acd405727c8f280d7..a0d6c470fadd19e4ef15a7ee97bb4e51125a5cc0 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php @@ -397,7 +397,7 @@ function testLockedField() { )); $field->save(); entity_create('field_instance', array( - 'field_uuid' => $field->uuid, + 'field_name' => $field->name, 'entity_type' => 'node', 'bundle' => $this->type, ))->save(); diff --git a/core/modules/forum/config/field.instance.taxonomy_term.forums.forum_container.yml b/core/modules/forum/config/field.instance.taxonomy_term.forums.forum_container.yml index 98fde492c476fa2ffea4012e8b1c3e35668dba63..05143d39eb2dca70fa1696c2eef6682573365b2d 100644 --- a/core/modules/forum/config/field.instance.taxonomy_term.forums.forum_container.yml +++ b/core/modules/forum/config/field.instance.taxonomy_term.forums.forum_container.yml @@ -2,9 +2,9 @@ id: taxonomy_term.forums.forum_container uuid: 8421d585-f6ef-4209-ad00-cfb30a1ab075 status: true langcode: en -field_uuid: babf2ba1-505f-4c71-8a07-7be19f4fb9f3 entity_type: taxonomy_term bundle: forums +field_name: forum_container label: Container description: '' required: true diff --git a/core/profiles/standard/config/field.instance.node.article.field_image.yml b/core/profiles/standard/config/field.instance.node.article.field_image.yml index 52445a2a917e4c42d8e38bb0d43c6363d7838934..791d047d494821f4152d17e0ce6efcdd960bf0fd 100644 --- a/core/profiles/standard/config/field.instance.node.article.field_image.yml +++ b/core/profiles/standard/config/field.instance.node.article.field_image.yml @@ -1,9 +1,8 @@ id: node.article.field_image uuid: 9601b8f1-65a9-46a6-a500-d072d1232449 -field_name: field_image -field_uuid: 748beaea-5074-4ff2-b51c-28a643d37c3a entity_type: node bundle: article +field_name: field_image label: Image description: '' required: false diff --git a/core/profiles/standard/config/field.instance.node.article.field_tags.yml b/core/profiles/standard/config/field.instance.node.article.field_tags.yml index 456a67664585e42eb14a3d7adfe3b1d57a8be70c..d0c7fbedd278d4a89f419e617234b9bb52187e58 100644 --- a/core/profiles/standard/config/field.instance.node.article.field_tags.yml +++ b/core/profiles/standard/config/field.instance.node.article.field_tags.yml @@ -1,9 +1,8 @@ id: node.article.field_tags uuid: 09fed4e4-c628-468a-9607-7dcd01f55a59 -field_name: field_tags -field_uuid: 60db47f4-54fb-4c86-a439-5769fbda4bd1 entity_type: node bundle: article +field_name: field_tags label: Tags description: 'Enter a comma-separated list of words to describe your content.' required: false diff --git a/core/profiles/standard/config/field.instance.user.user.user_picture.yml b/core/profiles/standard/config/field.instance.user.user.user_picture.yml index 2a3beec821b1dc4ebd1280599ba935adf1dc58a1..20f5a636a68b1d67b8829d8813adf034bee76318 100644 --- a/core/profiles/standard/config/field.instance.user.user.user_picture.yml +++ b/core/profiles/standard/config/field.instance.user.user.user_picture.yml @@ -2,9 +2,9 @@ id: user.user.user_picture uuid: 1e125e81-5211-4c73-a500-c45099ab9014 status: true langcode: en -field_uuid: 745b0ce0-aece-42dd-a800-ade5b8455e84 entity_type: user bundle: user +field_name: user_picture label: Picture description: 'Your virtual face or picture.' required: false