Assuming I have two distinct entities A and B, with B having an Entity Reference field to A, how can I associate other data (=other fields) with the Entity Reference?

For example, assume that A is "Political parties" and B "Politicians", and I'd like to store a date range field for the politician's membership in said party.

  • 1
    I dont want to design your data for you, but it almost sounds like you need an interim entity to store this. Maybe a few. I am not sure if fieldable relationships are a thing, yet. – Kevin Sep 3 '19 at 15:16
  • 3
    You can't, by database design an entity reference can only store two values: the target type (node, taxonomy term,...) and the numeric id of the target. You can either nest the reference inside an additional entity (e.g. a Paragraph with the ER field and your additional data), or code your own field. – Hudri Sep 3 '19 at 15:18

You can create a paragraph type (paragraph module replaces field collection in Drupal 8) which contains a reference to your entity type A and a (or more) date and in your entity type B, have a reference to this paragraph.

You can also create a custom field which contains a reference to your entity type A and a date if you want have particular behavior (for example, don't allow users to sort items and sort them automatically by date).

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.