Score:0

How can I programmatically insert data (rows) to a custom entity table?

us flag

I am using Drupal 9 and I am somewhat of a newbie.

I have created a custom entity into which I wish to log the date and number of image downloads. I can use the entity's form to create entries but I need to do it programmatically, when the user triggers a download action from the site. The only thing known at that point is the user id and the number of images in the download. How do I tell my module to create an entity record, being as I am triggering it from a function. When using the form in debug mode, I can see the variables being passed through but I have no idea as to how to get that data into the database. My function currently gets the number of images but nothing else.

Do I need to create a hook and if so, how do I trigger that from the download function?

I appreciate any advice anyone can give.

us flag
Partially, @PatrickKenny. I get a record in the image_downlaods table but as I added a field to the entity through the UI (for the date), the new entity data doesn't get added to that table, so I am unable to get my downloaded date. I did that because the date column I had in the main table wasn't being brought into the view. It seems I may have a different issue there, with exposing the column to the view as a date field. I'll keep digging.
us flag
I have resolved it, using the advice given by @PatrickKenny and also realising that I could use the 'created' BaseFeidDefinition to get me a timestamp in the baseTable. This can be marked 'solved'
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.