Managing Content Beans

Function List

List of contentBean functions

General Overview

The Mura API uses content beans as its way to read, create and modify content nodes.

Read

There are a few different ways to read content using the Mura API.

read()

This method acts as a wrapper to forward requests to the following three methods by looking at the arguments that were submitted.  The main options that can be submitted are contentid, contenthistid, filename and remoteid.  A second siteID argument is required. There is also an optional third agrument named 'use404' that tell Mura whether to return a 404 if the content is not found.

<!--- read by filename --->
<cfset contentBean = application.contentManager.read(filename='my-content',siteid='siteid')>
<!--- read by remoteid --->
<cfset contentBean = application.contentManager.read(remoteid=myImportedID,siteid='siteid')>
<!--- read by contentid --->
<cfset contentBean = application.contentManager.read(contentid='contentid',siteid='siteid')>
<!--- read by contenthistid --->
<cfset contentBean = application.contentManager.read(contenthistid='contenthistid',siteid='siteid')>
<!--- using the optional 'use404' argument --->
<cfset contentBean = application.contentManager.read(contenthistid='contenthistid',siteid='siteid',use404=true)>
getActiveContentByFilename()

Using this method, it's possible to read content based off the filename field in tcontent.  The filename is also the same string that appears after the /index.cfm/ in the URL on the front end of the site.  For example, if I have the URL /default/index.cfm/my-content-node/, I would read that content like this:

<cfset contentBean = application.contentManager.getActiveContentByFilename('my-content-node', 'siteID')>
getActiveByRemoteID()

This method only applies when the remoteID field is set in tcontent.  This field could be set on a content import, or programatically using the API. 

<cfset contentBean = application.contentManager.getActiveByRemoteID(myImportedID, 'siteID')>
getActiveContent()

This method allows you to read content based of the Mura contentID along with the associated siteID. 

<cfset contentBean = application.contentManager.getActiveContent('contentID', 'siteID')>

Once the content bean has been read, you can now access the content node's attributes.

<cfoutput>
#contentBean.getContentID()#<br>
#contentBean.getTitle()#<br>
#contentBean.getBody()#<br>
<!--- get an extended attribute --->
#contentBean.getValue('image_caption')#
</cfoutput>

Create

To create content, first get an empty content bean.

<cfset contentBean = application.contentManager.getBean()>

Now we have to set the required attributes.  If you don't set these attributes, Mura will throw an error when you try to save the bean.

<cfset contentBean.setSiteID('default')>
<cfset contentBean.setTitle('My Created Page')>
<cfset contentBean.setParentID('1ACF7E11-5ACA-4715-8D743C49298107E7')>
<cfset contentBean.setType('Page')>

If you want to set an extended attribute, use the setValue() method.

<cfset contentBean.setValue('image_caption', 'My Image Caption - Extended Attribute')>

Now save the bean.

<cfset contentBean.save()>

Update

To update content, use a similar method as creating content.  First, read in a content bean by passing in the contentID along with the siteID.

<cfset contentBean = application.contentManager.getActiveContent('1ACF7E11-5ACA-4715-8D743C49298107E7', 'default')>

The content bean has now been populated with data from the DB.  Now we can modify the attributes that need to change and then save the content node.

<cfset contentBean.setTitle('My New Title')>
<cfset contentBean.setBody('Body Content')>

Set an extended attribute.

<cfset contentBean.setValue('image_caption', 'My Image Caption - Extended Attribute')>

Save the bean.

<cfset contentBean.save()>

Delete

To delete a content node, first load the content bean.

<cfset contentBean = application.contentManager.getActiveContent('1ACF7E11-5ACA-4715-8D743C49298107E7', 'default')>

Then delete.

<cfset contentBean.delete()>