Score:0

Entity reference link: add reference from Entity B to Entity A

cn flag

I have created 2 Content entities. Entity A has an "Entity Reference Field" with multiple values to Entity B. This all work as expected.

What I want to achieve is, to create a link from Entity B page, that when the user clicks on it, it will be added (appended) to Entity A "Entity Reference Field".

What's the best way to get this done?

  • (I know I can use the Flag and Flag lists module, but there are limitations to it like it will be impossible/difficult to make these lists public/anonymous. After trying Flag lists out, it also didn't feel like a very Drupal native approach.)
  • (Getting a list of referenced items etc. is an other challenge, but my biggest problem is to get the linking approach working first.)
beltouche avatar
cn flag
I have some ideas, but would like more info. When you say "a link," what would that be like? Your description sounds like a checkbox, which I don't quite get.
cn flag
I mean like a real href link: <a href="Entity_A">Entity A</a>
beltouche avatar
cn flag
That blows my ideas out of the water. My inclination is to trigger an AJAX call to some custom endpoint with A & B as parameters and have the endpoint load up entity A, add the reference as necessary, and resave. Someone else here I'm sure can fill in the gaps more readily than I.
Score:0
cn flag

The direction I'm trying to take is, to add the reference with:

$node->{$target_field}[] = $id; 

But I'm struggling to figure out how to create this custom form that will be displayed on the node view.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.