From a11ee724b9af9e690cde171bab23a6f83167bfbf Mon Sep 17 00:00:00 2001 From: Jakob Perry Date: Wed, 16 Nov 2016 13:07:38 -0800 Subject: [PATCH] 1969018 --- entityreference.install | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/entityreference.install b/entityreference.install index cc7bce5..0b471e9 100644 --- a/entityreference.install +++ b/entityreference.install @@ -163,3 +163,39 @@ function entityreference_update_7002() { )); } } + +/** + * Update foreign keys where necessary. + */ +function entityreference_update_7003() { + // Load the base table configuration from the cache. + $base_tables = variable_get('entityreference:base-tables', array()); + + foreach (field_info_fields() as $field_name => $field) { + + if ($field['type'] != 'entityreference') { + // Not an entity reference field. + continue; + } + + // Read in the field config. + $field_name = field_read_field($field['field_name']); + + // Create a foreign key to the target entity type base type, if available. + $entity_type = $field_name['settings']['target_type']; + if (isset($base_tables[$entity_type])) { + list($base_table, $id_column) = $base_tables[$entity_type]; + $schema['foreign keys'][$base_table] = array( + 'table' => $base_table, + 'columns' => array('target_id' => $id_column), + ); + } + + // Update foreign keys to actually contain the proper entity foreign keys + // if they differ. + if ($field_name['foreign keys'] != $schema['foreign keys']) { + $field_name['foreign keys'] = $schema['foreign keys']; + field_update_field($field_name); + } + } +} -- 2.6.4 (Apple Git-63)