Issue #2143519 by yched: Allow FieldInstance yml files to refer to the Field by field name rather than by field_uuid.

8.0.x
Nathaniel Catchpole 2013-12-10 13:48:36 +00:00
parent 495d680ef8
commit f2e66e87c7
11 changed files with 45 additions and 29 deletions

View File

@ -73,6 +73,9 @@ field.instance.*.*.*:
field_uuid: field_uuid:
type: string type: string
label: 'Field UUID' label: 'Field UUID'
field_name:
type: string
label: 'Field name'
bundle: bundle:
type: string type: string
label: 'Bundle' label: 'Bundle'

View File

@ -76,6 +76,7 @@ function _update_8003_field_create_instance(array $field_config, array &$instanc
'required' => FALSE, 'required' => FALSE,
'uuid' => $uuid->generate(), 'uuid' => $uuid->generate(),
'field_uuid' => $field_config['uuid'], 'field_uuid' => $field_config['uuid'],
'field_name' => $field_config['name'],
'field_type' => $field_config['type'], 'field_type' => $field_config['type'],
'default_value' => array(), 'default_value' => array(),
'default_value_function' => '', 'default_value_function' => '',
@ -380,6 +381,7 @@ function field_update_8003() {
$field_data[$record['entity_type'] . ':' . $record['id']] = array( $field_data[$record['entity_type'] . ':' . $record['id']] = array(
'uuid' => $config['uuid'], 'uuid' => $config['uuid'],
'type' => $record['type'], 'type' => $record['type'],
'name' => $record['field_name'],
); );
} }
@ -393,6 +395,7 @@ function field_update_8003() {
'id' => $record['entity_type'] . '.' . $record['bundle'] . '.' . $record['field_name'], 'id' => $record['entity_type'] . '.' . $record['bundle'] . '.' . $record['field_name'],
'uuid' => $uuid->generate(), 'uuid' => $uuid->generate(),
'field_uuid' => $field_data[$field_data_key]['uuid'], 'field_uuid' => $field_data[$field_data_key]['uuid'],
'field_name' => $field_data[$field_data_key]['name'],
'field_type' => $field_data[$field_data_key]['type'], 'field_type' => $field_data[$field_data_key]['type'],
'entity_type' => $record['entity_type'], 'entity_type' => $record['entity_type'],
'bundle' => $record['bundle'], 'bundle' => $record['bundle'],

View File

@ -53,6 +53,13 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
*/ */
public $uuid; 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. * 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 * class; see the class property documentation for details. Some array
* elements have special meanings and a few are required; these special * elements have special meanings and a few are required; these special
* elements are: * elements are:
* - field_name: optional. The name of the field this is an instance of. * - field_name: The name of the field this is an instance of. This only
* - field_uuid: optional. Either field_uuid or field_name is required * supports non-deleted fields.
* to build field instance. field_name will gain higher priority. * - field_uuid: (optional) The uuid of the field this is an instance of.
* If field_name is not provided, field_uuid will be checked then. * If present, this has priority over the 'field_name' value.
* - entity_type: required. * - entity_type: required.
* - bundle: required. * - bundle: required.
* *
* In most cases, Field instance entities are created via * 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. * parameter as in this constructor.
* *
* @see entity_create() * @see entity_create()
@ -239,28 +246,30 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
* @ingroup field_crud * @ingroup field_crud
*/ */
public function __construct(array $values, $entity_type = 'field_instance') { public function __construct(array $values, $entity_type = 'field_instance') {
// Accept incoming 'field_name' instead of 'field_uuid', for easier DX on // Field instances configuration is stored with a 'field_uuid' property
// creation of new instances. // unambiguously identifying the field.
if (isset($values['field_name']) && isset($values['entity_type']) && !isset($values['field_uuid'])) { if (isset($values['field_uuid'])) {
$field = field_info_field($values['entity_type'], $values['field_name']);
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'])));
}
$values['field_uuid'] = $field->uuid;
}
elseif (isset($values['field_uuid'])) {
$field = field_info_field_by_id($values['field_uuid']); $field = field_info_field_by_id($values['field_uuid']);
if (!$field) { 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 unknown field @uuid', array('@uuid' => $values['field_uuid'])));
} }
$values['field_name'] = $field->getFieldName();
}
// 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 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 { else {
throw new FieldException('Attempt to create an instance of an unspecified field.'); 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 // At this point, we have a Field we can assign.
// 'field_name' property if it was provided, and assign the $field property.
unset($values['field_name']);
$this->field = $field; $this->field = $field;
// Discard the 'field_type' entry that is added in config records to ease // Discard the 'field_type' entry that is added in config records to ease
@ -300,6 +309,7 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
'status', 'status',
'langcode', 'langcode',
'field_uuid', 'field_uuid',
'field_name',
'entity_type', 'entity_type',
'bundle', 'bundle',
'label', 'label',
@ -340,8 +350,6 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
if ($prior_instance = $storage_controller->load($this->id())) { 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))); 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. // Set the default instance settings.
$this->settings += $field_type_manager->getDefaultInstanceSettings($this->field->type); $this->settings += $field_type_manager->getDefaultInstanceSettings($this->field->type);
// Notify the entity storage controller. // Notify the entity storage controller.
@ -411,9 +419,10 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
// Delete fields that have no more instances. // Delete fields that have no more instances.
$fields_to_delete = array(); $fields_to_delete = array();
foreach ($instances as $instance) { 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. // 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) { if ($fields_to_delete) {

View File

@ -2,6 +2,7 @@ id: entity_test.entity_test.field_test_import_staging
uuid: ea711065-6940-47cd-813d-618f64095481 uuid: ea711065-6940-47cd-813d-618f64095481
langcode: und langcode: und
field_uuid: 0bf654cc-f14a-4881-b94c-76959e47466b field_uuid: 0bf654cc-f14a-4881-b94c-76959e47466b
field_name: field_test_import_staging
entity_type: entity_test entity_type: entity_test
bundle: entity_test bundle: entity_test
label: 'Import from staging' label: 'Import from staging'

View File

@ -2,6 +2,7 @@ id: entity_test.test_bundle.field_test_import_staging_2
uuid: f07794a2-d7cc-45b6-b40d-13cf021b5552 uuid: f07794a2-d7cc-45b6-b40d-13cf021b5552
langcode: und langcode: und
field_uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00 field_uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00
field_name: field_test_import_staging_2
entity_type: entity_test entity_type: entity_test
bundle: test_bundle bundle: test_bundle
label: 'Test import field 2 on test bundle' label: 'Test import field 2 on test bundle'

View File

@ -2,6 +2,7 @@ id: entity_test.test_bundle_2.field_test_import_staging_2
uuid: 49d6dd19-5097-443d-8f00-fc79525bebce uuid: 49d6dd19-5097-443d-8f00-fc79525bebce
langcode: und langcode: und
field_uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00 field_uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00
field_name: field_test_import_staging_2
entity_type: entity_test entity_type: entity_test
bundle: test_bundle_2 bundle: test_bundle_2
label: 'Test import field 2 on test bundle 2' label: 'Test import field 2 on test bundle 2'

View File

@ -397,7 +397,7 @@ class ManageFieldsTest extends FieldUiTestBase {
)); ));
$field->save(); $field->save();
entity_create('field_instance', array( entity_create('field_instance', array(
'field_uuid' => $field->uuid, 'field_name' => $field->name,
'entity_type' => 'node', 'entity_type' => 'node',
'bundle' => $this->type, 'bundle' => $this->type,
))->save(); ))->save();

