Learning Joomla! 1.5 Extension Development

By Joseph L. LeBlanc
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Joomla! Extension Development: An Overview

About this book

Joomla! is the world's hottest open-source content management system, and the winner of the 2006 Open Source CMS Prize.

While Joomla! is packed with features, its greatest quality is that it is extremely extensible, allowing any number of complex applications to be cleanly integrated. Shopping carts, forums, social networking profiles, job boards, and real estate listings are all examples of extensions developers have written for Joomla!. All of these can run off one Joomla! site, while only one database, template, and core need to be maintained. When you build an extension to Joomla!, it will inherit the look and feel of the overall site.

If you're eager to start creating extensions for Joomla!, this book is for you. Walk through the development of complete Joomla! 1.5 components and modules with this tutorial for PHP programmers. Written for Joomla! version 1.5 and tested against the final release, this book will get you started coding your first extensions as quickly as possible, and adds the features that will make your work look professional! This book is the most up-to-date introduction to creating Joomla! extensions available.

The book will show you how to use the Model-View-Controller design pattern, pull in common HTML elements, use JavaScript frameworks, manage users, build configuration panels, internationalize interfaces, and manage database records. General knowledge of PHP is assumed, but previous Joomla! programming experience is not.

Publication date:
December 2008
Publisher
Packt
Pages
284
ISBN
9781847196200

 

Chapter 1. Joomla! Extension Development: An Overview

You have developed dynamic websites in the past, but a friend of yours told you about Joomla!, so you decide to give it a try. You want to start a simple website about restaurants after being inspired by the attractive celebrity chefs on the Food Network. The installation goes smoothly and more quickly than attempting to build a content management system from scratch. After finding a delicious template, adding some menus, and banging out a couple of reviews, you begin to think of some of the features that will draw in more visitors and even some cash. Within minutes, you install a shopping cart for selling books, a forum for gathering suggestions of places to review, and some advertising affiliate programs for the sidebars.

However, as you glance through the home page, you feel that something is missing. Then suddenly a brilliant idea hits you for something entirely new. Once it is executed, you know others will want to use it for their sites as well. You look around Joomla!'s source files and start looking for a way of building code that will slide right into place.

Why extend Joomla!?

Joomla! is not only designed to handle content articles, but also to allow a number of complex applications to be cleanly integrated. Shopping carts, forums, social networking profiles, job boards, and real estate listings are examples of the Joomla! extensions written by developers. All of these can run on a Joomla! site, and only a single database, template, and core need to be maintained. When you build an extension to Joomla!, it will inherit the look and feel of the overall site. Any type of program that can be coded in PHP is a potential extension waiting to be written.

Your extensions can also be portable. When coded correctly, you will easily be able to install your code on another copy of Joomla! without having to enter the database logins and other basic configuration information again. You will also be able to distribute your extensions to others as packages so they can install and enjoy them easily. End users will not need any programming or database knowledge to use your extensions.

Customization versus extension

Joomla!'s code is designed to be extended rather than hacked or directly modified. Rather than changing the core code, it is better to write an extension. When updates and patches are released for Joomla!, the core code will be updated, but your extensions will not be overwritten. These extensions are crafted in a self-contained manner, allowing you to develop your own code freely, without disturbing other items present in the Joomla! installation.

Although they are self-contained, extensions do not operate in a completely sealed environment; you can mix different extensions to get the functionalities you desire. Joomla!'s code allows extensions to share resources and sometimes perform actions on each other. Because we can write extensions, we will do this instead of customizing the core.

 

Why extend Joomla!?


Joomla! is not only designed to handle content articles, but also to allow a number of complex applications to be cleanly integrated. Shopping carts, forums, social networking profiles, job boards, and real estate listings are examples of the Joomla! extensions written by developers. All of these can run on a Joomla! site, and only a single database, template, and core need to be maintained. When you build an extension to Joomla!, it will inherit the look and feel of the overall site. Any type of program that can be coded in PHP is a potential extension waiting to be written.

Your extensions can also be portable. When coded correctly, you will easily be able to install your code on another copy of Joomla! without having to enter the database logins and other basic configuration information again. You will also be able to distribute your extensions to others as packages so they can install and enjoy them easily. End users will not need any programming or database knowledge to use your extensions.

