Chapter 3. Embedding Rich Media Features with Joomla! Plugins

"Small details make a big difference".

I've always believed that small details are what make sites look great. I think this is also true in the case of Joomla! plugins. Plugins are the smallest of extensions, but they give us great potential.

Plugins can also be useful for other less technical site administrators, who will find the code easier to write than the usual HTML code. There are lots of ways in which plugins can help make our site better, and in this chapter we are going to see some of them, such as:

  • Concept of plugins—what they are and how they work
  • A code highlighter plugin
  • captbunzo's Flickr Album Plugin
  • SC jQuery, a library loader plugin

I hope you will like this chapter. Let's get started.

Concept of plugin—what is a Joomla! plugin and how does it work?

As I have said previously, plugins are the smallest of Joomla! extensions and they work in a different way.

Components are placed usually in the central part of our site and are mostly used to display and organize our site content, such as articles, photo galleries, and even the products of a shop.

Modules tend to be smaller in complexity, work along components, and appear surrounding the central zone of our site.

Both these types of extensions show site visitors a wide variety of content, information, images, and so on.

Plugins work in a different fashion and, usually, more than displaying content, they are used to modify them. For this purpose, plugins are called in response to some Joomla! or user-defined events.

Joomla! events are a bit of a complex concept, but let's keep it simple. For example, displaying our content may seem like quite a simple task, but during that time some events are triggered. Among others we may find:

  • onPrepareContent—when contents are being prepared
  • onBeforeDisplayContent—just before contents are displayed
  • onAfterDisplayContent—when our contents are displayed

Plugins can be defined to be executed when these events are triggered.

So, if we have a plugin that reads our content and finds a "www.something" text to create a link, we can define our plugin to be executed in the onPrepareContent event.

This way our content will be modified before being displayed. In an extremely basic way, it works, as shown in the following diagram:

Concept of plugin—what is a Joomla! plugin and how does it work?

Well, it's pretty basic, but we are not going to enter into the "plugin development" garden. I just wanted to show you the basic way in which plugins work. Let's again take our previous example—the image gallery we created in Chapter 1. For our gallery to work, we wrote the following code in one of our articles:

{ppgallery}/stories/ppgallery{/ppgallery}
                

And when we enabled the plugin, instead of just showing the code in the frontend, a gallery was presented to us. If you check the source code of the page, you will see something like this:

<span style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><span class="pp_gallery">
                    <span class="ppg_thbox10"><span class="ppg_thb"><a href="/images//stories/ppgallery/image_1.jpg" rel="prettyPhoto[pgg10]" title="" target="_blank"><img src="/cache/images//stories/ppgallery/200x150_q75_t_image_1.jpg" alt="image_1.jpg" width="200" height="150" title="image_1.jpg" /></a></span></span>
                    .
                    .
                    .
                    <span class="ppg_clr"></span>
                

So our simple line of code {ppgallery}/stories/ppgallery{/ppgallery} has been converted to a full and complex HTML code that is able to display our image gallery. When did this happen? Well, this time the change has been made on the onPrepareContent event, which happens when content is being prepared. The plugin detects our previous line of code and replaces it with all the HTML code required for the gallery.

Note

You can find a lot of information about Joomla! events and plugins at http://docs.joomla.org/Tutorial:Plugins.

With this knowledge, we can see how powerful Joomla! plugins can be. Not only do plugins help us enhance our content, but they are also a tool for less-technical people to introduce great-looking contents into their site with minimum knowledge.

Next, we are going to look at some interesting plugins.

Code highlighter plugin

Code highlighter extensions are very useful when creating tutorial sites, code blogs, and so on. The difference they can make is great and these plugins also turn some difficult-to-read code into great-looking code.

For this example, we are going to use a very interesting extension called Core Design Chilicode plugin. As always, we can search for it in the JED or else use the following URL:

http://extensions.joomla.org/extensions/edition/code-display/7250.

Download it, then go to our administrator panel, then to Extensions | Install/Uninstall, and install the downloaded file.

Before taking a look at the parameters of this extension (in fact, before we can use it), we need to install another extension, the Core Design Scriptegrator plugin, which can be found at:

http://extensions.joomla.org/extensions/core-enhancements/scripts/3030.

