J3.x:Developing an MVC Component/Adding an alias
From Joomla! Documentation
< J3.x:Developing an MVC Component
Articles in This Series
- Introduction
- Developing a Basic Component
- Adding a View to the Site Part
- Adding a Menu Type to the Site Part
- Adding a Model to the Site Part
- Adding a Variable Request in the Menu Type
- Using the Database
- Basic Backend
- Adding Language Management
- Adding Backend Actions
- Adding Decorations to the Backend
- Adding Verifications
- Adding Categories
- Adding Configuration
- Adding ACL
- Adding an Install/Uninstall/Update Script File
- Adding a Frontend Form
- Adding an Image
- Adding a Map
- Adding AJAX
- Adding an Alias
- Using the Language Filter Facility
- Adding a Modal
- Adding Associations
- Adding Checkout
- Adding Ordering
- Adding Levels
- Adding Versioning
- Adding Tags
- Adding Access
- Adding a Batch Process
- Adding Cache
- Adding a Feed
- Adding an Update Server
- Adding Custom Fields
- Upgrading to Joomla4
This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! Version.
Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in this series).
This tutorial is part of the Developing a MVC Component for Joomla! 3.x tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
The main purpose of this step is to provide an introduction to the Joomla router functionality, which is responsible for taking the URL in the incoming HTTP request, working out which component should be responsible for handling it, and setting up the request parameters (view=abc, id=123, etc.) correctly so that the component can access them via the standard API calls.
We will set up an alias field in each of our helloworld records, and we'll use that alias within the SEF (Search Engine Friendly) URLs for accessing our helloworld messages. To accomplish this we'll define our own custom component router. In this tutorial step the router will be very basic, but we'll make it more sophisticated in the next step when we make our component multilingual. And we'll develop it further whenever we add associations.
Along the way, we'll look at some of the APIs associated with Menus and Menuitems.
A new router was introduced in Joomla 3.8, but this tutorial step just considers the traditional router.
Two videos associated with this step are available, covering Joomla general routing principles and tutorial code description.
Background on SEF URLs
SEF URLs are URLs which make sense to humans and search engines alike. So for example, if instead of a database of helloworld records we had a database of mountains, then an SEF URL would be something like:
www.domainname.net/mountains/himalayas/everest
instead of something like
www.domainname.net/mountains?catid=37&id=52
You can switch SEF URLs on/off through the Joomla Global Configuration, Site tab, SEO settings.
Back in "Adding a variable request in the menu type" we added the ability for an administrator to create a helloworld menu item, and select the particular greeting to be displayed on the web page. So an administrator could add a menu item which had a URL something like
localhost/myjoomlasite/hello-world and when you clicked it, it displayed "Hello World", because he/she had chosen to display that particular greeting on that web page.
However, in our code behind it, we never assumed any specific helloworld record – we just took an id and found the greeting associated with that id. And in fact we can enter a URL
localhost/myjoomlasite/hello-world?id=2
and end up with a different greeting being displayed.
So the id which the administrator chose in the menuitem gets overridden by the id which you enter in the URL parameter. When the Joomla router analyses a URL, it finds first the matching menu item, and any parameters (eg id, category id, view) associated with it. However, it will override those default parameters with any ones which it finds in the URL query string. This is a key point to understand!
However, our URL for getting an alternative message isn't very SEF-like. What we'd rather have is a menu item with a URL like
localhost/myjoomlasite/messages
to which we could add the particular greeting we wanted to display by localhost/myjoomlasite/messages/hello-world
localhost/myjoomlasite/messages/goodbye-world
This is what this step in the tutorial enables us to do.
Functionality
We'll add an alias field to our helloworld record, and functionality within our admin forms for setting and maintaining the alias data. This functionality will be similar to where alias is used within other Joomla components such as com_content, except that we'll force our alias to be unique (but we'll revisit that when we look at including multi-language capability). We'll update our front-end form also to allow a user to enter an alias.
Forcing the alias to be unique will actually cause a problem with the administrator Save as Copy functionality, and we'll have to address that.
We'll provide an equivalent of the article Category List, which will display a list of helloworld records which have a given Category. This will enable us to see the URL format of helloworld messages which haven't got a specific menuitem dedicated to them, and by clicking on the URL we'll be able to access them.
In the previous step we provided a list of helloworld messages via ajax when someone clicked on the Search Here button associated with the map. In this step we'll provide a link for each of those messages, so that a user can click on them to access them.
Finally we'll develop our custom routing functionality to enable the SEF URLs to be generated.
Approach
Database
We'll define our alias field in the same way as core Joomla components. As we'll want our alias field to be populated (not null) and unique, we'll populate the alias field for existing records in our sql update script based on the unique id field.
Admin Management
Managing the alias data will fit naturally into our existing admin framework for managing helloworld records with minimal changes. We just have to make sure that the alias is unique, and doesn't contain characters that aren't allowed in URLs, and we'll handle that by overriding the JTable::check() method.
Save as Copy
When editing a helloworld record, the administrator can click on the Save as Copy button and this will result in the current field values in the form being saved as a new record. However, if the alias field hasn't been changed from the original record then the Save as Copy will result in an error due to the unique index on the alias. While this is strictly correct, it's not very user-friendly, so we'll adopt the same approach as com_content which adds increasing numbers onto the article title and alias until it finds a number which doesn't cause a problem.
Category List View
This will be fairly straightforward, and will be more like revising previous steps in the tutorial.
Once you have developed and installed this new functionality you will have to go into Joomla admin and create a menuitem with alias "messages" pointing to a helloworld category list view. When creating the menuitem, it doesn't matter which category you select. This menuitem is where we'll hang the helloworld greetings, so that if the URL of the menuitem is
localhost/myjoomlasite/messages
then the URLs of our helloworld greetings will be eg
localhost/myjoomlasite/messages/hello-world
localhost/myjoomlasite/messages/goodbye-world
Front-end Form
As the alias is required and has to be unique, we need to update our form to allow the user to enter the alias field as well. This will require changes to our xml form definition, but our existing layout and model code will be fine.
Ajax changes
In our Joomla server code, specifically our helloworld model, with each message returned we'll add the URL of that message, obtained by calling JRoute::_ and passing the helloworld message id.
However, here we encounter a little problem. The problem is that we can get to our map and thus to our ajax call through our menuitem which displays a specific helloworld message, eg
localhost/myjoomlasite/hello-world
If we just added our message id to the JRoute call, then we would end up with URLs such as
localhost/myjoomlasite/hello-world/goodbye-world
which is not really what we want.
This is because whenever it creates an SEF URL JRoute::_ takes as default the menuitem which it is currently on (the Active Menuitem), which in this case is the menuitem with alias hello-world. If we want it to generate a different URL, associated with a different menuitem, then we have to pass it the Itemid of that other menuitem. So in our code we'll have to look through the menuitems on the system for one with an alias of "messages" and pass to JRoute::_ the Itemid of the menuitem we find. (The Itemid is what is in the rightmost ID column when you look at the menuitems in the Joomla admin).
In getting across from our "hello-world" menuitem to our "messages" menuitem we have got 2 possibilities:
- In our server ajax handling code, after we've found the helloworld records in our helloworld model, we could add the "messages" menuitem Itemid to the JRoute::_ call by adding "&Itemid=xxx" to generate the URL for each of those records.
- We could change the URL of our ajax call, so that it ran within the context of the "messages" menuitem. Then when JRoute::_ is called it's already in the right context, and we won't have to pass any menuitem Itemid to it.
In the code below we've opted for the second approach.
Router
We'll develop our custom router code – more about that later.
Data Configuration
Once you have installed this new version you need to create the "messages" menuitem as described above.
Updating the Database
Add the alias field to the database record.
admin/sql/install.mysql.utf8.sql
DROP TABLE IF EXISTS `#__helloworld`;
CREATE TABLE `#__helloworld` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`asset_id` INT(10) NOT NULL DEFAULT '0',
`created` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`greeting` VARCHAR(25) NOT NULL,
`alias` VARCHAR(40) NOT NULL DEFAULT '',
`published` tinyint(4) NOT NULL DEFAULT '1',
`catid` int(11) NOT NULL DEFAULT '0',
`params` VARCHAR(1024) NOT NULL DEFAULT '',
`image` VARCHAR(1024) NOT NULL DEFAULT '',
`latitude` DECIMAL(9,7) NOT NULL DEFAULT 0.0,
`longitude` DECIMAL(10,7) NOT NULL DEFAULT 0.0,
PRIMARY KEY (`id`)
)
ENGINE =MyISAM
AUTO_INCREMENT =0
DEFAULT CHARSET =utf8;
CREATE UNIQUE INDEX `aliasindex` ON `#__helloworld` (`alias`);
INSERT INTO `#__helloworld` (`greeting`,`alias`) VALUES
('Hello World!','hello-world'),
('Goodbye World!','goodbye-world');
New SQL update file: /admin/sql/updates/mysql/0.0.20.sql
ALTER TABLE`#__helloworld` ADD COLUMN `alias` VARCHAR(40) NOT NULL DEFAULT '' AFTER `greeting`;
UPDATE `#__helloworld` AS h1
SET alias = (SELECT CONCAT('id-', ID) FROM (SELECT * FROM `#__helloworld`) AS h2 WHERE h1.id = h2.id);
CREATE UNIQUE INDEX `aliasindex` ON `#__helloworld` (`alias`);
Each updated helloworld record ends up with an alias of "id-" + the id of the record. After you've installed this step of the tutorial you might want to change some of these to have a more meaningful alias.
Admin Management
Our XML definition of the form for editing helloworld records: admin/models/forms/helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<form
addrulepath="/administrator/components/com_helloworld/models/rules"
>
<fieldset
name="details"
label="COM_HELLOWORLD_HELLOWORLD_DETAILS"
>
<field
name="id"
type="hidden"
/>
<field
name="greeting"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
size="40"
class="inputbox validate-greeting"
validate="greeting"
required="true"
default=""
/>
<field
name="alias"
type="text"
label="JFIELD_ALIAS_LABEL"
description="JFIELD_ALIAS_DESC"
hint="JFIELD_ALIAS_PLACEHOLDER"
size="40"
/>
<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>
<field
name="latitude"
type="number"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_LATITUDE_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_LATITUDE_DESC"
min="-90.0"
max="90.0"
class="inputbox"
required="true"
default="0.0"
/>
<field
name="longitude"
type="number"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_LONGITUDE_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_LONGITUDE_DESC"
min="-180.0"
max="180.0"
class="inputbox"
required="true"
default="0.0"
/>
</fieldset>
<fields name="imageinfo">
<fieldset
name="image-info"
label="COM_HELLOWORLD_IMAGE_FIELDS"
>
<field
name="image"
type="media"
preview="tooltip"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_IMAGE_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_IMAGE_DESC" />
<field name="alt"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_ALT_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_ALT_DESC"
size="30"/>
<field name="caption"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTION_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTION_DESC"
size="30"/>
</fieldset>
</fields>
<fields name="params">
<fieldset
name="params"
label="JGLOBAL_FIELDSET_DISPLAY_OPTIONS"
>
<field
name="show_category"
type="list"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
default=""
>
<option value="">JGLOBAL_USE_GLOBAL</option>
<option value="0">JHIDE</option>
<option value="1">JSHOW</option>
</field>
</fieldset>
</fields>
<fieldset
name="accesscontrol"
label="COM_HELLOWORLD_FIELDSET_RULES"
>
<field
name="asset_id"
type="hidden"
filter="unset"
/>
<field
name="rules"
type="rules"
label="COM_HELLOWORLD_FIELD_RULES_LABEL"
filter="rules"
validate="rules"
class="inputbox"
component="com_helloworld"
section="message"
/>
</fieldset>
</form>
Our helloworlds model, for providing the data for our display of helloworld records: admin/models/helloworlds.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* HelloWorldList Model
*
* @since 0.0.1
*/
class HelloWorldModelHelloWorlds extends JModelList
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @see JController
* @since 1.6
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'id',
'greeting',
'author',
'created',
'published'
);
}
parent::__construct($config);
}
/**
* Method to build an SQL query to load the list data.
*
* @return string An SQL query
*/
protected function getListQuery()
{
// Initialize variables.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Create the base select statement.
$query->select('a.id as id, a.greeting as greeting, a.published as published, a.created as created,
a.image as imageInfo, a.latitude as latitude, a.longitude as longitude, a.alias as alias')
->from($db->quoteName('#__helloworld', 'a'));
// Join over the categories.
$query->select($db->quoteName('c.title', 'category_title'))
->join('LEFT', $db->quoteName('#__categories', 'c') . ' ON c.id = a.catid');
// Join with users table to get the username of the author
$query->select($db->quoteName('u.username', 'author'))
->join('LEFT', $db->quoteName('#__users', 'u') . ' ON u.id = a.created_by');
// Filter: like / search
$search = $this->getState('filter.search');
if (!empty($search))
{
$like = $db->quote('%' . $search . '%');
$query->where('greeting LIKE ' . $like);
}
// Filter by published state
$published = $this->getState('filter.published');
if (is_numeric($published))
{
$query->where('a.published = ' . (int) $published);
}
elseif ($published === '')
{
$query->where('(a.published IN (0, 1))');
}
// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering', 'greeting');
$orderDirn = $this->state->get('list.direction', 'asc');
$query->order($db->escape($orderCol) . ' ' . $db->escape($orderDirn));
return $query;
}
}
For our display of helloworld records, we'll show the alias in the same way as it's shown in com_content, namely in small letters under the greeting. admin/views/helloworlds/tmpl/default.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
use Joomla\Registry\Registry;
JHtml::_('formbehavior.chosen', 'select');
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
?>
<form action="index.php?option=com_helloworld&view=helloworlds" method="post" id="adminForm" name="adminForm">
<div id="j-sidebar-container" class="span2">
<?php echo JHtmlSidebar::render(); ?>
</div>
<div id="j-main-container" class="span10">
<div class="row-fluid">
<div class="span6">
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_FILTER'); ?>
<?php
echo JLayoutHelper::render(
'joomla.searchtools.default',
array('view' => $this)
);
?>
</div>
</div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th width="1%"><?php echo JText::_('COM_HELLOWORLD_NUM'); ?></th>
<th width="2%">
<?php echo JHtml::_('grid.checkall'); ?>
</th>
<th width="15%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_HELLOWORLDS_NAME', 'greeting', $listDirn, $listOrder); ?>
</th>
<th width="15%">
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_POSITION'); ?>
</th>
<th width="30%">
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_IMAGE'); ?>
</th>
<th width="15%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_AUTHOR', 'author', $listDirn, $listOrder); ?>
</th>
<th width="15%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_CREATED_DATE', 'created', $listDirn, $listOrder); ?>
</th>
<th width="5%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_PUBLISHED', 'published', $listDirn, $listOrder); ?>
</th>
<th width="2%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_ID', 'id', $listDirn, $listOrder); ?>
</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5">
<?php echo $this->pagination->getListFooter(); ?>
</td>
</tr>
</tfoot>
<tbody>
<?php if (!empty($this->items)) : ?>
<?php foreach ($this->items as $i => $row) :
$link = JRoute::_('index.php?option=com_helloworld&task=helloworld.edit&id=' . $row->id);
$row->image = new Registry;
$row->image->loadString($row->imageInfo);
?>
<tr>
<td><?php echo $this->pagination->getRowOffset($i); ?></td>
<td>
<?php echo JHtml::_('grid.id', $i, $row->id); ?>
</td>
<td>
<a href="<?php echo $link; ?>" title="<?php echo JText::_('COM_HELLOWORLD_EDIT_HELLOWORLD'); ?>">
<?php echo $row->greeting; ?>
</a>
<span class="small break-word">
<?php echo JText::sprintf('JGLOBAL_LIST_ALIAS', $this->escape($row->alias)); ?>
</span>
<div class="small">
<?php echo JText::_('JCATEGORY') . ': ' . $this->escape($row->category_title); ?>
</div>
</td>
<td align="center">
<?php echo "[" . $row->latitude . ", " . $row->longitude . "]"; ?>
</td>
<td align="center">
<?php
$caption = $row->image->get('caption') ? : '' ;
$src = JURI::root() . ($row->image->get('image') ? : '' );
$html = '<p class="hasTooltip" style="display: inline-block" data-html="true" data-toggle="tooltip" data-placement="right" title="<img width=\'100px\' height=\'100px\' src=\'%s\'>">%s</p>';
echo sprintf($html, $src, $caption); ?>
</td>
<td align="center">
<?php echo $row->author; ?>
</td>
<td align="center">
<?php echo substr($row->created, 0, 10); ?>
</td>
<td align="center">
<?php echo JHtml::_('jgrid.published', $row->published, $i, 'helloworlds.', true, 'cb'); ?>
</td>
<td align="center">
<?php echo $row->id; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<input type="hidden" name="task" value=""/>
<input type="hidden" name="boxchecked" value="0"/>
<?php echo JHtml::_('form.token'); ?>
</div>
</form>
When we write anything to the database using JTable the function check() is called before saving the record. Hence we can override this to ensure that our alias field is not blank and has bad characters stripped out using the JFilterOutput::stringURLSafe() method. This will check both records entered via the admin form, and those entered via the front-end form. Our unique index on the helloworld table will ensure that alias values are not duplicated.
admin/tables/helloworld.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
/**
* Hello Table class
*
* @since 0.0.1
*/
class HelloWorldTableHelloWorld extends JTable
{
/**
* Constructor
*
* @param JDatabaseDriver &$db A database connector object
*/
function __construct(&$db)
{
parent::__construct('#__helloworld', 'id', $db);
}
/**
* Overloaded bind function
*
* @param array named array
* @return null|string null is operation was satisfactory, otherwise returns an error
* @see JTable:bind
* @since 1.5
*/
public function bind($array, $ignore = '')
{
if (isset($array['params']) && is_array($array['params']))
{
// Convert the params field to a string.
$parameter = new JRegistry;
$parameter->loadArray($array['params']);
$array['params'] = (string)$parameter;
}
if (isset($array['imageinfo']) && is_array($array['imageinfo']))
{
// Convert the imageinfo array to a string.
$parameter = new JRegistry;
$parameter->loadArray($array['imageinfo']);
$array['image'] = (string)$parameter;
}
// Bind the rules.
if (isset($array['rules']) && is_array($array['rules']))
{
$rules = new JAccessRules($array['rules']);
$this->setRules($rules);
}
return parent::bind($array, $ignore);
}
/**
* Method to compute the default name of the asset.
* The default name is in the form `table_name.id`
* where id is the value of the primary key of the table.
*
* @return string
* @since 2.5
*/
protected function _getAssetName()
{
$k = $this->_tbl_key;
return 'com_helloworld.helloworld.'.(int) $this->$k;
}
/**
* Method to return the title to use for the asset table.
*
* @return string
* @since 2.5
*/
protected function _getAssetTitle()
{
return $this->greeting;
}
/**
* Method to get the asset-parent-id of the item
*
* @return int
*/
protected function _getAssetParentId(JTable $table = NULL, $id = NULL)
{
// We will retrieve the parent-asset from the Asset-table
$assetParent = JTable::getInstance('Asset');
// Default: if no asset-parent can be found we take the global asset
$assetParentId = $assetParent->getRootId();
// Find the parent-asset
if (($this->catid)&& !empty($this->catid))
{
// The item has a category as asset-parent
$assetParent->loadByName('com_helloworld.category.' . (int) $this->catid);
}
else
{
// The item has the component as asset-parent
$assetParent->loadByName('com_helloworld');
}
// Return the found asset-parent-id
if ($assetParent->id)
{
$assetParentId=$assetParent->id;
}
return $assetParentId;
}
public function check()
{
$this->alias = trim($this->alias);
if (empty($this->alias))
{
$this->alias = $this->greeting;
}
$this->alias = JFilterOutput::stringURLSafe($this->alias);
return true;
}
}
Save as Copy
To understand what we need to do, try clicking on Save as Copy when editing an article. Joomla creates a new article, appending (1) to the title and "-1" to the alias.
Close the new article and go back and edit the original article, then click on Save as Copy again. This time Joomla appends (2) to the title and "-2" to the alias, skipping over the (1) record which already exists. To support this adding of numbers to the title and alias Joomla has a utility function called generateNewTitle() which keeps increasing the number until it finds a combination where the alias plus category combination does not already exist in the database. This is the function we will use in our updated administrator helloworld model, and at the same time we'll introduce some validation of the catid uploaded from the form. (This won't 100% fix the issue, but it will when, in the next step, we change our unique index to be alias+category, rather than just alias).
We'll do this by overriding save() in our model. Why save() and not save2copy(), seeing as the task in this case is set to save2copy? If you look at the code of JControllerForm you'll see
$this->registerTask('save2copy', 'save');
which means that the edit form submission with task=save2copy results in the save() method being run in the controller, which in turn runs the save() method in the model. Different types of save function all run the save() method in the controller and model, so if we want to insert any code specific to save2copy we have to check what the original task variable was set to.
admin/models/helloworld.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
use Joomla\Registry\Registry;
/**
* HelloWorld Model
*
* @since 0.0.1
*/
class HelloWorldModelHelloWorld extends JModelAdmin
{
/**
* Method to override getItem to allow us to convert the JSON-encoded image information
* in the database record into an array for subsequent prefilling of the edit form
*/
public function getItem($pk = null)
{
$item = parent::getItem($pk);
if ($item AND property_exists($item, 'image'))
{
$registry = new Registry($item->image);
$item->imageinfo = $registry->toArray();
}
return $item;
}
/**
* Method to get a table object, load it if necessary.
*
* @param string $type The table name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JTable A JTable object
*
* @since 1.6
*/
public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/**
* Method to get the record form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return mixed A JForm object on success, false on failure
*
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm(
'com_helloworld.helloworld',
'helloworld',
array(
'control' => 'jform',
'load_data' => $loadData
)
);
if (empty($form))
{
return false;
}
return $form;
}
/**
* Method to get the script that have to be included on the form
*
* @return string Script files
*/
public function getScript()
{
return 'administrator/components/com_helloworld/models/forms/helloworld.js';
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*
* @since 1.6
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState(
'com_helloworld.edit.helloworld.data',
array()
);
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
/**
* Method to override the JModelAdmin save() function to handle Save as Copy correctly
*
* @param The helloworld record data submitted from the form.
*
* @return parent::save() return value
*/
public function save($data)
{
$input = JFactory::getApplication()->input;
JLoader::register('CategoriesHelper', JPATH_ADMINISTRATOR . '/components/com_categories/helpers/categories.php');
// Validate the category id
// validateCategoryId() returns 0 if the catid can't be found
if ((int) $data['catid'] > 0)
{
$data['catid'] = CategoriesHelper::validateCategoryId($data['catid'], 'com_helloworld');
}
// Alter the greeting and alias for save as copy
if ($input->get('task') == 'save2copy')
{
$origTable = clone $this->getTable();
$origTable->load($input->getInt('id'));
if ($data['greeting'] == $origTable->greeting)
{
list($greeting, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['greeting']);
$data['greeting'] = $greeting;
$data['alias'] = $alias;
}
else
{
if ($data['alias'] == $origTable->alias)
{
$data['alias'] = '';
}
}
// standard Joomla practice is to set the new record as unpublished
$data['published'] = 0;
}
return parent::save($data);
}
/**
* Method to check if it's OK to delete a message. Overrides JModelAdmin::canDelete
*/
protected function canDelete($record)
{
if( !empty( $record->id ) )
{
return JFactory::getUser()->authorise( "core.delete", "com_helloworld.helloworld." . $record->id );
}
}
}
The addition of numbers (1), (2), etc to our helloworld greeting causes us a problem, because we've defined the greeting to contain letters only. So we'll relax that condition, and allow it to contain anything except an asterisk. We have to change both the client javascript validation and the server php validation from what we specified in Adding Verifications.
admin/models/rules/greeting.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* Form Rule class for the Joomla Framework.
*/
class JFormRuleGreeting extends JFormRule
{
/**
* The regular expression.
*
* @access protected
* @var string
* @since 2.5
*/
protected $regex = '^[^\*]+$';
}
admin/models/forms/helloworld.js
jQuery(function() {
document.formvalidator.setHandler('greeting',
function (value) {
regex=/^[^\*]+$/;
return regex.test(value);
});
});
Category List View
The Category List view is similar to a front-end version of our admin Helloworlds view which displays the list of helloworld messages. So our code is similar to what we have there. The controller just is required to display the view, so there's no change necessary, but we will need:
- a new view – which we'll put in a new site category folder
- a new associated layout file
- a new associated model
- an XML file in the layout directory – for the menu option, so that the administrator can create a menuitem pointing to our category list
- an XML file associated with the filter form
As we're creating new folders we should remember to put dummy index.html files in each.
Overall there's nothing really new here which hasn't been covered in previous tutorial steps.
Our new Category List view file: site/views/category/view.html.php
<?php
/*
* View file for the view which displays a list of helloworld messages in a given category
*/
defined('_JEXEC') or die;
class HelloworldViewCategory extends JViewLegacy
{
public function display($tpl = null)
{
$this->items = $this->get('Items');
$this->pagination = $this->get('Pagination');
$this->state = $this->get('State');
$this->filterForm = $this->get('FilterForm');
$this->activeFilters = $this->get('ActiveFilters');
parent::display($tpl);
}
}
Our new layout file, in which we display the greeting, the alias, and what we get as a URL from JRoute::_ when we pass the parameters as shown below.
site/views/category/tmpl/default.php
<?php
/**
* Layout file for displaying helloworld messages belonging to a given category
*/
defined('_JEXEC') or die;
JHtml::_('formbehavior.chosen', 'select');
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
?>
<form action="#" method="post" id="adminForm" name="adminForm">
<div id="j-main-container" class="span10">
<div class="row-fluid">
<div class="span10">
<?php
echo JLayoutHelper::render(
'joomla.searchtools.default',
array('view' => $this, 'searchButton' => false)
);
?>
</div>
</div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th width="5%"><?php echo JText::_('JGLOBAL_NUM'); ?></th>
<th width="20%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL', 'greeting', $listDirn, $listOrder); ?>
</th>
<th width="20%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_HELLOWORLD_ALIAS_LABEL', 'alias', $listDirn, $listOrder); ?>
</th>
<th width="20%">
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_FIELD_URL_LABEL'); ?>
</th>
<th width="5%">
<?php echo JHtml::_('searchtools.sort', 'JGLOBAL_FIELD_ID_LABEL', 'id', $listDirn, $listOrder); ?>
</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5">
<?php echo $this->pagination->getListFooter(); ?>
</td>
</tr>
</tfoot>
<tbody>
<?php if (!empty($this->items)) : ?>
<?php foreach ($this->items as $i => $row) :
$url = JRoute::_('index.php?option=com_helloworld&view=helloworld&id=' . $row->id);
?>
<tr>
<td align="center"><?php echo $this->pagination->getRowOffset($i); ?></td>
<td align="center"><?php echo $row->greeting; ?></td>
<td align="center"><?php echo $row->alias; ?></td>
<td align="center"><a href="<?php echo $url; ?>"><?php echo $url; ?></a></td>
<td align="center"><?php echo $row->id; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</form>
Our new model
site/models/category.php
<?php
/**
* Model for displaying the helloworld messages in a given category
*/
defined('_JEXEC') or die;
class HelloworldModelCategory extends JModelList
{
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'id',
'greeting',
'alias',
);
}
parent::__construct($config);
}
protected function populateState($ordering = null, $direction = null)
{
parent::populateState($ordering, $direction);
$app = JFactory::getApplication('site');
$catid = $app->input->getInt('id');
$this->setState('category.id', $catid);
}
protected function getListQuery()
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$catid = $this->getState('category.id');
$query->select('id, greeting, alias')
->from($db->quoteName('#__helloworld'))
->where('catid = ' . $catid);
$orderCol = $this->state->get('list.ordering', 'greeting');
$orderDirn = $this->state->get('list.direction', 'asc');
$query->order($db->escape($orderCol) . ' ' . $db->escape($orderDirn));
return $query;
}
}
Our new XML file for the Category List menuitem:
site/views/category/tmpl/default.xml
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_HELLOWORLD_CATEGORY_VIEW_TITLE">
<message>
<![CDATA[COM_HELLOWORLD_CATEGORY_VIEW_DESC]]>
</message>
</layout>
<!-- Add fields to the request variables for the layout. -->
<fields name="request">
<fieldset name="request"
addfieldpath="/administrator/components/com_helloworld/models/fields"
>
<field
name="id"
type="category"
label="JGLOBAL_CHOOSE_CATEGORY_LABEL"
description="JGLOBAL_CHOOSE_CATEGORY_DESC"
extension="com_helloworld"
/>
</fieldset>
</fields>
</metadata>
Our definition of the filter fields we want to have on this view:
site/models/forms/filter_category.xml
<?xml version="1.0" encoding="utf-8"?>
<form>
<fields name="list">
<field
name="fullordering"
type="list"
onchange="this.form.submit();"
default="greeting ASC"
>
<option value="">COM_HELLOWORLD_SORT_BY</option>
<option value="greeting ASC">COM_HELLOWORLD_GREETING_ASC</option>
<option value="greeting DESC">COM_HELLOWORLD_GREETING_DESC</option>
<option value="id ASC">COM_HELLOWORLD_ID_ASC</option>
<option value="id DESC">COM_HELLOWORLD_ID_DESC</option>
<option value="alias ASC">COM_HELLOWORLD_ALIAS_ASC</option>
<option value="alias DESC">COM_HELLOWORLD_ALIAS_DESC</option>
</field>
<field
name="limit"
type="limitbox"
class="input-mini"
default="10"
onchange="this.form.submit();"
/>
</fields>
</form>
Front-end Form
Our updated xml form definition file: site/models/forms/add-form.xml
<?xml version="1.0" encoding="utf-8"?>
<form
addrulepath="/administrator/components/com_helloworld/models/rules"
>
<fieldset
name="details"
label="COM_HELLOWORLD_HELLOWORLD_DETAILS"
>
<field
name="id"
type="hidden"
/>
<field
name="greeting"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
size="40"
class="inputbox"
validate="greeting"
required="true"
hint="COM_HELLOWORLD_HELLOWORLD_GREETING_HINT"
default=""
/>
<field
name="alias"
type="text"
label="JFIELD_ALIAS_LABEL"
description="JFIELD_ALIAS_DESC"
hint="JFIELD_ALIAS_PLACEHOLDER"
size="40"
/>
<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>
<fields name="imageinfo" label="COM_HELLOWORLD_HELLOWORLD_IMAGE_LABEL">
<field
name="image"
type="file"
label="COM_HELLOWORLD_HELLOWORLD_PICTURE_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_PICTURE_DESC"
accept="image/*"
>
</field>
<field
name="caption"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_CAPTION_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_CAPTION_DESC"
size="40"
class="inputbox"
>
</field>
<field
name="alt"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_ALTTEXT_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_ALTTEXT_DESC"
size="40"
class="inputbox"
>
</field>
</fields>
<field
name="message"
type="textarea"
rows="5"
cols="80"
label="COM_HELLOWORLD_HELLOWORLD_MESSAGE_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_MESSAGE_DESC"
hint="COM_HELLOWORLD_HELLOWORLD_MESSAGE_HINT"
required="true"
>
</field>
<field
name="captcha"
type="captcha"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTCHA_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTCHA_DESC"
validate="captcha"
>
</field>
<fields name="params">
<field
name="show_category"
type="list"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
default=""
useglobal="true"
>
<option value="0">JHIDE</option>
<option value="1">JSHOW</option>
</field>
</fields>
</fieldset>
</form>
Ajax Changes
In our javascript file we'll now expect the Ajax URL to be provided from the server: media/js/openstreetmap.js
var map;
var ajaxurl;
jQuery(document).ready(function() {
// get the data passed from Joomla PHP
// params is a Javascript object with properties for the map display:
// centre latitude, centre longitude and zoom, and the helloworld greeting
const params = Joomla.getOptions('params');
ajaxurl = params.ajaxurl;
// We'll use OpenLayers to draw the map (http://openlayers.org/)
// Openlayers uses an x,y coordinate system for positions
// We need to convert our lat/long into an x,y pair which is relative
// to the map projection we're using, viz Spherical Mercator WGS 84
const x = parseFloat(params.longitude);
const y = parseFloat(params.latitude);
const mapCentre = ol.proj.fromLonLat([x, y]); // Spherical Mercator is assumed by default
// To draw a map, Openlayers needs:
// 1. a target HTML element into which the map is put
// 2. a map layer, which can be eg a Vector layer with details of polygons for
// country boundaries, lines for roads, etc, or a Tile layer, with individual
// .png files for each map tile (256 by 256 pixel square).
// 3. a view, specifying the 2D projection of the map (default Spherical Mercator),
// map centre coordinates and zoom level
map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ // we'll get the tiles from the OSM server
source: new ol.source.OSM()
})
],
view: new ol.View({ // default is Spherical Mercator projection
center: mapCentre,
zoom: params.zoom
})
});
// Now we add a marker for our Helloworld position
// To do that, we specify it as a Point Feature, and we add styling
// to define how that Feature is presented on the map
var helloworldPoint = new ol.Feature({geometry: new ol.geom.Point(mapCentre)});
// we'll define the style as a red 5 point star with blue edging
const redFill = new ol.style.Fill({
color: 'red'
});
const blueStroke = new ol.style.Stroke({
color: 'blue',
width: 3
});
const star = new ol.style.RegularShape({
fill: redFill,
stroke: blueStroke,
points: 5,
radius1: 20, // outer radius of star
radius2: 10, // inner radius of star
})
helloworldPoint.setStyle(new ol.style.Style({
image: star
}));
// now we add the feature to the map via a Vector source and Vector layer
const vectorSource = new ol.source.Vector({});
vectorSource.addFeature(helloworldPoint);
const vector = new ol.layer.Vector({
source: vectorSource
});
map.addLayer(vector);
// If a user clicks on the star, then we'll show the helloworld greeting
// The greeting will go into another HTML element, with id="greeting-container"
// and this will be shown as an Overlay on the map
var overlay = new ol.Overlay({
element: document.getElementById('greeting-container'),
});
map.addOverlay(overlay);
// Finally we add the onclick listener to display the greeting when the star is clicked
// The way this works is that the onclick listener is attached to the map,
// and then it works out which Feature or Features have been hit
map.on('click', function(e) {
let markup = '';
let position;
map.forEachFeatureAtPixel(e.pixel, function(feature) { // for each Feature hit
markup = params.greeting;
position = feature.getGeometry().getCoordinates();
}, {hitTolerance: 5}); // tolerance of 5 pixels
if (markup) {
document.getElementById('greeting-container').innerHTML = markup;
overlay.setPosition(position);
} else {
overlay.setPosition(); // this hides it, if we click elsewhere
}
});
});
function getMapBounds(){
var mercatorMapbounds = map.getView().calculateExtent(map.getSize());
var latlngMapbounds = ol.proj.transformExtent(mercatorMapbounds,'EPSG:3857','EPSG:4326');
return { minlat: latlngMapbounds[1],
maxlat: latlngMapbounds[3],
minlng: latlngMapbounds[0],
maxlng: latlngMapbounds[2] }
}
function searchHere() {
var mapBounds = getMapBounds();
var token = jQuery("#token").attr("name");
jQuery.ajax({
url: ajaxurl,
data: { [token]: "1", task: "mapsearch", view: "helloworld", format: "json", mapBounds: mapBounds },
success: function(result, status, xhr) { displaySearchResults(result); },
error: function() { console.log('ajax call failed'); },
});
}
function displaySearchResults(result) {
if (result.success) {
var html = "";
for (var i=0; i<result.data.length; i++) {
html += '<p><a href="' + result.data[i].url + '">' +
result.data[i].greeting + '</a>' +
" @ " + result.data[i].latitude +
", " + result.data[i].longitude + "</p>";
}
jQuery("#searchresults").html(html);
} else {
var msg = result.message;
if ((result.messages) && (result.messages.error)) {
for (var j=0; j<result.messages.error.length; j++) {
msg += "<br/>" + result.messages.error[j];
}
}
jQuery("#searchresults").html(msg);
}
}
We'll have to provide this ajax URL from the model: site/models/helloworld.php
<?php
/**
* @package Joomla.Administrator
* @subpackage com_helloworld
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JLoader::register('HelloworldHelperRoute', JPATH_ROOT . '/components/com_helloworld/helpers/route.php');
/**
* HelloWorld Model
*
* @since 0.0.1
*/
class HelloWorldModelHelloWorld extends JModelItem
{
/**
* @var object item
*/
protected $item;
/**
* Method to auto-populate the model state.
*
* This method should only be called once per instantiation and is designed
* to be called on the first call to the getState() method unless the model
* configuration flag to ignore the request is set.
*
* Note. Calling getState in this method will result in recursion.
*
* @return void
* @since 2.5
*/
protected function populateState()
{
// Get the message id
$jinput = JFactory::getApplication()->input;
$id = $jinput->get('id', 1, 'INT');
$this->setState('message.id', $id);
// Load the parameters.
$this->setState('params', JFactory::getApplication()->getParams());
parent::populateState();
}
/**
* Method to get a table object, load it if necessary.
*
* @param string $type The table name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JTable A JTable object
*
* @since 1.6
*/
public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/**
* Get the message
* @return object The message to be displayed to the user
*/
public function getItem()
{
if (!isset($this->item))
{
$id = $this->getState('message.id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('h.greeting, h.params, h.image as image, c.title as category, h.latitude as latitude, h.longitude as longitude')
->from('#__helloworld as h')
->leftJoin('#__categories as c ON h.catid=c.id')
->where('h.id=' . (int)$id);
$db->setQuery((string)$query);
if ($this->item = $db->loadObject())
{
// Load the JSON string
$params = new JRegistry;
$params->loadString($this->item->params, 'JSON');
$this->item->params = $params;
// Merge global params with item params
$params = clone $this->getState('params');
$params->merge($this->item->params);
$this->item->params = $params;
// Convert the JSON-encoded image info into an array
$image = new JRegistry;
$image->loadString($this->item->image, 'JSON');
$this->item->imageDetails = $image;
}
}
return $this->item;
}
public function getMapParams()
{
if ($this->item)
{
$url = HelloworldHelperRoute::getAjaxURL();
$this->mapParams = array(
'latitude' => $this->item->latitude,
'longitude' => $this->item->longitude,
'zoom' => 10,
'greeting' => $this->item->greeting,
'ajaxurl' => $url
);
return $this->mapParams;
}
else
{
throw new Exception('No helloworld details available for map', 500);
}
}
public function getMapSearchResults($mapbounds)
{
try
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('h.id, h.greeting, h.latitude, h.longitude')
->from('#__helloworld as h')
->where('h.latitude > ' . $mapbounds['minlat'] .
' AND h.latitude < ' . $mapbounds['maxlat'] .
' AND h.longitude > ' . $mapbounds['minlng'] .
' AND h.longitude < ' . $mapbounds['maxlng']);
$db->setQuery($query);
$results = $db->loadObjectList();
}
catch (Exception $e)
{
$msg = $e->getMessage();
JFactory::getApplication()->enqueueMessage($msg, 'error');
$results = null;
}
for ($i = 0; $i < count($results); $i++)
{
$results[$i]->url = JRoute::_('index.php?option=com_helloworld&view=helloworld&id=' . $results[$i]->id);
}
return $results;
}
}
But we put the complex functionality for finding the Ajax URL into a separate helper file, in a new helpers folder (into which we must put the usual dummy index.html). In the code below:
$sitemenu= $app->getMenu(); returns the menu structure for the whole site
$thismenuitem = $sitemenu->getActive(); returns the menuitem structure for the menuitem we're currently on, ie the menuitem which the Joomla router selected which got us to our code here.
$mainmenuitems = $sitemenu->getItems('menutype', 'mainmenu'); returns the subset of menuitems which are attached to the Main Menu, ie where the menutype property = "mainmenu".
If we're already on the "messages" menuitem, then we can pass a null URL to our Javascript. When in our Javascript we pass a null URL to the jQuery Ajax call, jQuery will just send the HTTP request to the current URL.
site/helpers/route.php
<?php
defined('_JEXEC') or die;
/**
* Helloworld Component Helper file for generating the URL Routes
*
*/
class HelloworldHelperRoute
{
/**
* When the Helloworld message is displayed then there is also shown a map with a Search Here button.
* This function generates the URL which the Ajax call will use to perform the search.
*
*/
public static function getAjaxURL()
{
$app = JFactory::getApplication();
$sitemenu= $app->getMenu();
$thismenuitem = $sitemenu->getActive();
// if we haven't got an active menuitem, or we're currently on the messages menuitem then just stay there
if (!$thismenuitem || $thismenuitem->alias == "messages")
{
return null;
}
$mainmenuitems = $sitemenu->getItems('menutype', 'mainmenu');
foreach ($mainmenuitems as $menuitem)
{
if ($menuitem->alias == "messages")
{
$itemid = $menuitem->id;
$url = JRoute::_("index.php?Itemid=$itemid&view=helloworld&format=json");
return $url;
}
}
return null;
}
}
Router
Finally, we're now able to consider our router code. First let's understand a little about how Joomla's routing works, by looking at how it would parse an SEF URL of the form: http://www.domainname.net/en/menu1/submenu-a/segment1/segment2
The aim of the router is to direct this request to the right component, and have defined:
- the language
- the formatting options, etc to apply to this web page
- the parameters such as id, format (eg html/json), view etc
First of all the router will remove the domain name part, as it refers to the whole site, and doesn't figure in the routing. So we're left with a path:
en/menu1/submenu-a/segment1/segment2
Now the router applies varies rules to try to parse the segments of the URL, parsing from the left hand side. As segments are matched the router builds up the configuration parameters and removes those matched segments from the URL, before applying the next rule.
If the site is multilingual then it will run the rule to parse the language. In this case it will find 'en' and will set the language to en-GB, and remove it from the path, leaving:
menu1/submenu-a/segment1/segment2
Now the router will run a rule to try to find a matching menu item. It will get all the menuitems on the site, and compare the route of each menu item with the leftmost segments of the path. In this example it might find 2 matching ones – one which matches "menu1" and another which matches "menu1/submenu-a" – in which case it will take the one with the longest match.
Now that it's found the menu item, it knows the component, component parameters, and also the formatting and other options which are associated with this menu item. We're now left with
segment1/segment2
It's at this point that the router turns to the component to parse the remaining segments.
Previously it will have got a list of all of the components on the site, and checked each component directory looking for a file called router.php with class functions in it which implemented the router interface, namely build(), parse() and preprocess() functions. If it found one, then it set that class as the component router, otherwise it just set a default legacy component router.
To parse the remaining segments it calls the parse() function of the component router, and passes in an array of the remaining segments. What it wants in return is a list of the query parameters (eg id=xxx, view=yyy), and it will use these to overwrite any equivalent parameters which were set against the menuitem.
Finally, it sets the HTTP Request parameters to the ones it has found, so that when the component is run it can use $input->get() to access them.
Looking at the converse side, when we call JRoute::_ we pass to it a query string like "id=52&view=helloworld". If SEF URLs are enabled on the site, then its job is to generate the segments of the SEF URL, and it does this by:
- checking if the language is multilingual, and if so adding the appropriate language segment
- adding the route of the menuitem passed in the "Itemid=xxx" part of the query string, or if no Itemid passed, then it will take the current ("active") menu item it's on
- if there are still query parameters which don't match those of the menu item, then it will call the component router's build() function, passing an array of those query parameters and expecting back the segments of the URL which it should append.
The third function preprocess() in our custom router is one which is called at the outset to allow the code to set up any initial data, but we won't use it.
Our build() and parse() functions are pretty much as simple as they can be: our build() takes an id and returns the associated alias, and our parse() takes an alias and returns the associated id.
site/router.php
<?php
defined('_JEXEC') or die;
class HelloworldRouter implements JComponentRouterInterface
{
public function build(&$query)
{
$segments = array();
if (isset($query['id']))
{
$db = JFactory::getDbo();
$qry = $db->getQuery(true);
$qry->select('alias');
$qry->from('#__helloworld');
$qry->where('id = ' . $db->quote($query['id']));
$db->setQuery($qry);
$alias = $db->loadResult();
$segments[] = $alias;
unset($query['id']);
}
unset($query['view']);
return $segments;
}
public function parse(&$segments)
{
$vars = array();
$db = JFactory::getDbo();
$qry = $db->getQuery(true);
$qry->select('id');
$qry->from('#__helloworld');
$qry->where('alias = ' . $db->quote($segments[0]));
$db->setQuery($qry);
$id = $db->loadResult();
if(!empty($id))
{
$vars['id'] = $id;
$vars['view'] = 'helloworld';
}
return $vars;
}
public function preprocess($query)
{
return $query;
}
}
Updated Language Strings
admin/language/en-GB/en-GB.com_helloworld.sys.ini
; Joomla! Project
; Copyright (C) 2005 - 2018 Open Source Matters. All rights reserved.
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
; Note : All ini files need to be saved as UTF-8
COM_HELLOWORLD="Hello World!"
COM_HELLOWORLD_DESCRIPTION="This is the Hello World description"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE="Hello World"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC="This view displays a selected message"
COM_HELLOWORLD_ADD_VIEW_TITLE="Hello World form"
COM_HELLOWORLD_ADD_VIEW_DESC="This displays a form to allow the user to enter a new message"
COM_HELLOWORLD_CATEGORY_VIEW_TITLE="Category List"
COM_HELLOWORLD_CATEGORY_VIEW_DESC="This displays the messages in the selected category"
COM_HELLOWORLD_INSTALL_TEXT="HelloWorld Install script"
COM_HELLOWORLD_MENU="Hello World!"
COM_HELLOWORLD_POSTFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld postflight discover install script"
COM_HELLOWORLD_POSTFLIGHT_INSTALL_TEXT="HelloWorld postflight install script"
COM_HELLOWORLD_POSTFLIGHT_UNINSTALL_TEXT="HelloWorld postflight uninstall script"
COM_HELLOWORLD_POSTFLIGHT_UPDATE_TEXT="HelloWorld postflight update script"
COM_HELLOWORLD_PREFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld preflight discover install script"
COM_HELLOWORLD_PREFLIGHT_INSTALL_TEXT="HelloWorld preflight install script"
COM_HELLOWORLD_PREFLIGHT_UNINSTALL_TEXT="HelloWorld preflight uninstall script"
COM_HELLOWORLD_PREFLIGHT_UPDATE_TEXT="HelloWorld preflight update script"
COM_HELLOWORLD_UNINSTALL_TEXT="HelloWorld Uninstall script"
COM_HELLOWORLD_UPDATE_TEXT="HelloWorld Update script. HelloWorld now updated to version %s."
site/language/en-GB/en-GB.com_helloworld.ini
; add new message form
COM_HELLOWORLD_LEGEND_DETAILS="New Helloworld Message Details"
COM_HELLOWORLD_HELLOWORLD_CREATING="Add message"
COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE="Sorry, you have an error"
COM_HELLOWORLD_HELLOWORLD_DETAILS="Message details"
COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL="Greeting"
COM_HELLOWORLD_HELLOWORLD_GREETING_DESC="Please specify the greeting to add"
COM_HELLOWORLD_HELLOWORLD_GREETING_HINT="Any characters allowed"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL="Category"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC="Please select the associated category"
COM_HELLOWORLD_HELLOWORLD_MESSAGE_LABEL="Reason"
COM_HELLOWORLD_HELLOWORLD_MESSAGE_DESC="Please say why you're adding this greeting"
COM_HELLOWORLD_HELLOWORLD_MESSAGE_HINT="No HTML tags!"
COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTCHA_LABEL="Spam protection"
COM_HELLOWORLD_HELLOWORLD_FIELD_CAPTCHA_DESC="Prove you're a real person!"
COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL="Display category or not?"
COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC="Select if you want the category displayed too"
COM_HELLOWORLD_HELLOWORLD_IMAGE_LABEL="Image information"
COM_HELLOWORLD_HELLOWORLD_PICTURE_LABEL="Image file to upload"
COM_HELLOWORLD_HELLOWORLD_PICTURE_DESC="Select the file with the image to upload"
COM_HELLOWORLD_HELLOWORLD_CAPTION_LABEL="Caption"
COM_HELLOWORLD_HELLOWORLD_CAPTION_DESC="Text to use as a caption for the image"
COM_HELLOWORLD_HELLOWORLD_ALTTEXT_LABEL="Alt text"
COM_HELLOWORLD_HELLOWORLD_ALTTEXT_DESC="Text to display if image cannot be shown"
; save and cancel confirmation messages
COM_HELLOWORLD_ADD_SUCCESSFUL="New greeting successfully saved"
COM_HELLOWORLD_ADD_CANCELLED="New greeting cancelled ok"
; file upload error conditions
COM_HELLOWORLD_ERROR_FILEUPLOAD="PHP Error %s encountered when uploading file"
COM_HELLOWORLD_ERROR_FILETOOLARGE="Upload file exceeds max size configured in Joomla"
COM_HELLOWORLD_ERROR_BADFILENAME="Upload file has an invalid filename"
COM_HELLOWORLD_ERROR_FILE_EXISTS="Upload file already exists"
COM_HELLOWORLD_ERROR_UNABLE_TO_UPLOAD_FILE="Error creating uploaded file"
; helloworld greeting page
COM_HELLOWORLD_SEARCH_HERE_BUTTON="Search here"
; Ajax handling errors
COM_HELLOWORLD_ERROR_NO_RECORDS="Didn't get any records"
COM_HELLOWORLD_ERROR_NO_MAP_BOUNDS="Error: no map bounds"
; category view, search and ordering fields and headings
COM_HELLOWORLD_SORT_BY="Sort by ..."
COM_HELLOWORLD_GREETING_ASC="Greeting asc"
COM_HELLOWORLD_GREETING_DESC="Greeting desc"
COM_HELLOWORLD_ID_ASC="id asc"
COM_HELLOWORLD_ID_DESC="id desc"
COM_HELLOWORLD_ALIAS_ASC="alias asc"
COM_HELLOWORLD_ALIAS_DESC="alias desc"
COM_HELLOWORLD_HELLOWORLD_ALIAS_LABEL="Alias"
COM_HELLOWORLD_HELLOWORLD_FIELD_URL_LABEL="URL"
Packaging the Component
Contents of your code directory. Each file link below takes you to the step in the tutorial which has the latest version of that source code file.
- helloworld.xml
- script.php
- site/router.php
- site/helloworld.php
- site/index.html
- site/controller.php
- site/controllers/helloworld.php
- site/views/index.html
- site/views/helloworld/index.html
- site/views/helloworld/view.html.php
- site/views/helloworld/view.json.php
- site/views/helloworld/tmpl/index.html
- site/views/helloworld/tmpl/default.xml
- site/views/helloworld/tmpl/default.php
- site/views/form/index.html
- site/views/form/view.html.php
- site/views/form/tmpl/index.html
- site/views/form/tmpl/edit.php
- site/views/form/tmpl/edit.xml
- site/views/category/index.html
- site/views/category/view.html.php
- site/views/category/tmpl/index.html
- site/views/category/tmpl/default.php
- site/views/category/tmpl/default.xml
- site/models/index.html
- site/models/helloworld.php
- site/models/form.php
- site/models/category.php
- site/models/forms/index.html
- site/models/forms/add-form.xml
- site/models/forms/filter_category.xml
- site/language/index.html
- site/language/en-GB/index.html
- site/language/en-GB/en-GB.com_helloworld.ini
- site/helpers/index.html
- site/helpers/route.php
- admin/index.html
- admin/helloworld.php
- admin/config.xml
- admin/controller.php
- admin/access.xml
- admin/helpers/helloworld.php
- admin/helpers/index.html
- admin/sql/index.html
- admin/sql/install.mysql.utf8.sql
- admin/sql/uninstall.mysql.utf8.sql
- admin/sql/updates/index.html
- admin/sql/updates/mysql/index.html
- admin/sql/updates/mysql/0.0.1.sql
- admin/sql/updates/mysql/0.0.6.sql
- admin/sql/updates/mysql/0.0.12.sql
- admin/sql/updates/mysql/0.0.13.sql
- admin/sql/updates/mysql/0.0.14.sql
- admin/sql/updates/mysql/0.0.16.sql
- admin/sql/updates/mysql/0.0.17.sql
- admin/sql/updates/mysql/0.0.18.sql
- admin/sql/updates/mysql/0.0.20.sql
- admin/models/index.html
- admin/models/fields/index.html
- admin/models/fields/helloworld.php
- admin/models/helloworlds.php
- admin/models/helloworld.php
- admin/models/forms/filter_helloworlds.xml
- admin/models/forms/index.html
- admin/models/forms/helloworld.js
- admin/models/forms/helloworld.xml
- admin/models/rules/greeting.php
- admin/models/rules/index.html
- admin/controllers/helloworld.php
- admin/controllers/helloworlds.php
- admin/controllers/index.html
- admin/views/index.html
- admin/views/helloworld/index.html
- admin/views/helloworld/view.html.php
- admin/views/helloworld/tmpl/index.html
- admin/views/helloworld/tmpl/edit.php
- admin/views/helloworld/submitbutton.js
- admin/views/helloworlds/index.html
- admin/views/helloworlds/view.html.php
- admin/views/helloworlds/tmpl/index.html
- admin/views/helloworlds/tmpl/default.php
- admin/tables/index.html
- admin/tables/helloworld.php
- admin/language/index.html
- admin/language/en-GB/index.html
- admin/language/en-GB/en-GB.com_helloworld.ini
- admin/language/en-GB/en-GB.com_helloworld.sys.ini
- media/index.html
- media/images/index.html
- media/images/tux-16x16.png
- media/images/tux-48x48.png
- media/js/index.html
- media/js/openstreetmap.js
- media/css/index.html
- media/css/openstreetmap.css
helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2018</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.20</version>
<!-- The description is optional and defaults to the name -->
<description>COM_HELLOWORLD_DESCRIPTION</description>
<!-- Runs on install/uninstall/update; New in 2.5 -->
<scriptfile>script.php</scriptfile>
<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 since J2.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>
<filename>router.php</filename>
<folder>controllers</folder>
<folder>views</folder>
<folder>models</folder>
<folder>helpers</folder>
</files>
<languages folder="site/language">
<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
</languages>
<media destination="com_helloworld" folder="media">
<filename>index.html</filename>
<folder>images</folder>
<folder>js</folder>
<folder>css</folder>
</media>
<administration>
<!-- Administration Menu Section -->
<menu link='index.php?option=com_helloworld' 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>config.xml</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<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">
<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
<language tag="en-GB">en-GB/en-GB.com_helloworld.sys.ini</language>
</languages>
</administration>
</extension>