View File

@ -2,9 +2,9 @@ id: taxonomy_term.forums.forum_container
uuid: 8421d585-f6ef-4209-ad00-cfb30a1ab075 uuid: 8421d585-f6ef-4209-ad00-cfb30a1ab075
status: true status: true
langcode: en langcode: en
field_uuid: babf2ba1-505f-4c71-8a07-7be19f4fb9f3
entity_type: taxonomy_term entity_type: taxonomy_term
bundle: forums bundle: forums
field_name: forum_container
label: Container label: Container
description: '' description: ''
required: true required: true

View File

@ -1,9 +1,8 @@
id: node.article.field_image id: node.article.field_image
uuid: 9601b8f1-65a9-46a6-a500-d072d1232449 uuid: 9601b8f1-65a9-46a6-a500-d072d1232449
field_name: field_image
field_uuid: 748beaea-5074-4ff2-b51c-28a643d37c3a
entity_type: node entity_type: node
bundle: article bundle: article
field_name: field_image
label: Image label: Image
description: '' description: ''
required: false required: false

View File

@ -1,9 +1,8 @@
id: node.article.field_tags id: node.article.field_tags
uuid: 09fed4e4-c628-468a-9607-7dcd01f55a59 uuid: 09fed4e4-c628-468a-9607-7dcd01f55a59
field_name: field_tags
field_uuid: 60db47f4-54fb-4c86-a439-5769fbda4bd1
entity_type: node entity_type: node
bundle: article bundle: article
field_name: field_tags
label: Tags label: Tags
description: 'Enter a comma-separated list of words to describe your content.' description: 'Enter a comma-separated list of words to describe your content.'
required: false required: false

View File

@ -2,9 +2,9 @@ id: user.user.user_picture
uuid: 1e125e81-5211-4c73-a500-c45099ab9014 uuid: 1e125e81-5211-4c73-a500-c45099ab9014
status: true status: true
langcode: en langcode: en
field_uuid: 745b0ce0-aece-42dd-a800-ade5b8455e84
entity_type: user entity_type: user
bundle: user bundle: user
field_name: user_picture
label: Picture label: Picture
description: 'Your virtual face or picture.' description: 'Your virtual face or picture.'
required: false required: false