This plugin is used to load libraries, like jQuery itself, which are needed. These libraries are for the code highlighter extension. Why aren't these extensions placed in the code highlighter extension itself? Well, the developer, wisely I have to say, has decided to separate this library loader plugin. This has been done so that all of the other extensions from this developer can use these libraries without having to place them in every extension they create. It also saves the necessity for each extension to load a copy of the library.

As I've said, this is a wise way of doing things. Now enough talking, we will download and install that plugin. Go to Extensions | Plugin Manager and look for System | Core Design Scriptegrator plugin. Enable it and also open it to see its parameters:

Code highlighter plugin

Very few parameters need to be changed, but we will take a look at them:

  • Load jQuery to—is very self-explanatory; as this plugin is used to load libraries for all other plugins to use, the jQuery field was very expected. We can select from Nowhere, Site, Administration or Both. This time we only need the library to be loaded in the "site" or the "frontend".
  • Load Highslide to—is used for other extensions from the developer. As this plugin doesn't need it, we can safely leave this as Nowhere.
  • GZIP compression—is enabled by default, which is okay because it will make our libraries smaller and faster to download. Anyway, if we detect any problem, we can disable this.

Advanced parameters are about the Highslide library, so we don't need to take a look at them for now. We are ready to check the other plugin. Without leaving the Plugin Manager screen, look for Core Design Chilicode plugin and open it. The following screenshot shows the plugins admin screen:

Code highlighter plugin

Not many parameters here, just two of them:

  • Default language—decides the default language to be used from a wide range of languages. As we can define this parameter later, we will leave it at None. But if you find yourself using a language more often than others, you could define it here—just to save some work later.
  • Default source—here we can select Content or File. For our example, we are going to use Content as the plugin that will show the contents of one of our articles. In case we want to use File, we will need to define the folder where our files will be placed. This is done in the advanced parameters section, as we can see in the following screenshot:
Code highlighter plugin

The only parameter in the Advanced Parameters section is as follows:

  • Script directory—if in the previous parameter we have selected to use File as the content source, here we will need to define where those files are placed. We can leave it as it is, as we are not going to use it.

Tip

Don't forget to enable both the plugins.

Now that we are done with the preparation, we can make use of this extension. But first we need an article where we can use it. I'm going to create a new section because as of now we only have one section called News and I wouldn't like to use it for this example. Navigate to Content | Section Manager and create a section called Site content.

We will also create a category called Plugins inside this section. Go to Content | Article Manager and create a new article. We are going to call this article Code Highlighting.

Now imagine we have the following content in our article:

Here we have some sample content for our article:
                        for($i=0; $i<250; $i++){
                        echo "Keep reading";
                        }
                        And now some bit more:
                        <div id="header">
                        <p>This is the header content</p>
                        </div>
                    

In the next screenshot, we can see how it will more or less look on our frontend:

Code highlighter plugin

This will be very difficult to read. So we are going to enhance it a bit using our previously installed plugin. Go back to the Article Manager screen. In order to use the plugin, we will write something like:

{chilicode language source} content {/chilicode}
                    

The parameters are:

  • chilicode—is the plugin's "name" and won't change.
  • language—indicates the language coloring that will be used. We can select from a wide range: JavaScript, PHP, MySQL, HTML, Java, C++, C#, Delphi, and LotusScript.
  • source—can be either content or file. We are going to use content.

Let's modify our contents accordingly; they will look more or less as follows:

Here we have some sample content for our article:
                        {chilicode php content}
                        for($i=0; $i<250; $i++){
                        echo "Keep reading";
                        }
                        {/chilicode}
                        And now some bit more:
                        {chilicode html content}
                        <div id="header">
                        <p>This is the header content</p>
                        </div>
                        {/chilicode}
                    

Check the following screenshot to see how these changes will affect the appearance of our article:

Code highlighter plugin

It's much better now. Our code samples look better and are a lot more readable (with very less effort required, and as we have commented before, in a very easy way).

Some other extensions to try

Of course, there are many other options. Some of them are as follows:

  • RJ_InsertCode—supports other interesting languages such as XML, Python, Perl, Visual Basic, and many others. If you don't find the language you need in our previous sample, maybe this one has it included.
  • CodeCitation—is a very interesting option, as it lets us define the tag that will encapsulate our code, hence making it easier to remember. It also supports some other interesting languages.

