Archived

Difference between revisions of "Developing a MVC Component/Adding categories"

From Joomla! Documentation

< Archived:Developing a MVC Component
(61 intermediate revisions by 20 users not shown)
Line 1: Line 1:
{{underconstruction}}
+
{{version/tutor|2.5}}
{{future|1.6}}
+
{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Contents}}
  
== Articles in this series ==
+
== Introduction ==
{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!1.6 - Contents}}
+
This tutorial is part of the [[Developing a Model-View-Controller (MVC) Component for Joomla!2.5]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
  
== Indroduction ==
+
The Joomla framework has implemented the use of categories for all components. Adding categorized ability to a component is fairly simple.
This tutorial is part of the [[Developing a Model-View-Controller (MVC) Component for Joomla!1.6]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
 
 
 
The Joomla!1.6 framework has implemented the use of categories for all components. Adding categorized ability to a component is fairly simple.
 
  
 
== Modifying the SQL ==
 
== Modifying the SQL ==
Line 33: Line 30:
 
</span>
 
</span>
  
<span id="admin/sql/update.mysql.utf8.sql">
+
<span id="admin/sql/updates/mysql/0.0.12.sql">
''admin/sql/update.mysql.utf8.sql''
+
''admin/sql/updates/mysql/0.0.12.sql''
 
<source lang="sql">
 
<source lang="sql">
 
ALTER TABLE `#__helloworld` ADD `catid` int(11) NOT NULL default '0'  
 
ALTER TABLE `#__helloworld` ADD `catid` int(11) NOT NULL default '0'  
</source>
 
</span>
 
 
The ''TableHelloWorld'' class must know that there exists a ''catid'' field.
 
 
In ''admin/tables/helloworld.php'' file, put these lines:
 
 
<span id="admin/tables/helloworld.php">
 
''admin/tables/helloworld.php''
 
<source lang="php">
 
<?php
 
// No direct access
 
defined('_JEXEC') or die('Restricted access');
 
// import Joomla table library
 
jimport('joomla.database.table');
 
/**
 
* Hello Table class
 
*/
 
class TableHelloWorld extends JTable
 
{
 
/**
 
* Primary Key
 
*
 
* @var int
 
*/
 
var $id = null;
 
/**
 
* @var string
 
*/
 
var $greeting = null;
 
/**
 
* @var int
 
*/
 
var $catid = null;
 
/**
 
* Constructor
 
*
 
* @param object Database connector object
 
*/
 
function TableHelloWorld(&$db)
 
{
 
parent::__construct('#__helloworld', 'id', $db);
 
}
 
}
 
 
</source>
 
</source>
 
</span>
 
</span>
Line 91: Line 44:
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
<form>
+
<form
<fields>
+
addrulepath="/administrator/components/com_helloworld/models/rules"
 +
>
 +
<fieldset>
 
<field
 
<field
id="id"
 
 
name="id"
 
name="id"
 
type="hidden"
 
type="hidden"
 
/>
 
/>
 
<field
 
<field
id="greeting"
 
 
name="greeting"
 
name="greeting"
 
type="text"
 
type="text"
 +
label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
 +
description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
 
size="40"
 
size="40"
 
class="inputbox validate-greeting"
 
class="inputbox validate-greeting"
Line 107: Line 62:
 
required="true"
 
required="true"
 
default=""
 
default=""
label="com_helloworld_HelloWorld_Greeting"
 
description="com_helloworld_HelloWorld_Greeting_Desc"
 
 
/>
 
/>
 
<field
 
<field
id="catid"
 
 
name="catid"
 
name="catid"
type="Categories"
+
type="category"
 
extension="com_helloworld"
 
extension="com_helloworld"
allow_none="true"
 
 
class="inputbox"
 
class="inputbox"
 
default=""
 
default=""
label="com_helloworld_HelloWorld_Category"
+
label="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL"
description="com_helloworld_HelloWorld_Category_Desc"
+
description="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC"
 
required="true"
 
required="true"
 
>
 
>
<option value="0">JOption_No_Category</option>
+
<option value="0">JOPTION_SELECT_CATEGORY</option>
 
</field>
 
</field>
</fields>
+
</fieldset>
</form></source>
+
</form>
 +
</source>
 
</span>
 
</span>
  
Line 131: Line 83:
  
 
== Modifying the menu type ==
 
== Modifying the menu type ==
The HelloWorld menu type display a drop down list of all messages. If the message is categorized, we have to add the category in this display.
+
The HelloWorld menu type displays a drop down list of all messages. If the message is categorized, we have to add the category in this display.
  
 
In the ''admin/models/fields/helloworld.php'' file, put these lines:
 
In the ''admin/models/fields/helloworld.php'' file, put these lines:
Line 141: Line 93:
 
// No direct access to this file
 
// No direct access to this file
 
defined('_JEXEC') or die;
 
defined('_JEXEC') or die;
 +
 
// import the list field type
 
// import the list field type
jimport('joomla.html.html.list');
+
jimport('joomla.form.helper');
 +
JFormHelper::loadFieldClass('list');
 +
 
 
/**
 
/**
 
  * HelloWorld Form Field class for the HelloWorld component
 
  * HelloWorld Form Field class for the HelloWorld component
Line 154: Line 109:
 
*/
 
*/
 
protected $type = 'HelloWorld';
 
protected $type = 'HelloWorld';
 +
 
/**
 
/**
 
* Method to get a list of options for a list input.
 
* Method to get a list of options for a list input.
Line 159: Line 115:
 
* @return array An array of JHtml options.
 
* @return array An array of JHtml options.
 
*/
 
*/
protected function _getOptions()  
+
protected function getOptions()  
 
{
 
{
 
$db = JFactory::getDBO();
 
$db = JFactory::getDBO();
$query = new JQuery;
+
 
 +
/// $query = new JDatabaseQuery; WARNING - There's an error in this line, JDatabaseQuery is an abstract class
 +
$query = $db->getQuery(true); // THIS IS THE FIX, WARNING IT MUST BE FIXED IN THE ZIP FILES
 +
 
 
$query->select('#__helloworld.id as id,greeting,#__categories.title as category,catid');
 
$query->select('#__helloworld.id as id,greeting,#__categories.title as category,catid');
 
$query->from('#__helloworld');
 
$query->from('#__helloworld');
Line 169: Line 128:
 
$messages = $db->loadObjectList();
 
$messages = $db->loadObjectList();
 
$options = array();
 
$options = array();
foreach($messages as $message)  
+
if ($messages)
 
{
 
{
$options[] = JHtml::_('select.option', $message->id, $message->greeting . ($message->catid ? ' (' . $message->category . ')' : ''));
+
foreach($messages as $message)
 +
{
 +
$options[] = JHtml::_('select.option', $message->id, $message->greeting .
 +
                      ($message->catid ? ' (' . $message->category . ')' : ''));
 +
}
 
}
 
}
$options = array_merge(parent::_getOptions() , $options);
+
$options = array_merge(parent::getOptions(), $options);
 
return $options;
 
return $options;
 
}
 
}
Line 183: Line 146:
  
 
== Managing the submenu ==
 
== Managing the submenu ==
The ''com_categories'' component allow to set the submenu using an helper file. With your favorite file manager and editor, put a ''admin/helpers/helloworld.php'' file containing these lines:
+
The ''com_categories'' component allows to set the submenu using a helper file. With your favorite file manager and editor, put a ''admin/helpers/helloworld.php'' file containing these lines:
  
 
<span id="admin/helpers/helloworld.php">
 
<span id="admin/helpers/helloworld.php">
Line 191: Line 154:
 
// No direct access to this file
 
// No direct access to this file
 
defined('_JEXEC') or die;
 
defined('_JEXEC') or die;
 +
 
/**
 
/**
 
  * HelloWorld component helper.
 
  * HelloWorld component helper.
 
  */
 
  */
class HelloWorldHelper
+
abstract class HelloWorldHelper
 
{
 
{
 
/**
 
/**
 
* Configure the Linkbar.
 
* Configure the Linkbar.
 
*/
 
*/
public static function addSubmenu($submenu)
+
public static function addSubmenu($submenu)  
 
{
 
{
JSubMenuHelper::addEntry(
+
JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
JText::_('com_helloworld_Messages'),
+
                        'index.php?option=com_helloworld', $submenu == 'messages');
'index.php?option=com_helloworld',
+
JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),
$submenu=='messages');
+
                        'index.php?option=com_categories&view=categories&extension=com_helloworld',
JSubMenuHelper::addEntry(
+
                        $submenu == 'categories');
JText::_('com_helloworld_Categories'),
+
// set some global property
'index.php?option=com_categories&view=categories&extension=com_helloworld',
+
$document = JFactory::getDocument();
$submenu=='categories');
+
$document->addStyleDeclaration('.icon-48-helloworld ' .
$document = &JFactory::getDocument();
+
                              '{background-image: url(../media/com_helloworld/images/tux-48x48.png);}');
$document->addStyleDeclaration('.icon-48-helloworld {background-image: url(../media/com_helloworld/images/tux-48x48.png);}');
+
if ($submenu == 'categories')  
if ($submenu=='categories')
 
 
{
 
{
$document->setTitle(JText::_('com_helloworld_Administration').' - '.JText::_('com_helloworld_Categories'));
+
$document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION_CATEGORIES'));
JToolBarHelper::title(JText::_('com_helloworld_Manager') . ': <small><small>[ ' . JText::_('com_helloworld_Categories') . ' ]</small></small>', 'helloworld');
+
}
JToolBarHelper::preferences('com_helloworld');
 
}
 
 
}
 
}
 
}
 
}
 
