I'm completely new to drupal and actually building a charity community site on Drupal 8.

I have a content type "PROJECT" with a field "PROJECT MEMBERS" as entity reference to "USER".

Now I would like to have a button on the display of any project node "REGISTER ME FOR THIS PROJECT".

If an authenticated user clicks on that button his name should be added as new Project Member Element in my node.

Is there any way to do this ? Thank you so much !

Cheers, Tom


There are ways. Here is one:

You will have to programmatically add value to that entity reference field. Here is how you should do it Programmatically update an entity reference field (see the multiple values case)

To make your button work you will need to add a route with arguments (one argument of the content ID you are adding a current user into) see here https://www.drupal.org/docs/8/api/routing-system/using-parameters-in-routes

And in order to add a button you can just update/override a corresponding twig template to inject HTML code of a button there referring the path of the route above with the nid as a parameter.

  • Thanks George for answering so fast!! I'll dig into that direction! :-) – sgt.sackhaar Mar 2 '18 at 21:28
  • NP. Please be aware that this is not the only way to achieve this. Moreover I'd rather do an entity reference field on User (referring to projects) instead of Node. I think this way it will be easier to present the list of participants on the project page by just embedding a view. The same approach will work will work with your way but will cause duplicated information requests from DB. One to render node and another - to render view. – George Mar 2 '18 at 21:43

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.