How To Package Joomla Libraries
How-Tos Development October 13, 2011 | by Joseph LeBlancYou’ve seen it before: the monster Joomla installation package. It’s a giant component, several side modules, and at least one plugin. Once you dig through the code, you notice the plugin isn’t even responding to events: it’s merely there to load up code shared by the other extensions.
Fortunately, Joomla 1.6 and higher provide a better way of handling this. Reusable code libraries can now be installed into the libraries folder without creating extraneous plugins. Just like any other extension in Joomla, a library is packaged as a .zip file with an XML manifest to guide the installation.
If you’ve created Joomla XML manifest files before, creating one for a library is straightforward. The main thing you need to keep in mind is the added <libraryname> element. This element is the “system name” for the library: make sure it is valid as a directory name across platforms. When coming up with this “system name,” avoid spaces, capital letters, and special characters. Upon installation, Joomla will create a subfolder of libraries using name specified by <libraryname>. Contrast this with the <name> element, which identifies an extension with a human readable name.
<name>Missing Tools!</name>
<libraryname>missing</libraryname>
Anything included in the <files> element will be copied into the library’s folder. Just like any other extension, you must list every folder and file to be copied. To keep maintenance work to a minimum, organize your code inside of folders. This way, the only time you have to update the <files> portion of the manifest is when you add more folders to the base level.
<files>
<folder>smart</folder>
</files>
Besides the <libraryname> element, the name of your XML file is important. Unlike other extensions, all of the library manifest files are stored together in administrator/manifests/libraries. If you choose manifest.xml as the name of your manifest file, it will get overwritten if another library chooses to do the same. To avoid this conflict, name the file after the value of the <libraryname> element. So if your library is named missing, name your manifest file missing.xml.
Place the manifest file inside of the folder with the code you want to use. Note that the method attribute can be set to upgrade on the <extension> element. Using upgrade allows site administrators to install newer copies of your library directly on top of older ones.
<?xml version="1.0" encoding="utf-8"?>
<extension type="library" method="upgrade" version="1.7.0">
Finally, you will want to include the author’s name, creation date, copyright, license, email, website, version, and description. This information is the same as it is for any other extension. The complete XML file will look similar to this:
<?xml version="1.0" encoding="utf-8"?>
<extension type="library" method="upgrade" version="1.7.0">
<name>Missing Tools!</name>
<libraryname>missing</libraryname>
<author>Joseph LeBlanc</author>
<creationDate>October 2011</creationDate>
<copyright>Copyright (C) 2011</copyright>
<license> http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL </license>
<authorEmail>contact@jlleblanc.com</authorEmail>
<authorUrl>www.jlleblanc.com</authorUrl>
<version>1.0</version>
<description> Tools you need to scratch the itches you have </description>
<files>
<folder>smart</folder>
</files>
</extension>
Aside from the XML manifest, the library will need the PHP code you’re wanting to package. Unless there is a specific reason for not doing so (such as a legacy codebase), place all of your code in PHP classes.
The core joomla library uses the prefix ‘J’ for all PHP class names. This prefix helps avoid namespace conflicts with other PHP classes, while maintaining compatibility with PHP 5.2. For your library classes, use the name of your library as the class name prefix.
In this case, we have the missing library with the folder smart and the file drops.php; this makes MissingSmartDrops a preferable class name. Although you can name your class as you wish, this convention reduces the risk of duplicate names. It also aids other developers in navigating your library.
<?php
defined( '_JEXEC' ) or die;
class MissingSmartDrops
{
// ...
// ...
// ...
}
Create a .zip file of the folder with your manifest file and code. Once the .zip file is ready, install it into Joomla as you would any other extension. The library is now available for use in any extension.
Passing a dot-separated path into jimport() loads the library if it is not already in memory. Libraries are not loaded until explicitly requested through jimport(), so installing additional libraries does not create direct overhead. Once loaded, you can either call class functions statically or instantiate the classes as objects.
<?php
defined( '_JEXEC' ) or die;
jimport('missing.smart.drops');
$colors = array('Red', 'Yellow', 'Blue');
$color_drop = new MissingSmartDrops('color', $colors);
echo $color_drop;
Installable libraries make reusable code in Joomla a reality. Instead of shoehorning your reusable code into a plugin, package your code as a library. But don’t just package your library: distribute it and work with other developers to reduce code duplication. Together, we can share code and write better extensions for Joomla!
(Also, the missing library package can be downloaded here and forked here. Enjoy!)
How to override offline mode in Joomla without requiring a login
How-Tos Mambots/Plugins January 17, 2011 | by CoryAs you may know, you can set your Joomla site "offline" by setting "Site Offline" to "Yes" in the Global Configuration. This is a great feature that enables you to build or troubleshoot your site in offline mode, restricting access only to backend users who login through the login form displayed on the offline page.
The only limitation to this feature is that you have to be logged in to see the frontend of the site, so you cannot test the site from the perspective of a user who is not logged in. For example, if you want to test registration, AEC or Ambra Subscriptions integration, or any number of other things that might require you to see the site from the perspective of a non-logged-in user, you cannot do that because offline mode requires you to log in to see the front end of the site.
I have developed a Joomla plugin called Offline Override that enables you to put the site in offline mode, and enter a keyword to override offline mode without requiring a login. You simply install and enable the Offline Override plugin, create your secret keyword (no spaces) and enter it in the plugin's parameters. Then, when your site is in offline mode, you can override the offline mode by adding "?keyword=VALUE" in the address bar for the frontend of your site. For example, if your keyword is "joomlarocks", you would go to http://yoursite.com/?keyword=joomlarocks. The override only lasts the length of your session and you must re-enter the keyword in the address bar (http://yoursite.com/?keyword=joomlarocks) the next time you open your browser.
Add a commentGetting started with JForm
How-Tos Development November 29, 2010 | by James KennardJForm is one of the most exciting new features to be introduced as part of Joomla! 1.6. The intention is to provide XML configurable forms complete with custom field types and form validation. I cannot stress enough just how much easier this is going to make life as a Joomla! developer. In this article we take a look at how to define JForms and how to display them.
Add a commentJoomla! Day DC 2010
Blog Community August 26, 2010 | by Joseph LeBlancFar and wide across this great nation, Joomla! users have been waiting. Waiting for their voice to be heard. Waiting for their vote to count. On October 16th at the footsteps of Washington DC, the wait will be over. We will finally be hearing from the President himself... Mr. Ryan Ozimek, President of Open Source Matters!
For many months now, NoVA Joomla has quietly lobbied from inside the Beltway to build a collation of representatives from across the nation. Delegates are traveling from the District of Columbia, New Hampshire, Massachusetts, New York, Virginia, Nebraska, Georgia, Louisiana, and Texas; we're even expecting visitors from England and Greece! Will you be represented? Only if you attend! Come with us and wave the Red, Green, and Blue! (and orangey-yellow too!)
Add a commentHow to track your Joomla! project with Git
How-Tos Development July 05, 2010 | by Joseph LeBlancIf you’ve ever worked on an existing website, chances are you’ve run into a directory listing like the following:
Copy of index.html
about.html
contact.html
favicon.ico
index.html
index.html.bak
index.html.bak2
index.html.old
pricing.html
It’s also quite possible you are responsible for having created a mess like this. We’re always told to make backups of our files, and so we make them, often right next to the files of a live site. While it’s a good idea to make a backup of your code before changing something that already works, .bak, .old, and .other files can accumulate very quickly.
Made-up extensions like .bak tell us (let alone others) very little about the significance of each change. It would be nice if there were some way of keeping a history of every change made to a file. Better still, tracking who made each change would be useful. And a way of combining changes from two different copies of the same file would be fantastic.
Fortunately, such systems already exist.
Add a commentDeploying Large-Scale Websites with Joomla - Part 1: An Interview with Mitch Pirtle
How-Tos Miscellaneous June 10, 2010 | by CoryI've heard a lot of talk lately about whether or not Joomla can handle large-scale websites, or if it's best used for small mom-and-pop sites. I can say emphatically that the answer to that question is that Joomla is not only useful for small mom-and-pop sites, but it can also be used to build it medium and large-scale sites. There are a few popular examples of large-scale websites built with Joomla, and two developers have been involved in a number of these sites: Mitch Pirtle from Space Monkey Labs and Fotis Evangelou from Komrade and JoomlaWorks. In this 2-part series, I am interviewing both Mitch and Fotis to get their insights on what it takes to deploy large-scale websites with Joomla. Part 1 is my interview with Mitch.
Add a commentRead more: Deploying Large-Scale Websites with Joomla - Part 1: An Interview with Mitch Pirtle
Establishing a patching process
How-Tos Security May 17, 2010 | by Tom CanavanGood day,
Waaaaaay back in January, I took the time in this column to discuss with you the concept and need to patch your site. Since that article was written till now (May 16, 2010) there has been about 180 report vulnerabilities for Joomla extensions. And some number of them for the Joomla core (to be fair only a few).
Given that you might be using one of these, its important to revisit this highly important topic.
Add a commentUTF-8 in Joomla
How-Tos Development April 18, 2010 | by James KennardHave you ever browsed to a website only to find that half the content is unreadable? Or that certain characters are being displayed in strange and mysterious ways? Or perhaps you wanted to enter a foreign or unusual character but found that the result was a garbled mess.
The chances are you have been subject to poorly managed character encodings. Joomla! extensions are no exception to these occurrences, but with a little bit of effort and some help from the Joomla! framework, we can avoid these problems with relative ease.
Add a commentPage 1 of 13









