How to add a custom prefix to a user input in the dataset hook?

I am creating a virtual business card where people can add their information and create a page with their social media links. I am almost done but I need help coding this:

When a user goes to the update profile page they are presented with a bunch of option for example an Instagram username text input or a Facebook username text input etc. I want to make it so when a person writes their social media username it is then send to the dataset hook where a prefix is added depending on which user text input was used. Then the link that has been created in the hook goes to the dataset where it is stored as a url.

Example: I write my username in the Instagram box - daniel_lachev and I click submit. The hook sees that and adds a prefix - (theInstagramUrldotcom/) to the username and the whole url is then send to the dataset collection - theInstagramUrldotcom/daniel_lachev.

Can someone help me with this task please?

1 Like

Hi Daniel,

I was able to find a tutorial that can point your in the right direction here . The tutorial shows you how to use data hooks to manipulate some data before it is stored in a database. In your scenario, you will have to write some code to add the URL prefix that you are trying to use to the data that is being submitted.

Hope this helps!
Edward | Corvid team

Hello @lachevd

This is probably super late but I had a similiar problem and wanted to share my solution in case anyone else in the future find it useful. For my site, I allowed users to type in their social media account address in an input box and I noticed that some people included the ‘https://’ part and some didn’t. I wanted to make sure that every url included it.

My solution:
I added a ‘before_insert’ hook to my collection that collected the user social media site input so before there input is inserted into my database, it checkes to see if the input includes ‘https://’ or not. If it does not include it, it adds it.

export function MemberInfo_beforeInsert(item, context) 
{
       //Prefix to add to social media site URLs
	const protocol = "https://"

	//Twitch Hook
       //Checks to see if 'https://' is NOT in the field /string AND checks if the field/string is NOT blank
	if (!item.twitchUrl.toLowerCase().includes("https://") && item.twitchUrl)  
	{
		item.twitchUrl = protocol + item.twitchUrl;
	}

	//Discord Hook
	if (!item.discordUrl.toLowerCase().includes("https://") && item.discordUrl )  
	{
		item.discordUrl = protocol + item.discordUrl;
	}

       return item;
}

It works like a charm.

God Bless & Good Luck brother

SkreenGG

2 Likes