Data Hook for dynamic url

I have a collection that contains a text field called eventtype . I have 4 different types of events that correspond to dynamic pages. What I would like to happen is after I create an event in the collection, the data hook will use the eventtype to copy the dynamic url from the correct column and put it into a new column called dynamicurl . For example, if the eventtype is Open Tournament, then the dynamicurl field contains the URL from Events Opens (Season, Title) . Here is a sample of the collection:

and here is what my data hook currently looks like. The editor did not like the item.link-Events-… without the quotes surrounding it. Is my syntax correct? Should this be a different type of hook? Thanks in advance!


export function Events_afterInsert(item, context) {
switch (item.eventtype)
{
case “Club Tournament”:
item.dynamicurl = “item.link-Events-Tournaments-season-title”;
return item;
case “Open Tournament”:
item.dynamicurl = “item.link-Events-Opens-season-title”;
return item;
case “Meeting”:
item.dynamicurl = “item.link-Events-Meetings-season-title”;
return item;
case “Officers Meeting”:
item.dynamicurl = “item.link-Events-Meetings-season-title”;
return item;
case “Special”:
item.dynamicurl = “item.link-Events-Special-season-title”;
return item;
default :
item.dynamicurl = “item.link-Events-Tournaments-season-title”;
return item;
}
}

Hi,

If you want the field dynamicurl will automatically be updated after entering the data to the database, use the hook function beforeUpdate( ).

export function Events_beforeUpdate(item, context) {   
        
}

The process will be:

  • Entering to your database the new Item.
  • Press on the plus button to create new item or focused on the dynamicurl field and press enter.
  • The hook function is triggered and the field dynamicurl will be updated.

Good Luck!
Sapir,

Thank you! I have updated that and I see the fields updating, however I am not sure how to reference one of the dynamic url pages. When I try to use item. link-Events-Opens-season-title, I get an error because it thinks Events and Opens and season are variables

Hi,

First, make sure that you write item. “Field key” of the field and not the “Field Name”,
Otherwise, send a URL to your site and specify:

  1. The name of the collection.
  2. In which page you first enter a new item to your collection.

Best,
Sapir

Sapir,

Yes I was using item.fieldname and it is still not working correctly.

Here is a link to my site:

The collection is named Events. I don’t have any pages I enter the data from. I just enter it directly into the collection.

Hi,

Change the hook function to be beforeUpdate()

Best,
Sapir

Thanks. The main issue I am having is that the fieldname is not being interpreted correctly. It throws an error when I use item.link-Events-Opens-season-title or any of the dynamic read only column field names

Hi,

you need to change the code to be like the first line in the example below:


The reason is that JS read the “-” as minus operator and that’s why you thought you have to put “” all over the line.

Hope it’s help,
Best,
Sapir

Sapir,

That was exactly what I needed! Now I can make a column of URLs for multiple types of events. Thank you!