As always, we can find these extensions and many others by searching the JED.

captbunzo's Flickr Album plugin

Our next stop will be something very interesting that will add some quality content to our site—we are going to see a Flickr plugin. With this plugin, we will be able to insert Flickr galleries in our articles, helping us to add images that complement our articles.

Search for "captbunzo's Flickr Album Plugin" in the JED or use the following link:

http://extensions.joomla.org/extensions/external-contents/photo-channels/6566.

You can use the method you prefer. Following the download link on the JED page will direct us to the author's page where we will be able to download the plugin.

There you will find two versions, one for PHP4 and the other for PHP5. Download the one that suits your needs.

After doing so, we will go to the admin screen, Extensions | Install/Uninstall, which will present us with the installation screen for extensions. Here we can search for our extension and click on the Upload File & Install button. If all goes as expected, the plugin will be installed without problems and we will be able to find it in Extensions | Plugin Manager.

Look for Content - Flickr Album; it will be disabled for now:

captbunzo's Flickr Album plugin

Clicking on the red cross will enable the plugin. We can do so now so that we don't forget about it. Our next step will be clicking on the plugin name so that we can access the plugin's admin screen. This screen will look similar to the following screenshot:

captbunzo's Flickr Album plugin

In this screen, we will be able to set up our options. Let's take a look at the parameters:

Parameter

Description

Flickr API Key

This is very important, as we will need an API key before using the plugin. However, it's quite easy to get an API key from the www.flickr.com page. Once you get your own API key, you need to place it here.

Album Title Default

This will be the default title that will appear over the image gallery. This title can be changed when invoking the gallery or later.

Link Text Default

This will generate a link to Flickr. We can place the text that will appear on the link.

Default User

Here we can indicate the default user from whom we will get the photos. We can follow the instructions from http://joomla.paulthompson.net/ to do so.

Color Theme

This is the first of the design parameters. Here we can select the color for the image border.

Thumb Size Default

Here, as the name implies, we can select the size of the images—square being the smaller one. We will be leaving this as is for now.

Enlarged Size Default

This time we can select the size of the enlarged or pop-up image—medium size will be okay for the images I will be using, but you can select any other size of your preference.

Sort Order Default

With this parameter, we will be able to select the ordering preferences for our images. As I've uploaded only a few images, this doesn't matter for me, but take a look at the options and select the one you prefer.

Col Count Default and Row Count Default

This will be used to select how many columns and rows will be presented with our gallery, directly affecting the number of images shown.

Thumb Padding Size and Thumb Margin Size

These options will affect the separation between images. Though we could change this in our CSS files, we can do so here for convenience. For now we will be leaving these parameters as they are.

Thumb Border Size

This is another similar parameter. We can select the border size for our images here. I think 1px will be enough for our border size.

Include jQuery

This is an important parameter that will let us decide whether we want to include the jQuery library or not. As we have already loaded this library for other extensions, here we will select to not include the library.

Include jQuery Flickr

This will include the Flickr jQuery plugin (as we need it in our example). We will leave this option as it is.

Debugging Comments

If something fails, we may use this option to try to find the problem. But for now, leave it at Disable debugging comments, as we don't want any message to be shown.

And that's all. We have finished configuring the plugin; now save the changes. Now to the interesting part—using the plugin. To use the plugin, we are going to create a new article, but you can use an existing one if you wish. Go to Content | Article Manager and create a new article.

I'm going to call this article Flickr demo, but this really doesn't matter. The important part will be the content for the article:

This is a demo of the flickr plugin:
                        {flickr-album}Type=User, User=46665927@N07{/flickr-album}
                        In order to call the plugin we use the following tag:
                        {flickr-album}
                        Which we close using this one:
                        {/flickr-album}
                    

Inside these two tags, we can specify some parameters in order to adapt the gallery to our needs. In the example, we are defining the Type as User and the user ID to be used. Those parameters and the ones configured in the plugin admin screen will result in a gallery similar to the following screenshot:

captbunzo's Flickr Album plugin

Sadly, I think I'm not such a good photographer. But, hopefully, this will serve as an example of what we can achieve with this plugin. Of course, we can specify some other parameters in the plugin call. We can see a full list at the following website:

http://joomla.paulthompson.net/.

Some of these include:

  • Title the title we want to use for our gallery
  • Type=Search—to show photos from a search instead of a user
  • Keywords=world domination—used with the previous parameter in order to perform the search

All of these options give us great configuration capabilities, which will allow us to use this plugin on lots of occasions to complement our articles with useful images.

Some other plugins to try

There are many other plugins that we can use in our site. Some of these are:

  • AutoFacebook plugin—this plugin will put your Joomla! article's title into your Facebook status message and can be very useful to attract Facebook visitors to your site.
  • The Ultimate Social Bookmarking Plugin—is an interesting plugin that will let our site visitors submit our articles to social networks, thus bringing more visitors to our site.

As always, we can find these extensions on the JED.

SC jQuery

For our third plugin, we are going to see something less visible. In the previous chapters, as well as in this chapter, we have seen modules and plugins that introduced into our site some very nice effects.

The SC jQuery plugin helps us with jQuery loading and has some nice features, even more than what you can see at first sight. Some of them, as said in the JED, are as follows:

  • Minified version of jQuery 1.3—loads a "compressed" version of jQuery, saving us a bit of bandwidth
  • Executes noConflict() after jQuery has been loaded—in order for other libraries to be used alongside jQuery, just like MooTools
  • jQuery UI integration and templates—is a curious extra, but we will be able to decide whether to include it or not
  • A place to add our own code to the template header

I'm going to leave the last and most interesting one untold, but we will see it later. Stay tuned so you don't miss it!

Well, now search for "SC jQuery" in the JED or load this URL: http://extensions.joomla.org/extensions/core-enhancements/scripts/7230. Once we are there, we will download the extension.

The next step is to install the file from our administrator screen Extensions | Install/Uninstall. Once we have done so, we are ready to go. Under Extensions | Plugin Manager, we will be able to find our newly installed plugin. Just search for System - SC jQuery and locate the plugin.

Enable it from the manager screen and then click on it to see its details. The next screenshot is showing just that:

SC jQuery

Though most of the parameters are self-explanatory, I would like to take a look at them with you:

  • Load jQuery UI libraries (all)—I've set this to No, as only our tabs module uses the jQuery UI. If we don't modify its code (because it will continue to do so), it's of no use loading jQuery UI again.
  • jQuery UI theme—this includes the images and the CSS style that make the jQuery UI themes, in the case of using the previous option: here we can decide the appearance.
  • Enable plugin for frontend—this will load the plugin and thus the jQuery library in the frontend. I've selected Yes.
  • Enable plugin for backend—we will be leaving this as No because we don't need it for now.
  • Enter custom code here—lastly we are presented with a textarea that will let us introduce any code we want. Any code put here will be placed into a document-ready function.

And that's all. Just don't forget to enable the plugin. If all of this goes well, we won't see any differences in our frontend. However, if we check the source code of the template, we will see this:

<script type="text/javascript" src="/plugins/system/scjquery/js/jquery-1.3.2.min.js"></script>
                        <script type="text/javascript" src="/plugins/system/scjquery/js/jquery.no.conflict.js"></script>
                    

These two lines of code will load the jQuery library and the "no conflict" script. Why don't you try the custom code parameter of the plugin? Just go back to the plugin admin screen and enter the following code in the custom code parameter field:

alert("Hello, How are you?");
                    

Don't worry; we will remove this annoying message just after we try it!

Save the changes and go back to the frontend, reload, and that's it! We have just created an annoying message!

SC jQuery

In fact, the ability to annoy visitors is not what I would like to show you; we will leave that for another chapter. At this point in time, I would like to show you the generated code.

On taking a look at the source code of our site, we will see some code as follows:

jQuery(function($){
                        alert("Hello, How are you?");
                        });
                    

As we can see, the code has been placed inside:

jQuery(function($){
                    

And:

});
                    

So our code will be able to work along with other libraries, such as MooTools. Before continuing, remove that code from the plugin.

And now we will see something very interesting and useful.

Along with all the functions we have seen, the SC jQuery plugin offers us a very useful extra feature. If we take a look at the plugin code, we see the following line of code at the end of it:

$app->set( 'jquery', true );
                    

Note