</source>
 
</source>
 
</span>
 
</span>
 +
'''NOTE:''' You MUST use your component name (without com_) for the helper file name, or else your submenus won't show in category view.
  
 
This function will be automatically called by the ''com_categories'' component. Note that it will
 
This function will be automatically called by the ''com_categories'' component. Note that it will
Line 236: Line 198:
 
// No direct access to this file
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
 +
 
// import Joomla controller library
 
// import Joomla controller library
 
jimport('joomla.application.component.controller');
 
jimport('joomla.application.component.controller');
 +
 
/**
 
/**
 
  * General Controller of HelloWorld component
 
  * General Controller of HelloWorld component
Line 248: Line 212:
 
* @return void
 
* @return void
 
*/
 
*/
function display($cachable = false)  
+
public function display($cachable = false, $urlparams = false)
 
{
 
{
 
// set default view if not set
 
// set default view if not set
JRequest::setVar('view', JRequest::getCmd('view', 'HelloWorldList'));
+
$input = JFactory::getApplication()->input;
 +
$input->set('view', $input->getCmd('view', 'HelloWorlds'));
 +
 
 
// call parent behavior
 
// call parent behavior
parent::display($cachable);
+
parent::display($cachable, $urlparams);
// Add submenu and icons
+
 
require_once JPATH_COMPONENT . DS . 'helpers' . DS . 'helloworld.php';
+
// Set the submenu
 
HelloWorldHelper::addSubmenu('messages');
 
HelloWorldHelper::addSubmenu('messages');
 
}
 
}
Line 261: Line 227:
 
