Add Alias Field to Joomla Component

An alias field is required for the SEF (Search Engine Friendly) URLs for accessing items on the website. SEF URLs are URLs which make sense to both humans and search engines. For example, if there is a database of planets, then an SEF URL would be something like:

www.example.com/galaxy/planets/venus

instead of something like

www.example.com/galaxy?catid=37&id=52

You can switch SEF URLs on or off through the Joomla Global Configuration, Site tab, SEO settings.

Step 1: Database Field

Define the alias field to be not null and unique. To add the alias field to the database record.

`alias` VARCHAR(400) NOT NULL DEFAULT '',

For existing tables,

ALTER TABLE `#__helloworld` ADD COLUMN `alias` VARCHAR(40o) NOT NULL DEFAULT '';

The, create unique index:

CREATE UNIQUE INDEX `aliasindex` ON `#__helloworld` (`alias`);

2. XML Form

Next, you need to change the back-end XML form definition for the alias field:

<field 
name="alias"
type="text"
label="JFIELD_ALIAS_LABEL"
description="JFIELD_ALIAS_DESC"
hint="JFIELD_ALIAS_PLACEHOLDER"
size="40"
/>

Step 3: Model File

The getListQuery() function of the model has to be changed for providing the alias information to display in the list of items.

// 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');

Step 4: Layout File

To display the alias in the same way as it's shown in the com_content, edit the defauly.php file in the tmpl folder as:

<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>

Step 5: Table

You have to make sure that the alias is unique, and doesn't contain characters that aren't allowed in URLs. You can do this by overriding the JTable::check() method. The function check() is called before saving the record. Hence, you can override this to ensure that the 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. The unique index will ensure that alias values are not duplicated.

Add the following method in the table class:

public function check()
{
$this->alias = trim($this->alias);
if (empty($this->alias))
{
$this->alias = $this->greeting;
}
$this->alias = JFilterOutput::stringURLSafe($this->alias);
return true;
}

After adding the alias field, you can create router for the component.

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive