Upd: Русская версия статьи: Структура внутренних ссылок сайта на Joomla.


Recently I wrote and implemented several functions that allow you to filter the pages output of the website running CMS Joomla version 2.5 and later. Before sharing them with the public, I will touch on the subject posed in the title of this article, as the principle of formation of internal links to Joomla application pages will help to better understand their algorithms.

A bit of theory

Applications for the CMS Joomla are being developed as components. Components are responsible for the pages output. A link to each page is generated in the respective component in the standard URI format:

http://[site name]/index.php?[query string]

The query string is a sequence of key=value pairs separated by an ampersand. Keys' values just determine the output of each page. URI of pages are generated in accordance with the structure of the component. Currently we are not interested in the mechanism of these links formation, but the result for each page or group of pages of the application will be important. Knowing how to identify keys and their values, you can flexibly operate on the output of the page content.

My only comment is that you'll see a link in the above format in the address bar of your browser only if the SEF is off on your site. When the SEF is on, the router converts page addresses into a more intelligible appearance for the human perception. But we interested in those internal links, so to get the keys and values from the address bar, you can disable SEF on the test site.

Joomla framework assigns a number of standard keys. Consider the most used ones.

  • option - the component name with the prefix 'com_';
  • view - appearance of a particular component page;
  • layout - additional variant of the output of a particular view;
  • Itemid - identifier of a menu item, which is bound to a page, if binding occurs.

In the case of multilingual site another parameter is also added

  • lang - the webpage language.

Component developers can add keys used to build the page addresses of each particular application. For example, a basic Joomla component com_content, which is forming the structure of categories and articles, operates with the following additional keys:

  • id - Identifier of article or category depending on the current view. It takes the value of the article ID in case of an article page output (view=article). The key contains a value of category ID when listing categories (view=categories) or articles in the form of category list or blog (view=category);
  • catid - Identifier of a category, which related to the current article (view=article).

Key values for this component:

  • option - com_content.
  • view -
    • archive - archived articles;
    • article - an article;
    • categories - a list of categories;
    • category - articles in a category presented as list or blog;
    • featured - featured articles in the form of a blog;
    • form - form of creating or editing an article content.
  • layout -
    • blog - withdrawal of articles in a category in the form of a blog in the category presentation (view=category);
    • edit - option for the form of creating or editing the article (view=form). There are also pagebreak and modal. It is unlikely that any of them is useful in practice. Maybe the first one, and even then only for the convenience of users having privilege to edit content.
  • lang - appears in the page URI of the multilingual Joomla website and contains two-lettered identifier of the active language, corresponding to the parameter URL Language Code in the admin panel's language settings - eg: en, ru. (Languages and their settings mentioned, for example, in an article about the 404 page customization.)
  • Itemid, id, and саtid - take values of identifiers according to the content page's view type; wherein the first one always contains the menu item ID in the numeric format, while the other two in a specific context can be in the following form: [ID]:[alias]. It will yet be discussed further on.

Examples of page output of the component com_content

Consider a few examples of the query string keys and their values in URIs of some specific pages generated by the com_content application.

Example 1. On a page bound to the menu item 35, the blog of featured articles of English part of the site is displayed:

option=com_content&view=featured&Itemid=35&lang=en

Example 2. Articles of a category with id=27 are displayed in the form of a blog on the Russian part of the site. The menu item, which is linked to the page is 62:

option=com_content&view=category&layout=blog&id=27&Itemid=62&lang=ru

If there was no pair of layout=blog, hen the articles would be displayed by default as a list of their titles.

Example 3. Page of the article that you are now reading:

option=com_content&view=article&id=958:the-structure-of-internal-links-within-a-joomla-site&catid=43:web-development&Itemid=192&lang=en

The last example just illustrates the case where the article and the category IDs also contain aliases, separated by a colon from ID in numerical representation. In the section about the use of query string keys will be examined, how to handle them.

So, by disabling SEF on your test site, you can see the parameters of any page in the address bar of your browser.

Practical usage

If you had enough patience to overcome the previous sections, congratulations: it's time to discuss, how to utilize received information.

Knowledge of query parameters allows you to compose a condition for some code execution on one or more specific pages and page group, united by a common feature - component, category or language.

To create such conditions let's use the structure:

$value = JFactory::getApplication()->input->getCmd([key]);

in which for [key] we will substitute the string names of the parameters we need. Method getCmd зreturns a key value as a string. For instance, the condition for pages relating to articles and categories of the com_content component will be the following:

$option = JFactory::getApplication()->input->getCmd('option');
if ($option == 'com_content') {
  ...
}

For integer identifiers the method getInt can be used:

$Itemid = JFactory::getApplication()->input->getInt('Itemid');
if ($Itemid == 1) {
 ...
}

The only hitch occurs in the above mentioned case where IDs can be followed by a colon and the alias. Although these tails are being added to ID only when SEF is turned off, such cases are to be treated: what if you will need to disable SEF? We need a universal solution. Therefore, the condition for articles and categories will be a little more complicated:

$temp = explode(':', $jinput->getCmd('id'));
$article_id = $temp[0] + 0;

(This approach has already been considered in articles about hiding breadcrumbs on certain pages and selective output of social media buttons.)

Now we can create a block of code for initializing of all the variables that correspond to component's request keys:

$jinput = JFactory::getApplication()->input;
 
$option = $jinput->getCmd('option');
$lang = $jinput->getCmd('lang');
$view = $jinput->getCmd('view');
$layout = $jinput->getCmd('layout');
$Itemid = $jinput->getInt('Itemid');
 
$temp   = explode(':', $jinput->getCmd('id'));
$art_cat_id = $temp[0] + 0;
 
$temp   = explode(':', $jinput->getCmd('catid'));
$cat_id = $temp[0] + 0;

After receiving the parameter values of the page output, you can combine them to create conditions for the content filtering. The functions mentioned in the beginning of this article, contain such conditions.

In conclusion - a few comments. Those who use additional frameworks like K2, T3, etc. - can get keys provided by their developers, on the same principle. As, however, of any other components. Those who want to create their own original components on the basis of existing ones or from scratch, can read online about the MVC (Model-View-Controller) pattern, examine the JRoute method and other intricacies. And the last thing I would like to note: customizing is great, but you should insert your own code into the core and extensions very carefully, trying to minimize the process of restoring your changes after you installed updates.

Add comment