</source>
 
</source>
 
</span>
 
</span>
 +
== Adding some ACL ==
 +
<span id="admin/access.xml">
 +
''admin/access.xml''
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="utf-8" ?>
 +
<access component="com_helloworld">
 +
<section name="component">
 +
<action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC" />
 +
<action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC" />
 +
<action name="core.create" title="JACTION_CREATE" description="JACTION_CREATE_COMPONENT_DESC" />
 +
<action name="core.delete" title="JACTION_DELETE" description="JACTION_DELETE_COMPONENT_DESC" />
 +
<action name="core.edit" title="JACTION_EDIT" description="JACTION_EDIT_COMPONENT_DESC" />
 +
</section>
 +
<section name="message">
 +
<action name="core.delete" title="JACTION_DELETE" description="COM_HELLOWORD_ACCESS_DELETE_DESC" />
 +
<action name="core.edit" title="JACTION_EDIT" description="COM_OLA_HELLOWORD_EDIT_DESC" />
 +
</section>
 +
</access>
 +
 +
</source>
 +
</span>
 +
 +
'''NOTE:''' If you don't add this file, buttons "New" "Edit" and ... don't show in category view . for more information read section Adding ACL on top of the page.
  
 
<span id="admin/helloworld.php">
 
<span id="admin/helloworld.php">
Line 268: Line 257:
 
// No direct access to this file
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
 +
 +
// require helper file
 +
JLoader::register('HelloWorldHelper', dirname(__FILE__) . DS . 'helpers' . DS . 'helloworld.php');
 +
 
// import joomla controller library
 
// import joomla controller library
 
jimport('joomla.application.component.controller');
 
jimport('joomla.application.component.controller');
 +
 
// Get an instance of the controller prefixed by HelloWorld
 
// Get an instance of the controller prefixed by HelloWorld
 
$controller = JController::getInstance('HelloWorld');
 
$controller = JController::getInstance('HelloWorld');
 +
 
// Perform the Request task
 
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
+
$input = JFactory::getApplication()->input;
 +
$controller->execute($input->getCmd('task'));
 +
 
 
// Redirect if set by the controller
 
// Redirect if set by the controller
 
$controller->redirect();
 
$controller->redirect();
Line 285: Line 282:
 
''admin/language/en-GB/en-GB.com_helloworld.ini''
 
''admin/language/en-GB/en-GB.com_helloworld.ini''
 
<source lang="ini">
 
<source lang="ini">
# Joomla16.Tutorials
+
COM_HELLOWORLD="Hello World!"
# Copyright (C) 2005 - 2009 Open Source Matters. All rights reserved.
+
COM_HELLOWORLD_ADMINISTRATION="HelloWorld - Administration"
# License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
+
COM_HELLOWORLD_ADMINISTRATION_CATEGORIES="HelloWorld - Categories"
# Note : All ini files need to be saved as UTF-8 - No BOM
+
COM_HELLOWORLD_HELLOWORLD_CREATING="HelloWorld - Creating"
 
+
COM_HELLOWORLD_HELLOWORLD_DETAILS="Details"
COM_HELLOWORLD_ADMINISTRATION=HelloWorld administration
+
COM_HELLOWORLD_HELLOWORLD_EDITING="HelloWorld - Editing"
COM_HELLOWORLD_CATEGORIES=Categories
+
COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE="Some values are unacceptable"
COM_HELLOWORLD_MANAGER=HelloWorld manager
+
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC="The category the messages belongs to"
COM_HELLOWORLD_MESSAGES=Messages
+
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL="Category"
COM_HELLOWORLD_HELLOWORLD_CATEGORY=Category
+
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC="This message will be displayed"
COM_HELLOWORLD_HELLOWORLD_CATEGORY_DESC=Category of the message
+
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL="Message"
COM_HELLOWORLD_HELLOWORLD_CREATING=Creating
+
COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING="Greeting"
COM_HELLOWORLD_HELLOWORLD_DETAILS=Details
+
COM_HELLOWORLD_HELLOWORLD_HEADING_ID="Id"
COM_HELLOWORLD_HELLOWORLD_EDITING=Editing
+
COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT="HelloWorld manager: Edit Message"
COM_HELLOWORLD_HELLOWORLD_ERROR_SOME_VALUES_ARE_UNACCEPTABLE=Some values are unacceptable
+
COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW="HelloWorld manager: New Message"
COM_HELLOWORLD_HELLOWORLD_GREETING_DESC=Message to be displayed
+
COM_HELLOWORLD_MANAGER_HELLOWORLDS="HelloWorld manager"
COM_HELLOWORLD_HELLOWORLD_GREETING=Greeting
+
COM_HELLOWORLD_N_ITEMS_DELETED_1="One message deleted"
COM_HELLOWORLD_HELLOWORLDLIST_ARE_YOU_SURE_YOU_WANT_TO_DELETE_THESE_GREETINGS=Are you sure you want to delete these greetings?
+
COM_HELLOWORLD_N_ITEMS_DELETED_MORE="%d messages deleted"
COM_HELLOWORLD_HELLOWORLDLIST_GREETING=Greeting
+
COM_HELLOWORLD_SUBMENU_MESSAGES="Messages"
COM_HELLOWORLD_HELLOWORLDLIST_GREETINGS_REMOVED=Greetings removed
+
COM_HELLOWORLD_SUBMENU_CATEGORIES="Categories"
COM_HELLOWORLD_HELLOWORLDLIST_ID=Id
 
COM_HELLOWORLD_HELLOWORLDLIST_ONE_OR_MORE_GREETINGS_COULD_NOT_BE_DELETED=One or more greetings could not be deleted: %s
 
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_MSG_DESC=This message will be displayed
 
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_MSG_LABEL=Message
 
 
</source>
 
</source>
 
</span>
 
</span>
Line 316: Line 309:
 
Content of your code directory
 
Content of your code directory
 
