diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php index edf1530..1c7ea0e 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php @@ -10,6 +10,7 @@ use Drupal\Core\Annotation\Translation; use Drupal\Core\Entity\Annotation\FieldType; use Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem; +use Drupal\field\FieldInterface; /** * Plugin implementation of the 'entity_reference' field type. @@ -41,6 +42,47 @@ class ConfigurableEntityReferenceItem extends EntityReferenceItem { /** * {@inheritdoc} */ + public static function schema(FieldInterface $field) { + $schema = array( + 'columns' => array( + 'target_id' => array( + 'description' => 'The ID of the target entity.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'revision_id' => array( + 'description' => 'The revision ID of the target entity.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + ), + ), + 'indexes' => array( + 'target_id' => array('target_id'), + ), + ); + + // Create a foreign key to the target entity type base type. + $entity_manager = \Drupal::service('entity.manager'); + 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; + } + + /** + * {@inheritdoc} + */ public function getPropertyDefinitions() { // Definitions vary by entity type and bundle, so key them accordingly. $key = $this->definition['settings']['target_type'] . ':';