Customization versus extension

Joomla!'s code is designed to be extended rather than hacked or directly modified. Rather than changing the core code, it is better to write an extension. When updates and patches are released for Joomla!, the core code will be updated, but your extensions will not be overwritten. These extensions are crafted in a self-contained manner, allowing you to develop your own code freely, without disturbing other items present in the Joomla! installation.

Although they are self-contained, extensions do not operate in a completely sealed environment; you can mix different extensions to get the functionalities you desire. Joomla!'s code allows extensions to share resources and sometimes perform actions on each other. Because we can write extensions, we will do this instead of customizing the core.

 

How to extend Joomla!


There are five types of extensions that Joomla! supports templates, languages, components, modules, and plug-ins. Templates are used to control the overall look and feel of your site, using HTML and CSS. Languages are used to translate the text labels of the Joomla! user interface through the use of a simple text file. Components, modules, and plug-ins are primarily PHP-based, and will be the focus of this book.

Components

Of the extensions available, components are the most crucial. Components are essentially what you see in the "main" portion of the page. Joomla! is designed to load and run exactly one component for each page generated. Consequently, Joomla!'s core content management functionality itself is a component.

Components frequently have sophisticated backend controls. The backend is commonly used to create and update records in database tables; it can also run PHP code not specific to Joomla!. For instance, you may have an existing program that sends emails to a large list of subscribers. Instead of making your users log into this program separately, you can provide a link in the backend through which non-programmers can tell the program to send a message. You can also create component backends, which allow site administrators to upload pictures or videos.

Modules

In contrast to components, any number of modules can appear on a page. They are frequently used to create sidebars, banners, and menus. Modules complement the contents of a component; they are not intended to be the main substance of a page. Joomla! also allows administrators to add their own text into custom modules called content modules; these involve no programming and can be displayed alongside coded components. The backend controls for modules are limited, typically consisting of basic formatting.

Plug-ins

When a piece of code is needed throughout the site, it is best implemented as a plug-in (formerly called a Mambot). Plugins are commonly used to format the output of an article, component, or module when a page is built. Some examples of plug-ins include keyword highlighting, article comment boxes, and JavaScript-based HTML editors. Plugins can also be used to add different types of data to the results found in the core search component. The backend controls for plug-ins are similar to those of modules.

 

Topic overview


This book will cover the following topics regarding the development of extensions for Joomla!:

  • Creating toolbars and list screens

  • Maintaining a consistent look, and reducing repetitive code by using HTML functions

  • Accessing the database and managing records

  • Security, and the preferred way of getting request variables

  • Menu item control

  • Controlling the logic flow within a component

  • Configuration through XML parameters

  • Packaging and distributing

Let's take an overview of these topics.

Creating toolbars and list screens

Joomla! has a standard set of toolbar buttons that are used throughout the backend. These provide a consistent appearance across components so that users quickly become familiar with the corresponding functions. When necessary, the labeling and functions of these buttons can be changed, and new buttons can also be added.

As with the standard toolbars, Joomla! has a certain look for screens that list a set of records from the database. These lists usually have links to the edit screens for the individual records and have toggles that change the publishing status of the record. Automatic pagination is also available for lists.

Maintaining a consistent look, and reducing repetitive code by using HTML functions

Several standard CSS class names are used to format content and HTML elements within your extensions. This makes it easy for your extensions to seamlessly blend in with the rest of the website. Additionally, Joomla! includes many functions to automate the generation of checkboxes, dropdowns, select lists, and other common elements.

Accessing the database and managing records

A common database object is used in Joomla!, so that only one connection is made during every page request. This object also provides a set of functions to make queries and retrieve results. These functions are database-independent and are designed in such a way that you can install multiple copies of Joomla! into the same database when desired.

In addition to having a common database object, Joomla! has a standard database table class. Records can be created, read, updated, and deleted using the core functions. Logic can also be added so that child records in other tables are deleted when the parent is removed.

Security, and the preferred way of getting request variables

Because Joomla! is a web application deployed within public reach, it is necessary to protect it against security vulnerabilities. Joomla! employs a common method to make sure that scripts are called only within the framework, and not randomly executed.

Besides unintended script behavior, maliciously-submitted data can be used by hackers to gain access to your database. Joomla! provides functionality that prevents attacks of this kind.

