diff --git a/core/modules/entity_reference/entity_reference.install b/core/modules/entity_reference/entity_reference.install index 1de70ca14ec0049c43d24ebe42e4782407a763f2..c40d3ead974ee1c25727571640793a632364700a 100644 --- a/core/modules/entity_reference/entity_reference.install +++ b/core/modules/entity_reference/entity_reference.install @@ -28,22 +28,21 @@ function entity_reference_field_schema($field) { 'indexes' => array( 'target_id' => array('target_id'), ), - 'foreign keys' => array(), ); // Create a foreign key to the target entity type base type. - // @todo It's still not safe to call entity_get_info() in here. - // see http://drupal.org/node/1847582 - // $entity_type = $field['settings']['target_type']; - // $entity_info = entity_get_info($entity_type); - // - // $base_table = $entity_info['base_table']; - // $id_column = $entity_info['entity_keys']['id']; - // - // $schema['foreign keys'][$base_table] = array( - // 'table' => $base_table, - // 'columns' => array('target_id' => $id_column), - // ); + $entity_manager = Drupal::service('plugin.manager.entity'); + if (is_subclass_of($entity_manager->getControllerClass($field['settings']['target_type'], 'storage'), 'Drupal\Core\Entity\DatabaseStorageController')) { + $entity_info = $entity_manager->getDefinition($field['settings']['target_type']); + + $base_table = $entity_info['base_table']; + $id_column = $entity_info['entity_keys']['id']; + + $schema['foreign keys'][$base_table] = array( + 'table' => $base_table, + 'columns' => array('target_id' => $id_column), + ); + } return $schema; } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php index c8bed9ac505060d478997361fab19605f3561e7f..b3138ed4031250f15aed9249027f6f18142350ae 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php @@ -98,4 +98,32 @@ public function testEntityReferenceItem() { $this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->id()); $this->assertEqual($entity->field_test_taxonomy->entity->name->value, $term2->name->value); } + + /** + * Tests foreign key support. + */ + public function testEntityReferenceFieldSchema() { + $field = field_info_field('field_test_taxonomy'); + $foreign_key_column_name = 'target_id'; + + // Grab the SQL schema and verify that the 'foreign keys' are present. + $schemas = _field_sql_storage_schema($field); + $schema = $schemas[_field_sql_storage_tablename($field)]; + $this->assertEqual(count($schema['foreign keys']), 1, 'There is 1 foreign key in the schema.'); + + $foreign_key = reset($schema['foreign keys']); + $foreign_key_column = _field_sql_storage_columnname($field['field_name'], $foreign_key_column_name); + $this->assertEqual($foreign_key['table'], 'taxonomy_term_data', 'Foreign key table name preserved in the schema.'); + $this->assertEqual($foreign_key['columns'][$foreign_key_column], 'tid', 'Foreign key column name preserved in the schema.'); + + // Create a field that references a config entity type and check that no + // foreign key is present. + $field_name = 'field_test_vocabulary'; + entity_reference_create_instance('entity_test', 'entity_test', $field_name, 'Test vocabulary reference', 'taxonomy_vocabulary'); + $field = field_info_field($field_name); + + $schemas = _field_sql_storage_schema($field); + $schema = $schemas[_field_sql_storage_tablename($field)]; + $this->assertFalse(isset($schema['foreign keys']), 'There is no foreign key in the schema.'); + } }