* ''[[#helloworld.xml|helloworld.xml]]''
 
* ''[[#helloworld.xml|helloworld.xml]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|site/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_02#site/helloworld.php|site/helloworld.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_02#site/helloworld.php|site/helloworld.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_02#site/controller.php|site/controller.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_02#site/controller.php|site/controller.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|site/views/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|site/views/helloworld/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/helloworld/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_04#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_04#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|site/views/helloworld/tmpl/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/helloworld/tmpl/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_06#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|site/models/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_02#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_06#site/models/helloworld.php|site/models/helloworld.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/models/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_02#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#site/models/helloworld.php|site/models/helloworld.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|site/language/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/language/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|site/language/en-GB/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/language/en-GB/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_08#site/language/en-GB/en-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_08#site/language/en-GB/en-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/index.html]]''
 
* ''[[#admin/helloworld.php|admin/helloworld.php]]''
 
* ''[[#admin/helloworld.php|admin/helloworld.php]]''
 
* ''[[#admin/controller.php|admin/controller.php]]''
 
* ''[[#admin/controller.php|admin/controller.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/sql/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_12#Adding%20some%20ACL|admin/access.xml]]''
* ''[[#admin/sql/install.mysql.utf8.sql|admin/sql/install.mysql.utf8.sql]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_06#admin/sql/uninstall.mysql.utf8.sql|admin/sql/uninstall.mysql.utf8.sql]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#admin/sql/install.mysql.utf8.sql|admin/sql/install.mysql.utf8.sql]]''
* ''[[#admin/sql/update.mysql.utf8.sql|admin/sql/update.mysql.utf8.sql]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#admin/sql/uninstall.mysql.utf8.sql|admin/sql/uninstall.mysql.utf8.sql]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/models/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/updates/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/models/fields/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/updates/mysql/index.html]]''
 +
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
 +
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#admin/sql/install.mysql.utf8.sql|admin/sql/updates/mysql/0.0.6.sql]]''
 +
* ''[[#admin/sql/updates/mysql/0.0.12.sql|admin/sql/updates/mysql/0.0.12.sql]]''
 +
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/models/index.html]]''
 +
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/models/fields/index.html]]''
 
* ''[[#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]''
 
* ''[[#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/models/forms/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/models/forms/index.html]]''
 
* ''[[#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]''
 
* ''[[#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_11#admin/models/forms/helloworld.js|admin/models/forms/helloworld.js]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_11#admin/models/forms/helloworld.js|admin/models/forms/helloworld.js]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/models/rules/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/models/rules/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_11#admin/models/rules/greeting.php|admin/models/rules/greeting.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_11#admin/models/rules/greeting.php|admin/models/rules/greeting.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_11#admin/models/helloworld.php|admin/models/helloworld.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_11#admin/models/helloworld.php|admin/models/helloworld.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_09#admin/models/helloworldlist.php|admin/models/helloworldlist.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_09#admin/models/helloworlds.php|admin/models/helloworlds.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/views/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/views/helloworldlist/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/helloworlds/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_10#admin/views/helloworldlist/view.html.php|admin/views/helloworldlist/view.html.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_10#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/views/helloworldlist/tmpl/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/helloworlds/tmpl/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_11#admin/views/helloworldlist/tmpl/default.php|admin/views/helloworldlist/tmpl/default.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_09#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_07#admin/views/helloworldlist/tmpl/default_head.php|admin/views/helloworldlist/tmpl/default_head.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_07#admin/views/helloworlds/tmpl/default_head.php|admin/views/helloworlds/tmpl/default_head.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_07#admin/views/helloworldlist/tmpl/default_body.php|admin/views/helloworldlist/tmpl/default_body.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_07#admin/views/helloworlds/tmpl/default_body.php|admin/views/helloworlds/tmpl/default_body.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_07#admin/views/helloworldlist/tmpl/default_foot.php|admin/views/helloworldlist/tmpl/default_foot.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_07#admin/views/helloworlds/tmpl/default_foot.php|admin/views/helloworlds/tmpl/default_foot.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/views/helloworldlist/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/helloworlds/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_11#admin/views/helloworld/view.html.php|admin/views/helloworld/view.html.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_11#admin/views/helloworld/view.html.php|admin/views/helloworld/view.html.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_11#admin/views/helloworld/submitbutton.js|admin/views/helloworld/submitbutton.js]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_11#admin/views/helloworld/submitbutton.js|admin/views/helloworld/submitbutton.js]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/views/helloworld/tmpl/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/helloworld/tmpl/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_11#admin/views/helloworld/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_11#admin/views/helloworld/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/tables/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/helpers/index.html]]''
 +
* ''[[#admin/helpers/helloworld.php|admin/helpers/helloworld.php]]''
 +
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/tables/index.html]]''
 
* ''[[#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
 
* ''[[#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
 
* ''[[#admin/language/en-GB/en-GB.com_helloworld.ini|admin/language/en-GB/en-GB.com_helloworld.ini]]''
 
* ''[[#admin/language/en-GB/en-GB.com_helloworld.ini|admin/language/en-GB/en-GB.com_helloworld.ini]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_08#admin/language/en-GB/en-GB.com_helloworld.menu.ini|admin/language/en-GB/en-GB.com_helloworld.menu.ini]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_08#admin/language/en-GB/en-GB.com_helloworld.menu.ini|admin/language/en-GB/en-GB.com_helloworld.menu.ini]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|admin/controllers/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/controllers/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_11#admin/controllers/helloworld.php|admin/controllers/helloworld.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_11#admin/controllers/helloworld.php|admin/controllers/helloworld.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_11#admin/controllers/helloworldlist.php|admin/controllers/helloworldlist.php]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_11#admin/controllers/helloworlds.php|admin/controllers/helloworldlist.php]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_08#language/en-GB/en-GB.ini|language/en-GB/en-GB.ini]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_08#language/en-GB/en-GB.ini|language/en-GB/en-GB.ini]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|media/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|media/index.html]]''
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1.6_-_Part_01#index.html|media/images/index.html]]''
+
* ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|media/images/index.html]]''
 
* ''media/images/tux-16x16.png''
 
* ''media/images/tux-16x16.png''
 
* ''media/images/tux-48x48.png''
 
* ''media/images/tux-48x48.png''
  
Create a compressed file of this directory or directly download the [http://joomlacode.org/gf/download/frsrelease/11394/46171/com_helloworld-1.6-part12.zip archive] and install it using the extension manager of Joomla!1.6. You can add a menu item of this component using the menu manager in the backend.
+
Create a compressed file of this directory or directly download the [http://joomlacode.org/gf/download/frsrelease/11394/58410/com_helloworld-1.6-part12.zip archive] and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.
  
 
<span id="helloworld.xml">
 
<span id="helloworld.xml">
Line 379: Line 379:
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="1.6.0" method="upgrade">
+
<extension type="component" version="2.5.0" method="upgrade">
<name>Hello World!</name>
+
 
 +
<name>COM_HELLOWORLD</name>
 +
<!-- The following elements are optional and free of formatting constraints -->
 
<creationDate>November 2009</creationDate>
 
<creationDate>November 2009</creationDate>
 
<author>John Doe</author>
 
<author>John Doe</author>
Line 387: Line 389:
 
<copyright>Copyright Info</copyright>
 
<copyright>Copyright Info</copyright>
 
<license>License Info</license>
 
<license>License Info</license>
 +
<!--  The version string is recorded in the components table -->
 
<version>0.0.12</version>
 
<version>0.0.12</version>
<description>com_helloworld_Description</description>
+
<!-- The description is optional and defaults to the name -->
 +
<description>COM_HELLOWORLD_DESCRIPTION</description>
  
 
<install> <!-- Runs on install -->
 
<install> <!-- Runs on install -->
Line 400: Line 404:
 
</sql>
 
</sql>
 
</uninstall>
 
</uninstall>
<update> <!-- Runs on update -->
+
<update> <!-- Runs on update; New in 2.5 -->
<sql>
+
<schemas>
<file driver="mysql" charset="utf8">sql/update.mysql.utf8.sql</file>
+
<schemapath type="mysql">sql/updates/mysql</schemapath>
</sql>
+
</schemas>
 
</update>
 
</update>
  
 +
<!-- Site Main File Copy Section -->
 +
<!-- Note the folder attribute: This attribute describes the folder
 +
to copy FROM in the package to install therefore files copied
 +
in this section are copied from /site/ in the package -->
 
<files folder="site">
 
<files folder="site">
 
<filename>index.html</filename>
 
<filename>index.html</filename>
Line 419: Line 427:
 
<folder>images</folder>
 
<folder>images</folder>
 
</media>
 
</media>
+
 
 
<administration>
 
<administration>
<menu img="../media/com_helloworld/images/tux-16x16.png">Hello World!</menu>
+
<!-- Administration Menu Section -->
 +
<menu img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
 +
<!-- Administration Main File Copy Section -->
 +
<!-- Note the folder attribute: This attribute describes the folder
 +
to copy FROM in the package to install therefore files copied
 +
in this section are copied from /admin/ in the package -->
 
<files folder="admin">
 
<files folder="admin">
 +
<!-- Admin Main File Copy Section -->
 
<filename>index.html</filename>
 
<filename>index.html</filename>
 
<filename>helloworld.php</filename>
 
<filename>helloworld.php</filename>
 
<filename>controller.php</filename>
 
<filename>controller.php</filename>
 +
          <!-- WARNING <filename>access.php</filename> has an incorrect file extension, should be corrected in the ZIP-file -->
 +
<filename>access.xml</filename>
 +
<!-- SQL files section -->
 
<folder>sql</folder>
 
<folder>sql</folder>
 +
<!-- tables files section -->
 
<folder>tables</folder>
 
<folder>tables</folder>
 +
<!-- models files section -->
 
<folder>models</folder>
 
<folder>models</folder>
 +
<!-- views files section -->
 
<folder>views</folder>
 
<folder>views</folder>
 +
<!-- controllers files section -->
 
<folder>controllers</folder>
 
<folder>controllers</folder>
 +
<!-- helpers files section -->
 
<folder>helpers</folder>
 
<folder>helpers</folder>
</files>
+
</files>
 +
 
 
<languages folder="admin">
 
<languages folder="admin">
 
<language tag="en-GB">language/en-GB/en-GB.com_helloworld.ini</language>
 
<language tag="en-GB">language/en-GB/en-GB.com_helloworld.ini</language>
<language tag="en-GB">language/en-GB/en-GB.com_helloworld.menu.ini</language>
+
<language tag="en-GB">language/en-GB/en-GB.com_helloworld.sys.ini</language>
 
</languages>
 
</languages>
 
</administration>
 
</administration>
 +
 
</extension>
 
</extension>
 
</source>
 
</source>
 
</span>
 
</span>
 +
 +
== Zips ==
 +
Download the zip file for this Part:
 +
[https://github.com/downloads/rvsjoen/joomla-tutorials/com_helloworld-part12.zip]
 +
 +
== Navigate ==
 +
[[Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Part 11|Prev: Adding verifications]]
 +
[[Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Part 13|Next: Adding configuration]]
  
 
== Contributors ==
 
== Contributors ==
 
*[[User:cdemko|Christophe Demko]]
 
*[[User:cdemko|Christophe Demko]]
 +
*[[User:oaksu|Ozgur Aksu]]
 +
*[[User:Anibal.sanchez|Anibal Sanchez]] // Just a fix
 +
*[[User:Postema|Hans Postema]] // Just another fix
  
 
[[Category:Development]]
 
[[Category:Development]]
[[category:Joomla! 1.6]]
+
[[Category:Joomla! 1.6]]
[[category:Manual]]
+
[[Category:Joomla! 1.7]]
 +
[[Category:Joomla! 2.5]]

Revision as of 10:59, 1 December 2013

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.


Introduction[edit]

This tutorial is part of the Developing a Model-View-Controller (MVC) Component for Joomla!2.5 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.

The Joomla framework has implemented the use of categories for all components. Adding categorized ability to a component is fairly simple.

Modifying the SQL[edit]

In order to manage categories, we have to change the SQL tables.

With your favorite editor, modify admin/sql/install.mysql.utf8.sql and put these lines:

admin/sql/install.mysql.utf8.sql

DROP TABLE IF EXISTS `#__helloworld`;

CREATE TABLE `#__helloworld` (
  `id` int(11) NOT NULL auto_increment,
  `greeting` varchar(25) NOT NULL,
  `catid` int(11) NOT NULL default '0',
   PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

INSERT INTO `#__helloworld` (`greeting`) VALUES
	('Hello World!'),
	('Good bye World!');

admin/sql/updates/mysql/0.0.12.sql

ALTER TABLE `#__helloworld` ADD `catid` int(11) NOT NULL default '0'

Modifying the form[edit]

A HelloWorld message can now belong to a category. We have to modify the editing form. In the admin/models/forms/helloworld.xml file, put these lines:

admin/models/forms/helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<form
	addrulepath="/administrator/components/com_helloworld/models/rules"
>
	<fieldset>
		<field
			name="id"
			type="hidden"
		/>
		<field
			name="greeting"
			type="text"
			label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
			description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
			size="40"
			class="inputbox validate-greeting"
			validate="greeting"
			required="true"
			default=""
		/>
		<field
			name="catid"
			type="category"
			extension="com_helloworld"
			class="inputbox"
			default=""
			label="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL"
			description="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC"
			required="true"
		>
			<option value="0">JOPTION_SELECT_CATEGORY</option>
		</field>
	</fieldset>
</form>

Note that the category can be 0 (representing no category).

Modifying the menu type[edit]

The HelloWorld menu type displays a drop down list of all messages. If the message is categorized, we have to add the category in this display.

In the admin/models/fields/helloworld.php file, put these lines:

admin/models/fields/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die;

// import the list field type
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');

/**
 * HelloWorld Form Field class for the HelloWorld component
 */
class JFormFieldHelloWorld extends JFormFieldList
{
	/**
	 * The field type.
	 *
	 * @var		string
	 */
	protected $type = 'HelloWorld';

	/**
	 * Method to get a list of options for a list input.
	 *
	 * @return	array		An array of JHtml options.
	 */
	protected function getOptions() 
	{
		$db = JFactory::getDBO();

		/// $query = new JDatabaseQuery; WARNING - There's an error in this line, JDatabaseQuery is an abstract class
		$query = $db->getQuery(true); // THIS IS THE FIX, WARNING IT MUST BE FIXED IN THE ZIP FILES

		$query->select('#__helloworld.id as id,greeting,#__categories.title as category,catid');
		$query->from('#__helloworld');
		$query->leftJoin('#__categories on catid=#__categories.id');
		$db->setQuery((string)$query);
		$messages = $db->loadObjectList();
		$options = array();
		if ($messages)
		{
			foreach($messages as $message) 
			{
				$options[] = JHtml::_('select.option', $message->id, $message->greeting .
				                      ($message->catid ? ' (' . $message->category . ')' : ''));
			}
		}
		$options = array_merge(parent::getOptions(), $options);
		return $options;
	}
}

It will now display the category between parenthesis.

Managing the submenu[edit]

The com_categories component allows to set the submenu using a helper file. With your favorite file manager and editor, put a admin/helpers/helloworld.php file containing these lines:

admin/helpers/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die;

/**
 * HelloWorld component helper.
 */
abstract class HelloWorldHelper
{
	/**
	 * Configure the Linkbar.
	 */
	public static function addSubmenu($submenu) 
	{
		JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
		                         'index.php?option=com_helloworld', $submenu == 'messages');
		JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),
		                         'index.php?option=com_categories&view=categories&extension=com_helloworld',
		                         $submenu == 'categories');
		// set some global property
		$document = JFactory::getDocument();
		$document->addStyleDeclaration('.icon-48-helloworld ' .
		                               '{background-image: url(../media/com_helloworld/images/tux-48x48.png);}');
		if ($submenu == 'categories') 
		{
			$document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION_CATEGORIES'));
		}
	}
}

NOTE: You MUST use your component name (without com_) for the helper file name, or else your submenus won't show in category view.

This function will be automatically called by the com_categories component. Note that it will

  • change the submenu
  • change some css properties (for displaying icons)
  • change the browser title if the submenu is categories
  • change the title and add a preferences button

We have to change the general controller to call this function and modify the component entry point (the .icon-48-helloworld css class is now set in the addSubmenu function)

admin/controller.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controller library
jimport('joomla.application.component.controller');

/**
 * General Controller of HelloWorld component
 */
class HelloWorldController extends JController
{
	/**
	 * display task
	 *
	 * @return void
	 */
	public function display($cachable = false, $urlparams = false)
	{
		// set default view if not set
		$input = JFactory::getApplication()->input;
		$input->set('view', $input->getCmd('view', 'HelloWorlds'));

		// call parent behavior
		parent::display($cachable, $urlparams);

		// Set the submenu
		HelloWorldHelper::addSubmenu('messages');
	}
}

Adding some ACL[edit]

admin/access.xml

<?xml version="1.0" encoding="utf-8" ?>
<access component="com_helloworld">
	<section name="component">
		<action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC" />
		<action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC" />
		<action name="core.create" title="JACTION_CREATE" description="JACTION_CREATE_COMPONENT_DESC" />
		<action name="core.delete" title="JACTION_DELETE" description="JACTION_DELETE_COMPONENT_DESC" />
		<action name="core.edit" title="JACTION_EDIT" description="JACTION_EDIT_COMPONENT_DESC" />
	</section>
	<section name="message">
		<action name="core.delete" title="JACTION_DELETE" description="COM_HELLOWORD_ACCESS_DELETE_DESC" />
		<action name="core.edit" title="JACTION_EDIT" description="COM_OLA_HELLOWORD_EDIT_DESC" />
	</section>
</access>

NOTE: If you don't add this file, buttons "New" "Edit" and ... don't show in category view . for more information read section Adding ACL on top of the page.

admin/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// require helper file
JLoader::register('HelloWorldHelper', dirname(__FILE__) . DS . 'helpers' . DS . 'helloworld.php');

// import joomla controller library
jimport('joomla.application.component.controller');

// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');

// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

// Redirect if set by the controller
$controller->redirect();

Adding some translation strings[edit]

Some strings have to be translated. In the admin/language/en-GB/en-GB.com_helloworld.ini file, put these lines:

admin/language/en-GB/en-GB.com_helloworld.ini

COM_HELLOWORLD="Hello World!"
COM_HELLOWORLD_ADMINISTRATION="HelloWorld - Administration"
COM_HELLOWORLD_ADMINISTRATION_CATEGORIES="HelloWorld - Categories"
COM_HELLOWORLD_HELLOWORLD_CREATING="HelloWorld - Creating"
COM_HELLOWORLD_HELLOWORLD_DETAILS="Details"
COM_HELLOWORLD_HELLOWORLD_EDITING="HelloWorld - Editing"
COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE="Some values are unacceptable"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC="The category the messages belongs to"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL="Category"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC="This message will be displayed"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL="Message"
COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING="Greeting"
COM_HELLOWORLD_HELLOWORLD_HEADING_ID="Id"
COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT="HelloWorld manager: Edit Message"
COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW="HelloWorld manager: New Message"
COM_HELLOWORLD_MANAGER_HELLOWORLDS="HelloWorld manager"
COM_HELLOWORLD_N_ITEMS_DELETED_1="One message deleted"
COM_HELLOWORLD_N_ITEMS_DELETED_MORE="%d messages deleted"
COM_HELLOWORLD_SUBMENU_MESSAGES="Messages"
COM_HELLOWORLD_SUBMENU_CATEGORIES="Categories"

Packaging the component[edit]

Content of your code directory

Create a compressed file of this directory or directly download the archive and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">

	<name>COM_HELLOWORLD</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>November 2009</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is recorded in the components table -->
	<version>0.0.12</version>
	<!-- The description is optional and defaults to the name -->
	<description>COM_HELLOWORLD_DESCRIPTION</description>

	<install> <!-- Runs on install -->
		<sql>
			<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
		</sql>
	</install>
	<uninstall> <!-- Runs on uninstall -->
		<sql>
			<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
		</sql>
	</uninstall>
	<update> <!-- Runs on update; New in 2.5 -->
		<schemas>
			<schemapath type="mysql">sql/updates/mysql</schemapath>
		</schemas>
	</update>

	<!-- Site Main File Copy Section -->
	<!-- Note the folder attribute: This attribute describes the folder
		to copy FROM in the package to install therefore files copied
		in this section are copied from /site/ in the package -->
	<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
		<filename>controller.php</filename>
		<folder>views</folder>
		<folder>models</folder>
		<folder>language</folder>
	</files>

	<media destination="com_helloworld" folder="media">
		<filename>index.html</filename>
		<folder>images</folder>
	</media>

	<administration>
		<!-- Administration Menu Section -->
		<menu img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
		<!-- Administration Main File Copy Section -->
		<!-- Note the folder attribute: This attribute describes the folder
			to copy FROM in the package to install therefore files copied
			in this section are copied from /admin/ in the package -->
		<files folder="admin">
			<!-- Admin Main File Copy Section -->
			<filename>index.html</filename>
			<filename>helloworld.php</filename>
			<filename>controller.php</filename>
           <!-- WARNING <filename>access.php</filename> has an incorrect file extension, should be corrected in the ZIP-file -->
			<filename>access.xml</filename>
			<!-- SQL files section -->
			<folder>sql</folder>
			<!-- tables files section -->
			<folder>tables</folder>
			<!-- models files section -->
			<folder>models</folder>
			<!-- views files section -->
			<folder>views</folder>
			<!-- controllers files section -->
			<folder>controllers</folder>
			<!-- helpers files section -->
			<folder>helpers</folder>
		</files>

		<languages folder="admin">
			<language tag="en-GB">language/en-GB/en-GB.com_helloworld.ini</language>
			<language tag="en-GB">language/en-GB/en-GB.com_helloworld.sys.ini</language>
		</languages>
	</administration>

</extension>

Zips[edit]

Download the zip file for this Part: [1]

Navigate[edit]

Prev: Adding verifications Next: Adding configuration

Contributors[edit]