Say I have three content types: Team member, Team and Matchday.

The team content type is made up of entity reference fields, which reference team members.

The Matchday content type should load team members from the Team content type automatically each time a new piece of content is created. Then the content editor should be able to delete people on each Matchday who don't show up, and also add members from other teams who are filling in.

Anyone know how to do this in Drupal 8, even just a general idea to point me in the right direction?

  • You should consider using a new content entity type instead of nodes (it's the new drupal way).
    – Eyal
    Apr 10, 2016 at 10:56
  • Does match_day contains a entity reference field to the team content type?
    – Eyal
    Apr 10, 2016 at 10:58
  • I haven't created any fields for match_day yet, as I'm not sure of the best approach, but it should load a list of players that belong to a team.
    – chap
    Apr 10, 2016 at 11:07

2 Answers 2

  1. Add an entity reference field to team members content type.
  2. Using form alter:
  3. Hide the above field in the add form.
  4. Add an entity reference render element that points to the team content type.
  5. In the submit callback load all the team members that in the selected team and add them to the entity member field.
  • You can use a hook to register the team render element as a pseudo field so the position could be altered in the form display page.
    – Eyal
    Apr 10, 2016 at 11:47

Another option would be to use the presave hook on Matchday to auto fill up the list of associated team members.

hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity)

This hook runs before the entity is created or updated, so you should check if the entity is new before adding the members.

You can read the doc on this hook at https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_presave/8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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