Book Image

Mastering Joomla! 1.5 Extension and Framework Development

Book Image

Mastering Joomla! 1.5 Extension and Framework Development

Overview of this book

Joomla! is one of the world’s top open source content management systems, which enables you to build websites and powerful online applications. Out of the box, Joomla! does a great job of managing the content needed to enhance the functionality of your website. But for many people, the true power of Joomla! lies in its application framework that makes it possible for thousands of developers around the world to create powerful add-ons and extensions. This book will guide you through the complexities of implementing add-ons, components, modules, and plugins in Joomla! 1.5. If you feel that you’ve mastered the basics of creating Joomla! extensions, then this book will take you to the next level. Packed with expert advice on all aspects of programming with Joomla!, this book is an invaluable reference guide you will turn to again and again for your Joomla! development tasks. The book starts by explaining the structure and design of the Joomla! Framework. Then we move on to extending and storing data in standard fields and dealing with multilingual requirements. Further, you will discover best practices, design, and coding methods for Joomla! components, modules, plugins, and other extensions. Along the way, you will actually design and create a component, module, and plugin that work together to add functionality to the Joomla! framework. You will also learn about customizing the page output using JavaScript effects, making use of Web Services from within Joomla! and ensuring that your code is secure and error-free. You will discover how to easily create sophisticated extensions that are robust, user-friendly, and professional by taking advantage of the many libraries and utilities provided by the Joomla! framework. A comprehensive, up-to-date reference to the Joomla! Framework API is also included in the book. Finally, we will discuss advanced methods for rendering your extensions more robust, secure, and professional.
Table of Contents (18 chapters)
Mastering Joomla! 1.5 Extension and Framework Development
Credits
About the Author
About the Reviewer
Preface

Development tools


There are numerous development tools available that we can use to develop Joomla! extensions. Most of these tools are not specific to Joomla!-like code and image editors or version control systems. Many can be found on the Joomla! extension directory at http://extensions.joomla.org/extensions/tools/development-tools.

When choosing an editor for modifying PHP source files, we recognize that it is important to ensure that the editor supports UTF-8 character encoding. Integrated Development Environments (IDE) such as the open source Eclipse and the commercial Adobe Dreamweaver are two of the more popular.

Open source image editors such as Gimp and Inkscape along with commercial products such as Adobe Photoshop, Illustrator, and Fireworks are frequent choices for manipulating web graphics.

JoomlaCode.org

An invaluable resource for developers is the developers' forge: http://www.joomlacode.org. This official site is used to host open source Joomla! projects. It provides third-party open-source Joomla! developers with free access to useful project development tools. This list details some of the tools JoomlaCode.org provides us:

  • Document Manager

  • Forums

  • FRS (File Release System)

  • Mail Lists

  • News

  • SVN (Subversion)

  • Tasks

  • Tracker

  • Wiki

If we intend to create an open source Joomla! project, we should consider using JoomlaCode.org to host the project, even if we do not intend to use all of the features it provides.

Coding standards

While you may not consider coding standards a tool, using a standardized format makes code easier to read and allows other developers to edit code more easily. Joomla! uses the PEAR coding standards. A complete guide to the PEAR coding standards is available at http://pear.php.net/manual/en/standards.php.

Here is a breakdown of the more common rules:

  • Indents are four spaces: \

    {
        // four space before me!
  • Control structures have one space between the name and first parenthesis:

    if (true) {
  • Curly braces should be used even when they are optional.

  • Functions and methods are named using the camelCase standard with a lowercase first character.

  • Functions and method declarations have no spaces between the name and first parenthesis. Parameter lists have no spaces at the ends. Parameters are separated by one space: foo($bar0, $bar1, $bar2).

  • Optional function and method parameters must be at the end of the parameter list. Optional parameter values, signified by an equals sign, are separated by spaces: function foo($bar0, $bar1, $bar2 = '').

  • Use phpDocumentor tags to comment code http://www.phpdoc.org/.

  • Use include_once() and require_once() in preference to include() and require().

  • Use <?php ?> in preference to all other PHP code block delimiters.

phpDocumentor

phpDocumentor is a documentation tool that allows us to easily create documentation from PHP source code. The documentation is extracted from the source and from special comments within the source; these comments are very similar to those used by JavaDoc.

This example demonstrates how we might document a simple function:

/**
 * Adds two integers together
 *
 * @param int $value1 Base value
 * @param int $value2 Value to add
 * @return int Resultant value
 */
function addition($value1, $value2)
{
    return ((int)$value1 + (int)$value2)
}

The multiline comment denotes a DocBlock. Note that it uses a double asterisk at the start. The first line is a general description of the function; this description can span more than one line. @param and @return are tags.

The @param tag is used to define a parameter in the format (the name is optional):

@param type [$name] description

The @return tag is used to define the return value in the format:

@return type description

Our initial example is telling us that the addition() function has two named integer parameters that it will add together. It will then return the resultant integer value.

When we document complex functions, we might want to provide two descriptions: a long description and a short description. This example demonstrates how we do this:

/**
 * Does some complex processing
 *
 * A verbose description of the function that spans more than
 * one line
 *
 * @param int $value1 Base value
 * @param int $value2 Value to add
 * @return int Resultant vaue
 */
function someComplexFunction($value1, $value2)
{
    // does some complex processing
}

Functions are not the only elements that can be documented. Elements that we can document include:

  • class methods

  • class variables

  • classes

  • define()

  • files

  • function declarations

  • global variables (requires use of the @global tag)

  • include()/include_once()

  • require()/require_once()

This list defines some common tags we are likely to encounter:

  • @access private|protected|public

  • @author name

  • @param type [$name] description

  • @return type description

  • @static

The DocBlocks are easy to read when they are displayed in code, but, more importantly, we can automatically create documentation from the source code. For more information about using phpDocumentor, please refer to http://www.phpdoc.org/.

J!Dump

J!Dump allows us to output variables during development. The output is displayed in a configurable pop-up window and describes data types and object properties and methods.

J!Dump comes as two separate extensions: a component, which we use to configure the functionality of J!Dump, and a system plugin, which defines functions that we use to 'dump' data to the J!Dump pop-up. Both extensions are required in order for J!Dump to function correctly.

Note

In order for one to use J!Dump the plugin must be published. If it is not, when we attempt to use the J!Dump functions, we will encounter fatal errors.

The most important function in J!Dump is the dump() function. We can pass a variable to this function, and it will be displayed in the pop-up. This example demonstrates how we use the dump() function:

// create example object
$object = new JObject();
$object->set('name', 'example');

// dump object to popup
dump($object, 'Example Object');

Using this will create a pop up, which looks like this:

Other functions we can use include dumpMessage(), dumpSysinfo(), dumpTemplate(), and dumpTrace().

Note

T o get a copy of J!Dump, refer to http://joomlacode.org/gf/project/jdump.