Menu item control

A noteworthy feature of Joomla! is that navigation is separated from content. However, if a component is not built to take this into account, it is possible that website administrators will lose their templates and module selections. To take advantage of the system, it is necessary to refer to the intended menu item in generated links.

Also, it is possible to give administrators multiple options for linking to your component. This will allow the selection of different display options for the frontend without the need to construct long, confusing URLs manually. These options can additionally offer administrators some simple configuration controls.

Controlling the logic flow within a component

The same file is always called when a certain component is loaded, but different functions are called within it. Joomla! uses standard variables to determine which function to execute on each request. There are also classes available to automate the logic flow based on these variables.

At a minimum, components are designed to separate the output from the database and other processing functions. Larger components will separate the logic flow using a controller, the data access methods using a model, and the output using views. These conventions make it easier to maintain the code and help the component perform in a reliable and predictable way.

The elements of larger components come together to form what is called the Model View Controller (MVC) design pattern. Rather than being a specific piece of code that you include, MVC is a method to organize your project. The use of MVC helps you keep HTML markup, database queries, and user requests separate from one another so that code changes cause minimal disruption. Other software projects such as Ruby on Rails, CodeIgniter, Django, CakePHP, Zend Framework, and Qcodo also use MVC as a way of designing web applications.

When writing MVC code the first time, it may seem like unnecessary work up front. However, as your components grow, MVC makes your code easier to read and maintain. For instance, when an extra<div> tag must be added around your output, another programmer maintaining the component will automatically know to look amongst the views instead of deciphering the controller function that accepts uploaded files. Code that mixes processing and HTML output quickly becomes difficult to maintain; if() statements pile up and you never know if you are adding tags to the right place. Although the use of MVC in Joomla! components is optional, it is strongly recommended for nontrivial code.

Configuration through XML parameters

Rather than create a separate table to hold all of the configuration for an extension, Joomla! sets aside a place where short values can be held. These variables are defined through an XML file, which is installed with the extension. The XML file also provides default values and constraints for these parameters. The saving and retrieval of these values is automated-that is, handwritten queries are not needed.

Packaging and distributing

Once the coding is complete, it can be easily packaged for others to use. A listing of all the files involved is added to the XML file. Any queries needed for table creation are also included. All of the files are then compressed in an archive. The extension is then ready to be installed on any Joomla! based website.

 

Our example project


We will build extensions to create, find, promote, and cross-link restaurant reviews. A component will handle common data points seen across all reviews, such as price range, reservations, cuisine type, and location. Your visitors will be able to search and sort the reviews, and add their own criteria, to zero in on their dining options for the evening.

Some modules will highlight new reviews, and draw the attention of frequent visitors. Finally, one plug-in will pull pieces of the reviews into feature articles, and another will integrate them into searches.

To prepare for this project, you need to install a fresh copy of Joomla! 1.5 on a web server with PHP and MySQL. If you prefer to use one computer exclusively to complete this project (and do not have a local web server), the best thing to do would probably be to download and install a bundled and pre-configured package such as WAMP for Windows (http://www.wampserver.com) or MAMP for Mac (http://mamp.info). This way, you will be able to work with all of the files on your local file system.

 

Summary


In this introductory chapter, we first understood the need for extending Joomla! 1.5 and compared customization with extension. We learnt that Joomla! can be extended through components, modules, and plug-ins. This allows you to add functionality to a Joomla! site without hacking the core code. Joomla! can then be maintained and updated without disturbing the custom code. We also saw an overview of the topics that we will be covering in the chapters that follow.

About the Author

  • Joseph L. LeBlanc

    Joseph L. LeBlanc started with computers at a very young age. His independent education gave him the flexibility to experiment and learn computer science. Joseph holds a bachelor’s degree in Management Information Systems from Oral Roberts University. Joseph is currently a freelance Joomla! extension developer. He released a component tutorial in May 2004, which was later translated into French, Polish, and Russian. Work samples and open-source extensions are available at www.jlleblanc.com. In addition to freelancing, he is a board member of the DC PHP Conference. He has also worked as a programmer for a web communications firm in Washington, DC.

    Browse publications by this author
Book Title
Access this book, plus 7,500 other titles for FREE
Access now