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:
type: string
label: 'Field UUID'
field_name:
type: string
label: 'Field name'
bundle:
type: string
label: 'Bundle'

View File

@ -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'],

View File

@ -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']);
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 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 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 {
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 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
'status',
'langcode',
'field_uuid',
'field_name',
'entity_type',
'bundle',
'label',
@ -340,8 +350,6 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
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 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
// 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) {

View File

@ -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'

View File

@ -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'

View File

@ -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'

View File

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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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