1. What Is Joomla! Development?

This chapter outlines the different ways you can extend and customize the functionality of Joomla!. In order to understand this, we need to first describe the various software programs that are used to build and run Joomla.

Developing for Joomla Is Not Difficult!

If you are new to Joomla and web development, you can feel overwhelmed by acronyms, jargon, and new concepts. However, with a little help and perseverance, you can succeed in writing code for Joomla to make it work just the way you want it to. Joomla is designed from the ground up to be modified and extended, and many people with limited technical knowledge have successfully learned to write programs for Joomla.

This book explains every new concept and topic as we proceed. We will not assume that you already know anything about Joomla or web development. As needed, we will point out more in-depth resources you can use to fill in gaps or explore topics in greater detail.

The Joomla Technical Environment

Newcomers to dynamic website development face a bewildering array of acronyms and jargon. Joomla uses web servers like Apache or Microsoft Internet Information Services (IIS); the PHP programming language; the MySQL database; the JavaScript programming language; HTML, XHTML, and XML document types; and CSS.

Let’s start with the server. The server is the computer where the Joomla program files reside and where most (but not all) Joomla programming code gets run. To run a Joomla website, a server must run the following three programs:

• A web server (Apache or Microsoft IIS)

• The PHP programming language

• A database (usually MySQL)

The web server is the software that actually allows the server to interact with the browser. It is the bridge between the external world and the local folders on the server. When a browser requests a URL, the web server knows which file directory on the server maps to that URL and finds the file to execute (for example, “index.php”).

PHP is the primary programming language that Joomla is written in. PHP is by far the world’s most popular program for writing dynamic websites. It interacts with the web server software and creates XHTML pages dynamically. For most Joomla development tasks, the coding will be done in PHP.


Server as a Concept versus Server as a Machine

When we refer to the server in the context of web development, we refer to the software that “serves” the HTML code to the browser. In a production website, this will typically run on a remote computer at a web hosting company. However, when we run Joomla locally (which we often do for development and testing), we still have a server. In that case, it is the server software (Apache or IIS) running on our PC.

From a technical standpoint, both environments work the same way. The server and the browser always communicate via TCP/IP. The only difference is that, with a remote server, the TCP/IP packets that carry the information back and forth travel a lot farther.


MySQL is the database that most Joomla sites use. It is by far the most popular database for web applications. In a Joomla website, the database holds almost all the information about the site. For example, every time you add an article or a menu or module, this gets saved in the database.

When you are using Joomla (or any other website), what you actually see in your browser is XHTML. This stands for extensible hypertext markup language and is the updated version of HTML. Most Joomla developers need to have a basic working knowledge of XHTML. The end result of all the processing that a Joomla program does is the display of XHTML in the browser. Note that in this book we will use the term HTML to refer to both HTML and XHTML.

CSS stands for cascading style sheets. CSS code is used to “style” the XHTML code so that it displays the way we want in the browser. For example, CSS controls the font size, type, color, the text spacing, and all other aspects of the way the information is formatted inside the browser.

All the information for how the site will be displayed—including the CSS and the way the content is laid out on the page—is contained in the Joomla template. This design allows us to completely separate the content of a site from the site’s appearance. That is why you can completely change the appearance of a Joomla website dynamically just by changing the template.

JavaScript is a program that runs inside your browser and is the J in the acronym AJAX (asynchronous JavaScript and XML). JavaScript allows us to make websites feel more like desktop programs. For example, in Joomla, JavaScript allows things to pop up automatically when you hover the mouse over a field and to highlight a field in a form when it is not valid. Since JavaScript runs on the client, it can respond to events without having to interact with the web server, so things change instantly in the browser, without the user having to press a Submit button.

Joomla Programming: What Do You Need to Know?

Most Joomla programming involves writing PHP code. Since the information for a Joomla website is in the database, some of this PHP code interacts with the database. This is normally in the form of SQL queries. Database queries are used to store data to the database and to pull data from the database. So Joomla developers need to know how to write SQL database queries to interact with the database.

Some Joomla programming involves working with XML or HTML. For example, parameters for forms are typically stored in XML files. The parts of the program that actually output the information to the browser (called views or layouts) typically contain a mixture of PHP and HTML code. So Joomla developers need to be somewhat familiar with HTML.

