MojoMotor, like many other Content Management Systems, allows you to write add-ons to extend core functionality. Often times, add-on development is tedious, difficult, or near impossible. This is not the case with MojoMotor, and it is made clear by the response from the community.
Derek “sexy Canadian” Allard* says it the best:
“Looking at MojoMotor’s development since it was announced, no idea or group has contributed more to its personality than early adopters and addon developers. As someone who’s witnessed MojoMotor grow from a promising idea and into a robust community, watching passionate people bend, expand and push MojoMotor is a very satisfying feeling.”
In this post I will walk you through every step of creating a basic add-on, and by the end, you should have a fully working add-on and a better understanding of how to create them.
Because MojoMotor is built on top of CodeIgniter 2.0, creating an add-on is EXACTLY like writing a library for any other CodeIgniter application. MojoMotor (for brevity I will use MM from now on) uses Layout tags similar to ExpressionEngine’s Template Tags. There are a number of built in tags (found here: http://mojomotor.com/user_guide/mojo_tags.html) that you can use to develop your site. For most small brochure sites (the target audience of MM) these tags will be sufficient. However, other sites will need extended functionality that is not provided in the core.
(that one was for you Derek).
First thing is to decide what you want to create (obviously). Today I want to create a random number/string generator. This could be used to show random images in your layout. I also want the tag to look like this:
{mojo:random:numeric}
Now that you know what you want to do you need to setup the basic structure. The information for file structure can be found in the User Guide here: http://mojomotor.com/user_guide/development/index.html
Here is the beauty of creating MM add-ons: Since MM is built on CodeIgniter 2.0, we have access to all the libraries and helpers that CI contains. Lucky for us CI has a String Helper that has a random_string() function (http://codeigniter.com/user_guide/helpers/string_helper.html). So since we will be using that function in both methods, lets load it in the constructor. I will also be adding both methods in and will explain it more after.
class Random {
// This is not required but nice to have so the user know
// which version of the add-on they are running
public $addon_version = '1.0';
// This will hold the global MojoMotor object (the CI super global)
private $mojo;
// This is the constructor. It gets the CI super
// global for use in the other functions
public function Random()
{
// Let's get the CI super global
$this->mojo = & get_instance();
// Load in the String Helper
$this->mojo->load->helper('string');
}
public function numeric($tag_data)
{
}
public function alnum($tag_data)
{
}
}
Each add-on method must take 1 parameter. That parameter is an array containing all the current tag data from the layout. It contains the following array:
array(8) {
["tag_open"] => string(31) "{mojo:random:numeric length="10"}"
["class"] => string(6) "random"
["function"] => string(5) "alnum"
["parameters"] =>
array(1) {
["length"] => string(2) "10"
}
["full_match"] => string(31) "{mojo:random:numeric length="10"}"
["template"] => string(41) "{mojo:random:numeric}{/mojo:random:numeric}"
["marker"] => string(25) "M1t4k3m3d0wn2p4r4d153c1ty"
["tag_contents"] => string(0) ""
}
You will notice that I have added a “length” parameter to allow you to choose the length of the random number/string you want. So with this knowledge we can finish the add-on off by finishing the two methods:
class Random {
// This is not required but nice to have so the user know
// which version of the add-on they are running
public $addon_version = '1.0';
// This will hold the global MojoMotor object (the CI super global)
private $mojo;
// This is the constructor.
// It gets the CI super global for use in the other functions
public function Random()
{
// Let's get the CI super global
$this->mojo = & get_instance();
// Load in the String Helper
$this->mojo->load->helper('string');
}
public function numeric($tag_data)
{
// Check if a length parameter was given, if not, lets use 8
$len = isset($tag_data['parameters']['length']) ? $tag_data['parameters']['length'] : 8;
// We return the random string which replaces the tag in the layout
return random_string('numeric', $len);
}
public function alnum($tag_data)
{
// Check if a length parameter was given, if not, lets use 8
$len = isset($tag_data['parameters']['length']) ? $tag_data['parameters']['length'] : 8;
// We return the random string which replaces the tag in the layout
return random_string('alnum', $len);
}
}
That’s it. Now you can use it in your layouts like this (i.e. showing a random image):
<img src="/assets/images/rotating/img{mojo:random:numeric length="1"}.png" alt="" />
Now keep in mind, we use a length of 1 here because the length is the number of characters is the returned string. So this will return a number 0-9. Alternatively a length of ‘2’ would return a number between 0 and 99. This could be fixed by writing in a custom random number generator, or using the built in PHP one, but I am just using this as an example.
This is just a very simple and brief over view of what you can do with MojoMotor Add-ons. You can find some more advanced add-ons here on this site in the add-on section.
* Derek “sexy Canadian” Allard has given himself this nickname in hopes that if people say it enough…it will come true.
I cannot make this addon work. It gives me a completely blank page if I call it.
I probably have the structure wrong, but where does the array fit into this?
David, not sure if you have figured out the problem yet (you most likely have by now - but for future readers) don’t forget the <?php ?> tags at the beginning/end of the document.
This is PHP we are working in and those tags are very important.
Without those tags, this code will not work.
All in all, a very good, simple example.
Date: 08/25/10
Author: Dan Horrigan
I'm particularly interested in how the body class is added when the mojobar is opened! That was one that was hard to get around in the past.
Feb 09, 2012 3:20am
Robin
10/28/10
Considering adopting MojoMotor as the CMS for the smaller “brochure” style websites. ExpressionEngine probably overkill for them and too expensive for the clients with low budgets. Good to know that MojoMotor has add-on functionality; being able to tweak field-types etc. and code widgets for ExpressionEngine is a life-saver.