In newer versions of the plugin this line of code seems to have been removed. However we can modify the plugins/system/scjquery.php file and add that line to it, at the very bottom, just like this:

...
                        $app->set( ‘jquery’, true );
                        $doc->setHeadData($headData);
                        return true;
                        }
                        }
                    

Note

This way we will be able to use the techniques we are about to show.

What's that? Well, the plugin is setting a variable called jquery with the value of true. How can we use this variable? Very easily; remember working with the AJAX search module in Chapter 2?

The fact that many modules were loading the jQuery library gave us some problems. In order to solve them, we needed to make a complex code similar to the following:

$custom = '<script type="text/javascript">' ."
";
                        $custom .= 'if (typeof jQuery == "undefined") {' ."
";
                        $custom .= 'var script = document.createElement("script");' ."
";
                        $custom .= 'script.setAttribute("type","text/javascript");' ."
";
                        $custom .= 'script.setAttribute("src", "/modules/mod_ajaxsearch/js/ jquery-1.3.2.min.js");' ."
";
                        $custom .= 'document.getElementsByTagName("head")[0]. appendChild(script);' ."
";
                        $custom .= 'var script = document.createElement("script");' ."
";
                        $custom .= 'script.setAttribute("type","text/javascript");' ."
";
                        $custom .= 'script.setAttribute("src", "/modules/mod_ajaxsearch/js/ script.js");' ."
";
                        $custom .= 'document.getElementsByTagName("head")[0]. appendChild(script);' ."
";
                        $custom .= '}else{ ' ."
";
                        $custom .= 'var script = document.createElement("script");' ."
";
                        $custom .= 'script.setAttribute("type","text/javascript");' ."
";
                        $custom .= 'script.setAttribute("src", "/modules/mod_ajaxsearch/js/ script.js");' ."
";
                        $custom .= 'document.getElementsByTagName("head")[0]. appendChild(script);' ."
";
                        $custom .= '}' ."
";
                        $custom .= '</script>' ."
";
                        $document->addCustomTag($custom);
                    

In this code, we used JavaScript to detect if the jQuery library had been loaded, as follows:

if (typeof jQuery == "undefined") {
                    

And, if it was not loaded, we loaded it. First we created a script element:

var script = document.createElement("script");
                    

Then we added a type attribute to this script element:

script.setAttribute("type","text/javascript");
                    

And, of course, we added a src attribute:

script.setAttribute("src", "/modules/mod_ajaxsearch/js/jquery-1.3.2.min.js");
                    

In the end, we added the following code to the head section of our site:

document.getElementsByTagName("head")[0].appendChild(script);
                    

Now that the SC jQuery plugin is creating a variable, we can use just that in an easy way. Open the file modules/mod_ajaxsearch/mod_ajaxsearch.php and remove all the JavaScript. We will replace the JavaScript with this code:

$app =& JFactory::getApplication();
                        if(!$app->get('jquery') ){
                        $document->addscript(JURI::root(true).'modules'.DS. 'mod_ajaxsearch'.DS.'js'.DS.'jquery-1.3.2.min.js'),
                        }
                    

Here we are getting an instance of the application and trying to get the jquery variable that was defined earlier as follows:

$app->set( 'jquery', true );
                    

Also uncomment the following line:

$document->addscript(JURI::root(true).'modules'.DS. 'mod_ajaxsearch'.DS.'js'.DS.'script.js'),
                    

Looks easier, doesn't it? And it is more readable. We just check if the jquery variable is defined; if not, we load the jQuery library. If the variable returns true, it would mean that the plugin has already loaded the jQuery library. So we don't need to do it again and can follow with loading the module's own libraries.

Maybe you are thinking "Hey Jose, why didn't you show this first?" Well, I always like to see all the possible options. I feel the learning process is more complete if you are given all of the possible options.

The first solution was the one made by our own means, without the use of an external plugin. Now, with the introduction of another tool, we are able to upgrade the way in which we detect if the jQuery library has been loaded. And that's a good summary of a learning process, a continuous upgrade of knowledge.

Keep with us; we will see many other useful things!

Some other plugins to try

There are many other plugins just like the following plugin, but I just wanted to note this one in particular:

  • Google Ajax Library—is a module that I think is interesting because it loads the library from Google, thus saving us a bit of bandwidth
..................Content has been hidden....................

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