Chapter 2: Getting the Lay of the Land Up Main page Chapter 4: Plugin Advanced Feature Overview 

3 Your First InterWorx Plugin

3.1 A Plugin of Your Very Own

The quickest way to build your first plugin is to create a working framework using the key components outlined in chapter 2. With the structure in place, additional functionality can be added in a much more organized fashion.

A basic example is the standard "Hello World" plugin, comprised of the following files:
  • Plugin Class file: ~iworx/plugins/hello/Plugin/Hello.php
    This file is the plugin "core". The majority of functionality and configuration of the plugin is here, and can be modified to suit a variety of needs. [see Figure 3.1↓]
Figure 3.1 ~iworx/plugins/hello/Plugin/Hello.php
1/**
2 * Hello World plugin.
3 *
4 * @package    InterWorx
5 * @subpackage Plugin
6 */ 
7class Plugin_Hello extends Plugin {
8  /**
9   * Init the plugin, disabling if necessary.
10   */
11  protected function _init() {
12    /*
13     * You can conditionally disable the plugin
14     */
15    //$this->_disable( ’reason for disabling goes here, will go in iworx.log’ );
1617    /*
18     * You can set plugin variables as needed like this
19     */
20    //$this->_setVar( ’myvariable’, ’somevalue’ );
2122    /*
23     * And retrieve them later like this
24     */
25    //$this->getVar( ’myvariable’ );
2627    /*
28     * You can set mixed datastore variables as needed like this
29     */
30    //$this->setDatastoreVar( ’storedvar’, array( 1, 2, 3 ) );
3132    /*
33     * And retrieve it later like this
34     */
35    //$this->getDatastoreVar( ’storedvar’ );
36  }
3738  /**
39   * Customizations to siteworx menu can be done here.
40   *
41   * @param IWorxMenuManager $MenuMan
42   */
43  public function updateSiteworxMenu( IWorxMenuManager $MenuMan ) {
44    $new_data = array( ’text’   => ’Hello World’,
45                       ’url’    => ’/siteworx/hello/world’,
46                       ’class’  => ’iw-i-star’ );
47    
48    $MenuMan->addMenuItemAfter( ’iw-menu-home’, ’hello’, $new_data );
49    // also updateMenuItem( $id, $data )
50    //      removeMenuItem( $id )
51  }
5253  /**
54   * Customizations to siteworx menu can be done here.
55   *
56   * @param IWorxMenuManager $MenuMan
57   */
58  public function updateNodeworxMenu( IWorxMenuManager $MenuMan ) {
59    $new_data = array( ’text’   => ’Hello World’,
60                       ’url’    => ’/nodeworx/hello/world’,
61                       ’class’  => ’iw-i-star’ );
62    $MenuMan->addMenuItemAfter( ’iw-menu-home’, ’hello’, $new_data );
63    // also updateMenuItem( $id, $data )
64    //      removeMenuItem( $id )
65  }
66} 
  • ~iworx/plugins/hello/Ctrl/Nodeworx/HelloWorld.php
    Since we updated the NodeWorx menu to include our new plugin (line 62), we should define a controller for it. Assigning the title below (line 12) will allow us to specify the title header in-browser. [see Figure 3.2↓]
Figure 3.2 ~iworx/plugins/hello/Ctrl/Nodeworx/HelloWorld.php
1/**
2 * Nodeworx hello world sample plugin controller.
3 *
4 * @package    InterWorx
5 * @subpackage Plugin
6 */ 
7class Ctrl_Nodeworx_HelloWorld extends Ctrl_Nodeworx_Plugin {
8  /**
9   * Default action.
10   */   
11  public function indexAction() {
12    $this->getView()->assign( ’title’, ’Hello World Sample Plugin, nodeworx’ );
13  }
14}
  • ~iworx/plugins/hello/Ctrl/Siteworx/HelloWorld.php
    Same as above, we’ve included the plugin on the SiteWorx menu, so we should define a controller and set the title (line 12). [see Figure 3.3↓]
Figure 3.3 ~iworx/plugins/hello/Ctrl/Siteworx/HelloWorld.php
1/**
2 * Siteworx hello world sample plugin controller.
3 *
4 * @package    InterWorx
5 * @subpackage Plugin
6 */ 
7class Ctrl_Siteworx_HelloWorld extends Ctrl_Siteworx_Plugin {
8  /**
9   * Default action.
10   */
11  public function indexAction() {
12    $this->getView()->assign( ’title’, ’Hello World Sample Plugin, siteworx’ );
13  }
14} 
  • ~iworx/plugins/hello/plugin.ini
    The plugin INI is where the plugin metadata is stored, much of which is displayed in NodeWorx > Plugin Management. [see Figure 3.4↓]
Figure 3.4 ~iworx/plugins/hello/plugin.ini
1[plugin]
2name="Hello World"
3description="Hello World Sample InterWorx Plugin"
4details="This plugin adds a ’Hello World’ menu item to the top of BOTH NodeWorx and SiteWorx."
5version="1.0"
6author="interworx"
  • ~iworx/plugins/hello/templates/default.tpl
    The template for this plugin isn’t too complicated — just some text to display on the page. Templates ordinarily are where forms and payloads would be included. [see Figure 3.5↓]
Figure 3.5 ~iworx/plugins/hello/templates/default.tpl
1Hello, world!
 Chapter 2: Getting the Lay of the Land Up Main page Chapter 4: Plugin Advanced Feature Overview 

(C) 2017 by InterWorx LLC