Get current record type in onBoot

Hello,

I am building a plugin and want to run a bunch of operations in onBoot,
but only if the current user is editing a specific record type.

Something on the like of

async onBoot(ctx) {
   if (ctx.currentRecordType !== 'specificRecordType') {
     return;
   }
 
   // a bunch of operations here
   .......
}

Is this possible to do somehow? I couldn’t find anything in the documentation.
Alternatively, is there another event hook similar to onBoot where I can get that information?

Hi @matteo.tonini, welcome to the forum!

Would a Form Outlet work for your needs? That lives at the record level instead of the UI level, and can be triggered only on specific models: Form outlets — DatoCMS

Thanks @roger!

I assumed that Form Outlet was to be used when you wanted to render something at top of the page.

Is it appropriate to use itemFormOutlets to just fetch data and initialise few variables?

Example in (pseudo)code below:

let  records = [];
....
connect({
  itemFormOutlets(model: ModelBlock, ctx: IntentCtx) {
  
   if (ctx.currentRecordType !== 'specificRecordType') {
     return;
   }
  
    const client = buildClient({
      environment: ctx.environment,
      apiToken: ctx.currentUserAccessToken
    });
    // We'll be building up an array of all records using an AsyncIterator, `client.items.listPagedIterator()`
    for await (const record of client.items.listPagedIterator({
      filter: { type: "specificRecordType" },
      nested: true,
    })) {
      records.push(record);
    }
  },
  ....
 onBeforeItemUpsert(ctx) {
    //do something with records
 }

});
.....

That should be fine! You don’t necessarily have to render anything on the page to use a form outlet. From the docs:

If you want the outlet to be hidden from the interface, just return null and set an initialHeight: 0 in the itemFormOutlets hook.

1 Like

Amazing! Thanks for your help :raised_hands:

1 Like