Derek Jones
President/CTO, EllisLab, Inc.

Accessories, a Quick Primer

Since the Developer Preview is still a ways off, I thought I would share something code-wise that is completely new to 2.0, control panel Accessories.  This is a very very basic accessory, that just displays some simple static information in the tab for the user.  We anticipate that these sort of accessories will be common for design firms to use, as each has a particular way that they like to instruct their clients on how to use the control panel, publish information, find out important details with respect to their account, etc.

The information in this post is targeting developers; users will never be exposed to any of this, in the same way that they don’t have to learn how plugins and extensions work to be able to use them.  Let’s break it down:

class Sample {

    
var $name           'Sample';
    var 
$id             'sample';
    var 
$version        '1.0';
    var 
$description    'A static Accessory demonstrating the basics of how Accessories work';
    var 
$sections       = array(); 

Like all other add-ons, you will have your primary class, whose name will match the file name just like plugins, modules, and accessories do now.  In this case, the file would be acc.sample.php.  There are some required class properties that control how your Accessory is displayed in the control panel.  The $name is the name displayed both in the list of available Accessories, and in the Accessory’s tab itself.  The $id is the id= attribute that is applied to your Accessory’s container, allowing you to target your content with CSS and JavaScript if necessary.  $version and $description are self explanatory.  $sections is an array that is accessed by ExpressionEngine to build your Accessory’s content.  To add content to your Accessory, you simply add it to the $sections array.  So simple that even those with zero PHP experience can do it.

/**
 * Constructor
 */
function Sample()
{
    
// load the EE super object
    
$this->EE =& get_instance();

Next we have the class constructor.  Basic PHP stuff here, with the addition of something that will be familiar to CodeIgniter users, a local reference to the application super object.  More on that later.

/**
 * Set Sections
 *
 * Set content for the accessory
 *
 * @access    public
 * @return    void
 */
function set_sections()
{
    $this
->sections['Heading One''<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                                    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                                    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
                                    nisi ut aliquip ex ea commodo consequat.</p>
                                    
                                    <p>Duis aute irure dolor in reprehenderit in voluptate velit esse
                                    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
                                    cupidatat non proident, sunt in culpa qui officia deserunt mollit
                                    anim id est laborum.</p>'
;
    
$this->sections['Heading Two''<ul>
                                        <li>List Item One</li>
                                        <li>Another List Item</li>
                                        <li>And something else</li>
                                      </ul>
                                      <p>(c) Copyright 2008 Me. All Rights Reserved.</p>'
;

This is the one required method in your Accessory class.  It is automatically called when the Accessory is loaded, and is what you will use to set (whoda thunk?) your content into the $sections array.  The array keys are output as headings, and can be named anything you like.  They are output in the order they are added to the array.  The value of each array key is the full content, including any markup you wish to use, of that “section” of your Accessory.  As you can see above, for an Accessory that wishes to display static information, it’s dead simple.

But what about more complex Accessories?  You essentially have no limitations apart from what you can do with PHP and MySQL.  You can have install() and uninstall() methods, which are automatically triggered on those actions, so you can use your own data tables.  They can have their own language files, and you have full access to all of the libraries and resources of ExpressionEngine, via the $this->EE super object.  Database library (including Active Record), Security library, Typography library, Email library, jQuery, and so on.

So what’s it look like?  What is the result of this simple accessory demo? (click for full size image)

desc

And for your viewing pleasure, the entire, unbroken file.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * ExpressionEngine - by EllisLab
 *
 * @package     ExpressionEngine
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2003 - 2008, EllisLab, Inc.
 * @license     http://expressionengine.com/docs/license.html
 * @link        http://expressionengine.com
 * @since       Version 2.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * ExpressionEngine Sample Accessory
 *
 * A static Accessory demonstrating the basics of how Accessories work
 *
 * @package     ExpressionEngine
 * @subpackage  Control Panel
 * @category    Accessories
 * @author      ExpressionEngine Dev Team
 * @link        http://expressionengine.com
 */
class Sample {

    
var $name           'Sample';
    var 
$id             'sample';
    var 
$version        '1.0';
    var 
$description    'A static Accessory demonstrating the basics of how Accessories work';
    var 
$sections       = array();

    
/**
     * Constructor
     */
    
function Sample()
    
{
        
// load the EE super object
        
$this->EE =& get_instance();
    
}

    
// --------------------------------------------------------------------

    /**
     * Set Sections
     *
     * Set content for the accessory
     *
     * @access    public
     * @return    void
     */
    
function set_sections()
    
{
        $this
->sections['Heading One''<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                                        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                                        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
                                        nisi ut aliquip ex ea commodo consequat.</p>
                                        
                                        <p>Duis aute irure dolor in reprehenderit in voluptate velit esse
                                        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
                                        cupidatat non proident, sunt in culpa qui officia deserunt mollit
                                        anim id est laborum.</p>'
;
        
$this->sections['Heading Two''<ul>
                                            <li>List Item One</li>
                                            <li>Another List Item</li>
                                            <li>And something else</li>
                                          </ul>
                                          <p>(c) Copyright 2008 Me. All Rights Reserved.</p>'
;
    
}
    
    
// --------------------------------------------------------------------
    
}
// END CLASS

/* End of file acc.sample.php */
/* Location: ./system/expressionengine/accessories/acc.sample.php */