Blog

How to dynamically create Extended Attributes/SubTypes

Over the last six months i've been working on numerous Mura CMS projects here at Blue River, and these projects often require me to work with extended attributes. While it's easy to create them manually, creating them programatically provides even more functionality and flexibility.

For those who are not familiar with extended attributes they allow common Mura nodes (pages, files, users, portals, etc.) to have additional fields assigned to them. This allows you to create highly structured content in Mura CMS.

Throughout the system these are referred to as subTypes (example: "Page / CustomType"). VERY nice to have whenever you might need to store more data than what Mura comes bundled with.

Now while working on a plugin project I was required to setup some extended attributes dynamically. So after I wrapped up the project I decided to push out some example code here for everyone to use.

How to build a new subType

<!--- create a new subType ---><cfset subType = application.classExtensionManager.getSubTypeBean() /><cfset subType.setType( "Portal" ) /><cfset subType.setSubType( "newPortalType" ) /><cfset subType.setBaseTable( "tcontent" ) /><cfset subType.setSiteID( "your siteId" ) /><!--- we load the subType in case it already exists ---><cfset subType.load() /><!--- save the subType ---><cfset subType.save() />

how to assign attributes to your "default" subType's extend set

<!--- get the default extend set. this is automatically created for every subType ---><cfset extendSet = subType.getExtendSetByName( "Default" ) /><!--- create a new attribute for the "default" extend set ---><!--- Note that AutoApprove does not exist yet, but getAttributeBy Name will look for it and if not found give me a new bean to use ---><cfset attribute = extendSet.getAttributeByName( "AutoApprove" ) /><cfset attribute.setLabel( "Auto Approve Topics" ) /><cfset attribute.setType( "RadioGroup" ) /><cfset attribute.setOptionList( "Yes^No" ) /><cfset attribute.setOptionLabelList( "Yes^No" ) /><cfset attribute.setDefaultValue( "Yes" ) /><!--- save the attribute ---><cfset attribute.save() />

That's it! Having the ability to manipulate extended attributes can be quite powerful, so be careful and have fun!

----------------------------------

Pat Santora
Team Blue River

Comments

Tony Garcia

VERY timely post, Pat! I was about to do some work on a project where I needed to be able to do just this! Thanks!

January 11, 2010, 12:12 PM
Reply
Flag as Spam
Pat Santora

Glad this will help you out.

January 13, 2010, 4:12 AM
Reply
Flag as Spam
David

Hi Pat,

This might seem like a really basic question, but where would this code go? Could you build an example to show us what a sub page might look like? Would it be smart to use this for something like a resume, where the sub type would store data such as job and education history? I'm just not sure i quite understand how to implement, and when best to use it.

Thanks,

David

January 16, 2010, 12:43 PM
Reply
Flag as Spam
Pat Santora

Hey David,

Very good questions. I think the best way to answer them is via another blog entry that talks about extended attributes in general and how they can be used.

I will also look to toss in a "How to setup extended attributes guide" to help you along.

With that said I am going to work on getting such a blog entry going. Look for it soon. I will also post it here for reference when done.

Thanks

-Pat

January 16, 2010, 2:55 PM
Reply
Flag as Spam
David

Sounds great! Thanks, Pat!

January 16, 2010, 3:28 PM
Reply
Flag as Spam
Pat Santora

@David

Sorry for the late post. We've just updated the Class Extension Manager documentation.

You can find it here:

http://docs.getmura.com/index.cfm/developer-guides/back-end-development/the-class-extension-manager/

You can also find answers to your questions via our newest blog post:

http://www.getmura.com/index.cfm/blog/how-to-use-extended-attributessubtype/

January 21, 2010, 10:25 AM
Reply
Flag as Spam
Steve Withington

@Pat,

Nice job on this post. I would suggest you add one more attribute when creating the subType:

subType.setBaseTable("tcontent");

If 'BaseTable' isn't set, it will default to 'NULL', so when deleting the plugin, any records (pages, portals, etc.) that have been assigned to the newly created subType won't be updated.

Obviously, this could also be avoided and not really needed if the code base would just use 'tcontent' as the default value instead of NULL.

Anyway, there's my nickel contribution.

February 18, 2010, 4:28 AM
Reply
Flag as Spam
Pat Santora

@Steve

Thanks for the contribution. The code has been updated to reflect this.

February 18, 2010, 5:57 AM
Reply
Flag as Spam
voltronad

Great post! I have one question - I already have my subType created (based on Page), and I would like to programatically create a series of pages of this sub type while I look over some data I need to import. Is there code available in the API to create a new page of a certain subType, and populate the normal things (title, etc) and the extended attributes I've set up?

July 21, 2010, 12:55 PM
Reply
Flag as Spam
David

@voltronad,

You can do that in your eventHandler.cfc, located in the root of your site/includes path. Create a new function with the corresponding new subType Page and make the name of the funtion be like this: onPageMYSUBTYPENAMEBodyRender. Then just put your variables right in there.

<cfargument name="$">

   $.content('myExtendedAttribute')

July 21, 2010, 1:46 PM
Reply
Flag as Spam
Post a Comment

Required Field