3

For a custom Drupal 8 module I am integrating data from a separate database (other than the default drupal database) by defining a new Drupal entity in my module.

How can I configure my Entity class (or an EntityStorage class?!) to get the data of this entity from a different database? Can I change the injected (?) Database Connection object from which the entity is queried?

My question is similar to Drupal entity from different database dealing with Drupal 7. However, I think Drupal 8 has some different approaches.

Note: This other database is currently running on the same server.

5

At least if the "external" database is on the same server, there is an easy solution. The base_table of an entity can refer to a different database using . as a separator.

Just adapt the EntityType annotation in the comment above your Entity class. The following example uses the table myentity_table from the my_database database for this Entity's data:

@ContentEntityType(
 *   id = "myentity",
 *   base_table = "my_database.myentity_table",
 *   [...]
 * )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.