By James

2015-03-19 14:33:36 8 Comments

Let's say I have a team, which has members. I have a content type for team and a content type for individual team members. Let's say there are also other relationships, for instance teams can belong to departments, and there are projects that can be assigned to individuals or teams.

As I understand it there are two ways I can define the relationships between these entities - either Entity references, or using Taxonomy terms. When I should use one type over the other? Is it best to choose just one method, or mix them?

It seems to me that Taxonomy is the most flexible, because it is easy to build trees using taxonomy types, or for instance if within a team I then decided I wanted to build a hierarchy in the team the functionality is already there (just drag the taxonomy terms into the hierarchy) whereas if I've used Entity reference I can't think of a simple way to do that (other than adding taxonomy, which then results in redundancy).

It feels as if there is something I'm not understanding here, but I'm not sure what it is!

Any help would be appreciated.


@Zuzuesque 2015-03-19 16:27:44

You are talking about two different concepts here. The first is the question whenever one wants to organize content into different categories or if one wants to build a relationship between existing content types. The other is, if, when using a taxonomy it, it would be better to use a taxonomy reference field or a entity reference field.

On the first point:

It absolutely depends on your use case. Taxonomies as great for building hierachies as you mentioned, but ideally they shouldn't be used to contain actual content. The reason for that is easy - while you can add fields to taxonomy terms, all hierachy levels in a taxonomy use the same fields. Taking your example with members belonging to different teams, this might lead to problems if you want to store more information about a team or member than just the name. If for example you want to store information about a members firstname, lastname and biography and add this fields to the taxonomy, they would be available on team terms as well. And if you add a team description field for the teams those whould show up for team members.

Taxonomies are best used when organizing similar items in a hierachical fasion. Like tags for example:

- vegetable
    - carrot
    - potatoe
- fruit
    - apple
    - banana

Entity references are great for establishing relationships between content types. Like have a node type 'team' and a node type 'team member' each with their own fields. Or a node type 'song' which is referencing an 'album' which itself references a 'musician'. In this regard entity references are actually more flexible than taxonomies, as it allows for more complex relationships. When you use views you can also utilize these relationships. Taking your example you could create a view of all team members and using the entity reference for a relationship could display any field on the team content type along with fields from the member node.

Mixing node referenced and taxonomy fields is absolutely legit too. In your example with the teams, both team and member could be a node, referencing each other with entity reference, while department could be a taxonomy with all available departments.

On the second point

When D7 was released it shipped with a taxonomy reference field to use when referencing taxonomies. Since the release the Entity API module and consequently the entity reference module where created (and since terms and taxonomies are entites they can be referenced like any other entity). At this point the two are working very similarly and in many cases it doesn't matter which one you use. However, there are still some contrib modules providing field formatters/widgets which only work for one or the others. So it mostly depends if you need such a formatter if you should use taxonomy reference or entity reference.

As the taxonomy reference field will be replaced by the entity reference field in D8 I personally prefer to go with the entity reference field to link to taxonomies rather than the field supplied by the taxonomy module.

@James 2015-03-19 16:56:33

What a wonderful explanation! Thank you very much! Now I understand!

Related Questions

Sponsored Content

1 Answered Questions

How to display custom fields of taxonomy in field twig

1 Answered Questions

[SOLVED] Entity Query Condition for the referenced entity's type

1 Answered Questions

[SOLVED] How to create an Entity reference with taxonomy term selection?

1 Answered Questions

2 Answered Questions

Pre load entity reference fields into a node

  • 2016-04-10 10:33:52
  • chap
  • 88 View
  • 0 Score
  • 2 Answer
  • Tags:   8 entities

3 Answered Questions

[SOLVED] Hierarchy between terms from different vocabularies in Drupal 7?

  • 2013-12-04 21:50:46
  • user24216
  • 1642 View
  • 7 Score
  • 3 Answer
  • Tags:   taxonomy-terms

1 Answered Questions

Link Taxonomy with Node via Feeds, Entity Reference and Views

2 Answered Questions

[SOLVED] Using CCK or Taxonomy with Custom Fields in Drupal 7

Sponsored Content