Stupid Mura Tricks - Creating Class Extensions via Code

February 22, 2017

Continuing with the “Stupid Mura Tricks” series, recently I had to create a SubType/Class Extension, give it an Extended Attribute, then create 30-ish groups of that SubType, all via code. The idea is, we’re developing the site on Dev machines, then testing on a Review server, and deploying to Production later. Rather than making someone manually create all of those things in 3 different environments, we can just run a script as part of the deployment, and have it all automated.

I'm going to break that all up into several blog posts; we’ll start with just creating the Class Extension. If you were to do this in Mura Admin you’d use this screen:

Add Class Extention drop-down screenshot

Pretty straight forward, right? Pick the Base Type and it creates your object. When I saw that, my first thought was the code would be pretty similar: make an instance of a SubTypeBean and use the setType() method to set “File”, or “Calendar” or whatever and I’d be done. It’s actually a little more involved than that.

The Base Types are all just different enough that there are more parameters that need to be set, and they all vary slightly depending on which Base Type you pick. You’ll need to set the Site ID and Type (of course), and also the Base Table, Base Key Field, and Data Table for the SubType.

We know we can get the Base Type from that drop-down in Mura Admin, but where do the other values come from? You can get those from that same drop-down by doing “Inspect Element” in your browser:

typeSelector drop-down screenshot

Notice that the drop-down isn’t actually a field called “BaseType”, but it’s called “typeSelector” and that there are several bits of data in there all delimited with a ^ character. There are 4 fields total; left to right they are the following:

Type (or Base Type)
Base Table
Base Key Field
Data Table

In my case I was creating a new User Group called “CV Groups”, so I’d use the values in the first option, like so (they all work the same way):

<cfset subtypeBean = application.classExtensionManager.getSubTypeBean() />
<cfset subtypeBean.setSiteID( “my site id” ) />
<cfset subtypeBean.setType( '1' ) />
<cfset subtypeBean.setBaseTable( 'tusers' ) />
<cfset subtypeBean.setbasekeyfield( 'userid' ) />
<cfset subtypeBean.setDataTable( 'tclassextenddatauseractivity' ) />
<cfset subtypeBean.setSubType( “UserGroupSubTypeNameGoesHere” ) />
<cfset subtypeBean.setDescription( “plain text description of the group“ ) />
<cfset subtypeBean.save() />

I’m using application.classExtensionManager instead of the Mura $ scope because I’m running this code from a one-off .CFM page, not within a Mura site directly (as this is part of a site deployment script for my project).

Also, if you want to re-run this script and keep it from accidentally creating duplicate Extensions, you can use application.classExtensionManager.getSubTypes() to filter out any previously created entries. So my final script would like about like so:

<cfset rsSubTypes = application.classExtensionManager.getSubTypes( siteID = “my site ID”, activeOnly = false ) />
<cfif not ListFindNoCase( ValueList( rsSubTypes ), “UserGroupSubTypeNameGoesHere” )>
<cfset subtypeBean = application.classExtensionManager.getSubTypeBean() />
<cfset subtypeBean.setSiteID( “my site id” ) />
<cfset subtypeBean.setType( '1' ) />
<cfset subtypeBean.setBaseTable( 'tusers' ) />
<cfset subtypeBean.setbasekeyfield( 'userid' ) />
<cfset subtypeBean.setDataTable( 'tclassextenddatauseractivity' ) />
<cfset subtypeBean.setSubType( “UserGroupSubTypeNameGoesHere” ) />
<cfset subtypeBean.setDescription( “plain text description of the group“ ) />
<cfset subtypeBean.save() />
</cfif>

Now if you go into Mura Admin / Site Settings / Class Extensions, you should see your new extension. It doesn’t have any Sets or Attribute yet, I’ll cover that in another post.

-nolan