Photo from Chile

Extending Mura CMS with Plugins - Part IV - Using Extended Attributes

In the previous post in this series about Extending Mura CMS (an open source ColdFusion CMS) with Plugins, we looked at using plugin settings to make our plugin configurable by a Mura administrator. In that simple example, our plugin settings were used directly in our ouput. Although plugin settings can be useful in a number of scenarios, using them in that manner has a limitation, which is that you'll get the exact same output on each and every page that uses the plugin, because the value of the setting is set via the Mura Admin and hence the plugin always uses that single value.

In this post we'll look at a different technique for making a plugin configurable, and this time we'll be able to configure the plugin on a page-by-page basis, instead of having one global configuration setting. We'll do this by assigning a value to an extended attribute of a page, and then direct our plugin to use that value. The first step, therefore, is to enable an extended attribute for a page, so we'll look at that first.

When editing a page in the Mura admin, there's a tab labelled Extended Attributes. When you click on that tab for a standard page, you'll see a message stating: There are currently no extended attributes available. In order to provide extended attributes for a page you need to use the Class Extension Manager to create a Page Sub Type that extends the built-in Page class, and then add attributes to that new Sub Type. Here is a step-by-step guide:

  1. Access the Class Extension Manager in order to create a Page Sub Type:
    1. In the upper right-hand corner of the Mura Admin, point to Site Settings and choose Edit Current Site.
    2. Click on the Class Extension Manager link immediately under the Site Settings header.
  2. Add a Page Sub Type which can then be assigned to a Page:
    1. Click on the Add Class Extension link.
    2. Choose a Base Type. In this example we are choosing Page because we want to add extended attributes to a Page.
    3. Enter a name for the Sub Type. In this example we'll enter "HelloPage" as this will be a type of page that can say hello.
    4. Click the Add button.
  3. Add an Attribute:
    1. Click on the [Edit] link beside the word Default. This will allow us to add an attribute to the Default attribute set.
    2. Click the [Add New Attribute] link. This will bring up a form which allow us to define our new attribute.
    3. Fill in the form. You must specify at least a Name for the attribute. Let's name this attribute "HelloName". You can leave the rest of the fields blank. The other fields on the form are used to define how the attribute will be presented to an administrator when editing a page. These fields are nearly identical to those available when defining settings for a plugin, so you can find more information on them in the previous post in the series that discussed plugin settings.
    4. Click the Add button. This will being you back to the Manage Attributes Set screen, and you should see your new attribute at the bottom with links to [Edit] and [Delete] beside it.

We now have a HelloPage Page Sub Type that can be assigned to a page, and it will allow us to specify a value for the HelloName extended attribute. To do that, create a new page or edit an existing page. On the Edit Content screen, you'll see there's a select box labelled Type. When you click the select box you should now see an option labelled Page / HelloPage. Choose that option, which tells Mura that you want this page to use the HelloPage Sub Type that we just created. Now click the Extended Attributes tab and you should see a text box labelled HelloName. Enter your own name into the text box and then publish the page. That page will now have your name assigned to the extended attribute HelloName. Next we have to tell our plugin to make use of that extended attribute.

As we did in the last article, we're going to add another display object to our plugin, which will make use of the extended attribute. Let's do that by editing the /plugin/config.xml file. Here's the new version:

view plain print about
2    <name>Hello World Plugin Step 3</name>
3    <package>HelloWorldPluginStep3</package>
4    <version>1.0</version>
5    <provider>SilverWare Consulting</provider>
6    <providerURL></providerURL>
7    <category>Sample Application</category>
8    <settings>
9        <setting>
10            <name>toWhom</name>
11            <label>We are speaking to</label>
12            <hint>The name of the person to whom we're speaking</hint>
13            <type>text</type>
14            <required>true</required>
15            <validation></validation>
16            <regex></regex>
17            <message></message>
18            <defaultvalue></defaultvalue>
19            <optionlist></optionlist>
20            <optionlabellist></optionlabellist>
21        </setting>
22        <setting>
23            <name>sayWhat</name>
24            <label>We are going to say</label>
25            <hint>This is what we are going to say to them</hint>
26            <type>select</type>
27            <required>true</required>
28            <validation></validation>
29            <regex></regex>
30            <message></message>
31            <defaultvalue>Hello</defaultvalue>
32            <optionlist>Hello^Goodbye</optionlist>
33            <optionlabellist></optionlabellist>
34        </setting>
35    </settings>
36    <displayobjects location="global">
37        <displayobject name="Hello World Message"
38            displayobjectfile="displayObjects/dspHelloWorld.cfm"/>

39        <displayobject name="Greeting Message"
40            displayobjectfile="displayObjects/dspGreeting.cfm"/>

41        <displayobject name="Hello Page Message"
42            displayobjectfile="displayObjects/dspHelloPage.cfm"/>

43    </displayobjects>

Once again we changed the name and package elements to reflect the fact that this is a new plugin. Then we added a new displayobject element, Hello Page Message, which will be used to display a message based on the extended attribute assigned to a page.

Next we create that new display object. Here's what dspHelloPage.cfm will look like:

view plain print about
2<h1>Hello Page</h1>
3<p>Hello #Event.getContentBean().getValue("HelloName")#!</p>

We get the ContentBean for the current page using the getContentBean() method of the Event object, and then ask for the value of the HelloName extended attribute using the getValue() method. Install this plugin as you've done previously and add the Hello Page Message display object to a content area on the page for which you entered your name into the HelloName extended attribute. When you browse to that page ou should now see the output of the plugin saying hello to you. To use this same plugin display object on a different page to say hello to someone else, simply provide a different value for the HelloName extended attribute on that page.

As with the other articles in this series, the usefulness of this particular example is questionable, but the technique of providing information to a plugin on a page-by-page basis can be quite useful. For example, I'm using this technique in a plugin that generates catalog pages for an ecommerce site. The site administrator can specify a value in an extended attribute for a catalog page, and the plugin uses that value to ask the model for data for that particular catalog page. A display object in the plugin then formats the data and it is presented on the page.

As always, the completed plugin is available as an attachment to this post.

Thanks for the wonderful article. Just in time for my next project!!!!
# Posted By AUH | 5/31/10 3:28 PM
Thanks very helpful info. looking at Extended Attributes via code also.
# Posted By Glyn Jackson | 10/19/10 7:17 AM
Great post! I am just diving into Mura (after spending way too much time fighting our school's Joomla installation), and I am loving it.
Can a Page Sub Type be created dynamically?
If I want my plugin to use Extended Attributes functionality, I guess it will have to create necessary Page Sub Type Class Extension and its Attributes at install time? Any pointer on how this can be achieved?


# Posted By Azadi Saryev | 1/30/11 5:53 AM
To answer my own question for the sake of others looking for an answer:

Yes, Extended Attributes and SubTypes can be created dynamically.
# Posted By Azadi Saryev | 2/10/11 1:25 AM
Thansk for this - quick question:

Your example has (I added some spaces around the # sign in case it breaks in the comments):

<p>Hello # Event.getContentBean().getValue("HelloName") # !</p>

How do you tweak if you have, say a "Yes/No/Maybe" option field in the ExtData admin with the values 0,1,2 - and you wanted to display not the value "2", but the text "Maybe" in there?
# Posted By Jo | 9/17/12 10:08 AM