In the previous chapter, we explored Joomla templates, the options to customize them, and the differences between template styles.
We’ll now look at overrides, a feature that enhances the level of flexibility of Joomla, increasing the number of ways you can customize your website.
After reading this chapter, you will be able to do the following:
Note
Creating overrides requires HTML, PHP, and CSS knowledge.
As we saw in the previous chapter, the appearance of your website highly relates to how the template is structured and styled. Sometimes you may want to customize how a specific part of the website looks, going beyond the options offered by the template style in use. Overrides help with that, allowing you to completely customize the appearance of your website through PHP, to make your website fit your styling requirements. Furthermore, overrides make you work on a copy of system files, without altering the original files, ensuring you have the opportunity to start from scratch in case of issues or when you change the template and, more importantly, preventing software updates from overwriting your changes.
Basically, with overrides, it’s possible to completely rewrite all the views in Joomla, tailoring the appearance of any component or module output for your website.
What is a view?
A view is the PHP file that defines the appearance of the output of a component or a module. The view file includes the layout definition and all the page elements, such as tables, lists, and much more.
Overrides are heavily connected with templates, as for each template, it’s possible to create specific overrides. When you create an override for a template, the original Joomla file for the specific view is copied to the /html folder under the template folder structure. The copy of the file under the template override folder is the one you need to edit at your convenience.
When loading a page, Joomla checks whether there is an override file for the specific layout in use and, if one exists, the override takes precedence over the standard layout.
The override system is heavily used in Joomla, in fact, most template producers distribute overrides for system standard views in their template packages so that they can provide customized layouts for core components such as articles, contacts, and categories.
In the next section, we’ll discover where to use overrides and when they can be extremely helpful.
As displayed in Figure 13.1, Joomla provides the ability to create overrides for all elements that include a view:
Figure 13.1 – Overridable items
This grants a high degree of flexibility, as each part visible on the public website is completely customizable.
Based on the item selected, there could be one or more views that can be customized; each view resides in a specific PHP file that will be added in the /html folder under the template in which you are creating the override.
If, on one hand, overrides increase the level of customization of your website’s appearance; on the other, they can cause some files to not be up to date, as they were duplicated. To prevent missing updates, Joomla includes an interesting feature that checks automatically whether the original files of any of the overridden files have been updated, so that you can check and eventually apply changes to your files to ensure security and compatibility with the latest version of the CMS.
To check whether the files you have changed have been updated or not, on the template management screen, there is the Updated Files tab, as displayed in Figure 13.2.
Figure 13.2 – Overrides | Updated Files
When a core or extension update involves any of the overridden files, then on the Updated Files screen, you can see the list of changed files. For each file, you can see the file path, the date when it was added to the list, the date the latest change occurred through updates, and the source of the update, as shown in Figure 13.3.
Figure 13.3 – Overrides | Updated Files – List of changed files
You can check each file in the list by clicking on the file name.
If you enable the Show Difference toggle in the editing screen, you can see, in addition to the regular code editing box, a new box in which the changes to the file are highlighted. Removed content is highlighted in red, and added content is highlighted in green, as shown in Figure 13.4.
Figure 13.4 – Overrides | Updated Files | Show Difference
You can then verify each modified file to ensure it includes all the required code. After completing the operation, you can mark the file as checked by clicking on the symbol in the Checked column in the list of updated files. The file will be then marked as checked, as shown in Figure 13.5.
Figure 13.5 – Overrides | Updated Files | Checked file
In this way, your overrides are always up to date and kept in sync with the core updates. In the next section, we will create an override as an example.
In this example, we will see overrides in action. We want to change the appearance of a single article, making our article titles rendered as an H2 header instead of H1.
Let’s start creating our override:
Figure 13.6 – Overrides | Create Overrides | com_content
Figure 13.7 – Overrides | Create Overrides – Override creation confirmation
Figure 13.8 – Overrides | Override files
Figure 13.9 – Overrides | File editing
Overrides can also be used to add custom CSS class markers to page items in the template structure or to simplify the page view, removing non desired items.
Removing an override
If we have messed up the structure of the file or if we simply want to remove an override, we just need to delete the folder or the override file that is placed under the html folder of the template in use. For example, if we want to remove the override created in this tutorial, we can either remove the /html/com_content folder or the /html/com_content/article/default.php file.
In the next example, we will create an override of a module.
In this example, we want to override a module standard view. Let’s work on the mod_footer module:
=
Figure 13.10 – Overrides | mod_footer override creation confirmation
Figure 13.11 – Overrides | mod_footer override
Figure 13.12 – Overrides | Template CSS folder
Figure 13.13 – Overrides | Creating a custom.css file
Figure 13.14 – Overrides | custom.css file editing window
.myfooter {
font-size: 13px;
font-weight: 600;
color: #1c77ba;
}
Overriding your template CSS
In Joomla, there is a simple way to override the CSS of a template. Simply create a new file under the /css folder of your template called custom.css. Such a file can contain your customized CSS rules or completely new classes that you can recall from overrides and other Joomla items. Joomla automatically checks whether or not a custom.css file exists in the css folder of your template. If it exists, rules specified in it take priority over those of the standard template CSS files. The use of the custom.css file prevents your changes being lost while updating your templates.
In the previous examples, we explored the procedure to override a component output (single article view) and the style applied to a module (mod_footer), completing our basic overview on overrides. In the next section, we will explore another type of override that is applied to language strings: language overrides.
To complete this part about overrides, we should discuss language overrides. Sometimes you want to change some language strings to better fit your website needs. Language overrides help us with that, without the need to operate directly on original language pack files, preventing us from losing our changes while updating Joomla language files.
Language overrides allow us to modify a language string in the desired language simply from the Joomla backend.
Let’s start creating our first language override:
Figure 13.15 – Languages: Overrides
Figure 13.16 – Languages: Overrides | Select Language & Client
Figure 13.17 – Languages: Overrides | String to be overridden
Figure 13.18 – Languages: Overrides | Edit Override
Figure 13.19 – Languages: Overrides | String search results
Figure 13.20 – Languages: Overrides | Create an override
Figure 13.21 – Language Overrides | A language override in action
Creating language overrides is simple and may be useful to customize how some of the strings are displayed in our website.
Special symbols and characters in strings
Some language strings may include special symbols and characters, such as the one we changed in the example above. The %s marker in a language string is replaced automatically on the website by a variable, in fact the original string was Hits: %s and on the website, we can see the number of hits. Be careful while creating overrides to keep all the special characters that represent variables or calculated values.
With language overrides we completed the exploration of overrides. Test your knowledge with the exercises proposed in the next section.
It’s time to apply the knowledge about overrides acquired in this chapter through the proposed exercises.
In this exercise, you are going to create a new override for the login module. We want to inject text into the login module once the users are logged in to warn them about the session expiration:
<p class="text-center">
Please note that your session will expire 15 minutes after your last action.
</p>
In this exercise, you are required to create a new language override.
In Languages: Overrides, create a new override for the English (United Kingdom) – Site language, changing the Written by: %s string to Author: %s on the website. To do so, you need to create an override for the string identified by the constant COM_CONTENT_WRITTEN_BY. Follow the steps seen in the preceding example to change the language string. Once done, save the override and check the results on the website frontend.
In this chapter, we have explored the override features of Joomla and understood how to create overrides for a component or a module view. We have also discovered a different type of override related to language files.
In the next chapter, we’ll complete the presentation of the template-related features, introducing child templates.