Some Joomla developers are heavily involved with the way a site is laid out and the way it looks. These people need to be very good at working with HTML and CSS. Most of this work is done during the development of the template. As indicated earlier, this book does not cover template development.

Other Joomla developers are mostly concerned with getting the correct information on the page. In this case, you do not need to be an expert on HTML or CSS, although a general familiarity is still very helpful.

JavaScript is in a special category. It interacts with the PHP programs as well as the HTML and CSS. At this time, most Joomla development does not require a lot of JavaScript programming or knowledge. We will discuss the role of JavaScript in Joomla in greater detail in Chapter 12.

As developers, we don’t have to know much about the web server (Apache or IIS). When you deploy a site to the web, the security of the site is a high priority. Much of the security of a site depends on the host you use and the way they have configured the web server and database software. But this does not normally affect the way we write our Joomla programs.

To sum up, a Joomla website uses a number of programs and file types to do its work. Most Joomla development involves writing PHP code, and that is the main focus of this book. Some of this code will include queries to the database. We discuss database issues in detail in Chapter 11. Some of our code will output HTML to the browser. We will explain how PHP works elegantly with HTML when creating the final output.

Unless you are working on templates, you don’t need an extensive knowledge of HTML and CSS, so we won’t discuss much about CSS.

We can do a lot of Joomla development without knowing JavaScript. However, it is an important topic and we cover JavaScript topics in Chapter 13.

Extending Joomla: Let Me Count the Ways

We said earlier that Joomla was designed to be extended. What does that mean in practice? Extending Joomla means making it do what you want it to do. This can be in the form of changing the way something works or adding entirely new functionality. This section will outline all the different ways that Joomla can be extended.

Open Source Means You Have Control

Joomla is free and open-source software (FOSS). As such, the programming source code is part of the normal package download. Anyone who creates a Joomla website can, if they wish, edit any of the source code files and alter the working of the program.

Let’s define two terms you will see when people discuss modifying Joomla programs:

Core code is source code that is distributed as part of the set of files you get when you download and install Joomla (for example, in a zip archive file).

• A hack or core hack is making a change directly to one of the core source code files.

Because Joomla is FOSS, you are completely within your legal rights to hack any file in the system. However, this is generally considered inadvisable.

Why? Hacking the core files is not recommended for two simple reasons. First, if you change these files, you can no longer rely on the integrity of the system. If you encounter a problem in your system, you won’t know for sure whether it is a bug in Joomla or a result of something you changed. To make matters worse, it will make getting updates for your site much more difficult. Normally, the Joomla project releases maintenance updates on a regular basis. For example, when you initially load your site, you might be on Joomla version 2.5.1 and then version 2.5.2 will be released.

The 2.5.2 archive file will contain a full copy of every file that was changed since the last version, and when you do the upgrade, your old files will be replaced. If you have modified some of these programs, you have a problem. If you do the upgrade, you will lose your modifications. If you don’t upgrade those files, you will not get the bug fixes or other changes. The only solution is to either redo your changes on the new version of the files or identify what was changed between Joomla versions and make the same changes to your modified files.

If you have a small number of minor hacks, this could be manageable. However, it complicates the process of keeping your site updated.

The good news here is that hacking core files is almost never needed to make Joomla work your way. The next sections discuss all the different ways you can modify Joomla without changing any core files.

Overrides Let You Change What Shows on the Page

One of the most common changes that people want to make to a site is to modify what is shown on the page. If you are familiar with using Joomla, you know that most components, modules, and plugins provide parameters that let you control different aspects of how these extensions will operate. For example, when you show an article on a page, you can decide with a parameter whether you wish to show the author or the date it was created. Those types of changes require no programming at all to implement.

However, the different ways that people might choose to show information on a page is almost limitless, and it is not practical to try to have a parameter setting for every possibility. Let’s consider a simple example. The standard article list layout in Joomla looks like what’s shown in Figure 1.1.

Image

Figure 1.1. Standard layout for articles in a single category

Suppose we prefer to show the author in the first column and the title in the second column. There is no parameter that controls the order of display. So what do we do?

By simply copying one file and cutting and pasting a few lines of code, we can change the output to what’s shown in Figure 1.2.

Image

Figure 1.2. Modified layout using template override file

Because this is done as an override, we are not hacking any core files. So we don’t have to worry about our files getting overwritten during a version upgrade.

Starting with version 1.6, the use of override files has been greatly expanded. Template overrides are still supported. But now language files, menu items, and modules can all have overrides independent of the template in use.

Overrides will be discussed in detail in Chapter 4.

Extensions

Extensions are the most visible aspect of any Joomla website. The word extension is commonly used to mean two different things with respect to Joomla. The most common usage is as an add-on software program that works with Joomla to add functionality to a website. The Joomla project maintains a website called the Joomla Extensions Directory or JED (http://extensions.joomla.org) that lists many of the extensions available from developers in the community.

From a programming point of view, the word extension has a slightly different meaning. In this sense of the word, an extension is any component, module, plugin, language, or template. Extensions that are included with the standard Joomla download are called core extensions. Extensions from other sources, such as the JED, are called third-party extensions.

A key point to understand is that, when Joomla is running, it doesn’t see any difference between core extensions and third-party extensions. That is why third-party extensions appear to be completely integrated into the package. They are treated exactly like core extensions.

If you are using a Joomla website that incorporates well-written third-party extensions, it can often be difficult to tell where the core package leaves off and an extension begins. This is by design.

Plugins

Plugins are perhaps the best-kept secret in the Joomla development world. Many times, plugins provide a simple way to do something that would otherwise be difficult or impossible. But since they are a bit difficult to visualize, it is easy to overlook them as a solution to a problem.

One way to understand plugins is to visualize the program flow as a conveyor belt or assembly line. At different points in the process, we define events. Figure 1.3 shows the program flow for saving an article to the database.

Image

Figure 1.3. Simple save process

In the first box, the user has entered in the article and presses the Save button. In the second box, Joomla checks to make sure the article has valid data. If so, in the next step the article is saved to the database. The last step shows we are done with this activity.

With Joomla’s event system, the process actually is a bit more complex but much more powerful. This is shown in Figure 1.4.

Image

Figure 1.4. Save process with plugins

The steps going down the left are the same as in the first example. However, we now introduce two events. The first event, called onBeforeSave, takes place after the article’s data is validated but before the article is saved to the database. The second event, called onAfterSave, happens just after a successful save to the database but before we finish the process. These events break into the normal program flow and allow us to do some special processing. Once the special processing is completed, the normal program flow continues.

In Figure 1.4, Joomla found two plugin programs with onBeforeSave and onAfterSave methods. This just happens to be the case in this example. It is important to understand that, depending on your Joomla implementation, there might be no plugins to run for an event or there could be ten. Each time an article is saved, Joomla checks for any methods that need to be executed and runs them one at a time.

This is best illustrated by an example. Suppose we want to examine every article on the site and make sure it has appropriate keywords defined in the metadata. Let’s say we have already written a program that looks for certain words in each article and adds them as keywords.

Using the onBeforeSave event, it is very easy to implement this feature as a custom plugin. We simply create a plugin with an onBeforeSave method and run our program. In this case, our plugin method has the article as a parameter, so we can easily examine the contents of the article and update the keywords column before the article is saved to the database. We don’t change any core files. We just add our new code in the right place and Joomla finds it and runs it at just the right time.

The cool thing about events is that they are always there but they don’t get in your way. Joomla very quickly can check to see whether there are any methods that need to be run for a given event. An event is said to be fired when the program checks for methods tied to that event and executes them if found. You can fire events from your own code and even create custom events if needed.

Chapter 5 discusses plugins and events in detail and walks you through how to use them to great advantage to extend Joomla.

Modules

In Joomla, modules are extensions that render on a page typically as small “boxes” that show around the main content of the page (which is normally a component). For example, Figure 1.5 shows the sample data home page for version 2.5. The modules are the small areas outlined in black. One page can show multiple modules.

Image

Figure 1.5. Example home page component and modules

Modules typically are simple extensions. A module can be linked to a component. For example, the Related Items module is linked to the Single Article view of the Articles component and shows a list of articles that have keywords that match the keywords for the article in view. In other cases, a module will show the same information regardless of the component on the page.

All menus in Joomla are modules and there are many other types of modules included with the Joomla core. We will discuss the core modules in Chapter 3.

We would typically choose to use a module when we want to show information on multiple pages and when that information won’t be the central focus of that page.

We will discuss developing modules in Chapter 6.

Components

The component, in most cases, provides the central focus of a page. In the previous example, the content outlined in gray shows the Featured Articles Blog menu item, which is one of the menu items included with the articles component. Components include one or more menu item types that you choose when you create or edit a menu item. Each menu item type corresponds to a view for the component. For example, the articles component provides seven possible menu types or views: Archived Articles, Single Article, List All Categories, Category Blog, Category List, Featured Articles, and Create Article.

Examples of Joomla’s core components include articles (also known as content or com_content), banners (com_banners), and contacts (com_contact). We will discuss all the core components in Chapter 3. Figure 1.6 shows all the Menu Item Types available for the core components.

Image

Figure 1.6. Core menu item types for Joomla 2.5

Components in Joomla are written using the model-view-controller (MVC) design pattern. This makes it easier to maintain the programs over time and allows for separation between the different tasks associated with preparing and displaying a Joomla web page. Use of this pattern also makes it much easier for developers who are already familiar with object-oriented programming (OOP) and design patterns to understand how Joomla components work.

Components are typically the most complicated type of extension. We will discuss writing components in Chapters 710.

Languages

One of Joomla’s strengths is its very high degree of internationalization. Joomla is currently translated into over 68 languages. This allows people to work with the entire Joomla program in their own language.

Language translation is implemented by way of language extensions. A language extension is a set of special files (with the extension “.ini”) that include all the words and phrases that are displayed as part of the Joomla user interface. This makes it possible to translate the user interface into any language. Let’s look at how this is done.

In PHP, we use the echo command to display text on a page in the browser. Let’s look for example at the administrator login screen for Joomla, shown in Figure 1.7.

Image

Figure 1.7. Administrator login screen

Looking at the screen in Figure 2.5, you might reasonably expect that, somewhere in the Joomla PHP code, there is an echo command, something like echo 'Joomla! Administration Login'. However, if we search for that code inside a PHP file, we will not find it.

In Joomla, we don’t “hard-code” text that is part of the user interface. If we did, we would have to change the program code for each different language. Instead, we hard-code the language file keys and read the actual text at runtime.

If we search for the string Joomla! Administration Login in a language .ini file, we will find that it is in a file called administrator/language/en-GB/en-GB.com_login.ini, as shown in Figure 1.8.

Image

Figure 1.8. Search results for text in “.ini” files

The part on the left, COM_LOGIN_JOOMLA_ADMINISTRATION_LOGIN, is called the language key (or tag). The part in double quotes on the right is the actual text that Joomla will display when the language is set to en-GB.

We can see this in action if we search for that tag in the PHP files. We find this tag appears twice, in the files administrator/templates/bluestork/login.php and administrator/templates/hathor/login.php. Here is the line of code:

<h1><?php echo JText::_('COM_LOGIN_JOOMLA_ADMINISTRATION_LOGIN') ?></h1>

Instead of using echo with the English text, we echo the output of the method JText::_(). The argument for that method is the language key from the .ini file. The JText::_() method looks up the key in the .ini file for our language and returns the matching text. If for some reason the key is not found, it just returns the key instead.

If we had a different language extension (also called language pack) installed and had that selected, we would see the text in that language. Figure 1.9 shows the login screen with a Chinese language pack installed.

Image

Figure 1.9. Login screen in simplified Chinese

Language extensions are created by making new files for each core .ini file with all the tags translated into the target language. As you can imagine, this is a big job and it is not surprising that the Joomla Translation Team is the largest team in the project, with 68 languages and counting.

If you create an extension for the Joomla community, it is strongly advised that you create language .ini files for the extension and use the JText class methods to output the text. That way, it is easy for someone to provide a translation for your extension. We will show an example of how you do this in Chapter 5 when we add a language file to a plugin extension.

Templates

The last type of extension in Joomla is a template. The template controls the way all the information is shown on each page of the website. A basic principal of Joomla (and other content management software) is to separate the content of the site from the presentation of that content. This is accomplished by separating the preparation of each page into different steps. In Joomla, the entire document for each page is created and then, as the last step in the process, this document is passed to the template where it is actually converted (or rendered) by the template into the final HTML that will get passed to the browser.

You can see the result of this separation of content and presentation in dramatic fashion by changing the default template. If we navigate to the front end of the site with the default Beez2 template, we see the home page, shown in Figure 1.10.

Image

Figure 1.10. Home page with Beez2 template

If we navigate to the Template Manager, and change the default template to “Beez5 - Default - Fruit Shop,” the same home page now appears, as shown in Figure 1.11.

Image

Figure 1.11. Home page with Beez5 template

It is the exact same content, but the display is different. This separation allows site designers to work on the layout and presentation of the site and site developers to work on getting the right information shown on each page. To a site administrator, it means that the presentation of the site can be changed easily without changing anything about the way the site is built.

Because this book is targeted to developers and not designers, we will not go into details about how to build templates. However, there are some aspects of template structure that we will need to understand to do our development tasks. These include the folder structure for template override files—which we discuss in Chapter 4—and the way that module positions are created in a template, which we discuss in Chapter 6.

Which Extension Type Should I Use for My Project?

We have seen that we have four options for extending the functionality of Joomla: layout overrides, plugins, modules, and components. How do we decide which one to use for a given project?

In many cases, once we understand how each extension type works, it becomes obvious which one to use. It is also important to realize that some projects will require a combination of different extension types to do the job.

If your project will result in something similar to how a core extension works, then that will often tell you the best extension type to use.

One way to think about this is to try to use the simplest approach to solve the problem at hand. As a general rule, layouts and plugins are the simplest extension types. Modules come next. Components are the most complicated and difficult to write. So if you can do the job with a simpler extension, that is usually the best approach.

Using Joomla as a Platform or Framework

Joomla is a content management system (CMS) built using a set of reusable library classes mostly contained in the folder libraries/joomla. These library classes do many low-level tasks that a CMS or any other web application might need to do, including the following:

• Allow users to log in with different sets of permissions, interacting with the database.

• Keep track of the current browser session.

• Cache output to improve performance.

• Process events for plugins.

• Filter user input to prevent malicious security attacks.

• Process error conditions in a consistent and user-friendly manner.

Suppose we want to create a new web application for tracking the inventory for an online business. The application is not going to have users submitting or displaying articles, contacts, or banners. In fact, our application may not even display web pages at all. Perhaps we will just check inventory and return the information to another application.

In this example, we would not need or want to install the entire Joomla CMS. However, we might still need to check that a user has correct permissions for the actions. We would definitely be interacting with a database. We would want to filter requests to improve the application’s security. We might need to handle error conditions.

In other words, we wouldn’t need the CMS part of Joomla, but we could use the Joomla libraries to save ourselves a lot of work by reusing code that is already written and that works well.

With Joomla versions 1.5 and 1.6, you could handle this scenario by installing Joomla and then stripping out the parts you don’t need in your application. However, this involves extra work and should not be necessary.

For this reason, the functionality contained in the Joomla libraries has been broken out into a separate project, as of July 2011. Known as the Joomla Plaform Project, this allows developers who want to use the Joomla platform without the CMS to have an easy way to get what they need.

It also allows the platform to operate as a separate project from the CMS, with its own release schedule, code base, and developers.

Chapter 13 discusses using the Joomla platform in greater detail and creates an example application using only the platform.

Summary

In this chapter, we learned that the Joomla developer must be at least a little familiar with web servers, PHP, SQL, XHTML, XML, CSS, and Javascript. This can seem daunting to a newcomer, but Joomla actually makes doing web development easier than we might imagine.

We then discussed the different types of development we can do in Joomla: layout overrides, plugins, modules, and components.

We then learned how Joomla works with multiple languages and how that affects the way we output text to the browser. We briefly showed how templates can dramatically change the way a site looks, while presenting the exact same content.

We discussed how you can choose which approach is the best for a given project.

Finally, we discussed how Joomla can be used as a platform for building entirely new applications outside the scope of a CMS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.41.235