Blog Archive

Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Thursday, 24 January 2013

Adding autocomplete to a simple module field in FUEL CMS

This quick tip follows on from Add a JQuery plugin to FUEL CMS module admin page.

FUEL CMS v1.0 uses JQuery's UI library  in the admin by default. This means features like auto-complete, or auto-suggest if you prefer, are very easy to add to simple modules.

In your model, add the form_fields() function if it is not already present, and choose the field in the model you wish to add autocomplete to - in this example, I'm going to suggest emails in an email field called "contact_email":

$fields['contact_email'] = array(
    'class' => 'email_suggest', 
    'comment' => 'Known emails will be suggested as you type'
); 
 
The array configuration here is minimal, we are just attaching a CSS class to the field, and adding a comment so that hovering over the label in the form will show the comment as a tooltip. More field options are shown on this page.

Since suggesting emails to the user when filling in an email field is so useful, I want to do it for any module that has an appropriate field, so instead of adding the JQuery function to the model's Controller.js, I'm going to add it to the parent class, BaseFuelController.js, then it will be available to call from any extended class.

Find a suitable location in BaseFuelController.js to put the following (I put it at the very end, don't forget to extend the array by putting a comma before it)

email_suggest : function() { 
    $('.email_suggest').autocomplete({ 
         source: '/suggest_emails' 
    });
}
 
As per the Autocomplete documentation, this function (email_suggest) will apply the autocomplete to an element with a class of "email_suggest", and will look for data remotely in the url '/suggest_emails'.

I made a controller to return emails in a JSON array from that url, but you could use the opt-in view functionality of FUEL CMS and create a view file of the same name, thus saving a bit of typing! If the emails are private, you might want to protect them from being viewed by just anybody (ie outside of the admin). You could opt to put the url inside of the Fuel module, but that's another project.

In the Controller.js of your module (mine was an "events" module, so it was named EventsController.js), you then just need to add something like this:

add_edit: function(){
    this._super(); 
    this.email_suggest(); 
} 
 
Since the module's Controller.js is an extension of the BaseFuelController.js, the function is called from the parent class. So for other modules that need the same feature, just add this.email_suggest(); to the add_edit() function in their Controller.js, not forgetting to define the js_controller key in MY_fuel_modules for each module you need to.

And that's pretty much it!

Monday, 25 June 2012

Add a JQuery plugin to FUEL CMS module admin page

When creating a new FUEL CMS module, you get a fully fledged administration CRUD when the module is added to MY_fuel_modules.php. As good as that is, there’s often a need to extend its javascript capabilities in certain situations.

FUEL CMS makes use of a javascript framework named JQX. Its employment in the FUEL admin allows additional code and plugins to be added.

Firstly, let’s demonstrate how JQuery can be hooked into the existing HTML & PHP of the admin. We would want to assign a CSS id or classname to a given field or fields as a selector for JQuery. If the application of a plugin is to a single field, then FUEL CMS assigns an HTML id attribute to the fields within the create / edit page of the module anyway, so that’s catered for. If you wish to assign a custom class name then this can be done within the model of the module, using FUEL’s form_fields() method:

$fields['yourfield']['class'] = 'your-class-name';

So, class or id, we are ready to add functionality via JQX.

First off, we need to create a javascript controller file, using the same name of the module. For example, if the module in hand were named “news” we would create a file called ‘NewsController.js’ and add it to the /fuel/modules/fuel/assets/js/fuel/controller folder. This naming convention allows FUEL to use the script without any further intervention! So when you browse to (for example) /your-site/fuel/news/edit/1 any code within the NewsController.js will be executed. However, the code in that file should be an extension of the existing JQX controller class! So the javascript within should look something like:

   1:  fuel.controller.NewsController = jqx.createController(fuel.controller.BaseFuelController, {
   2:      
   3:      init: function(initObj){
   4:          this._super(initObj);
   5:      },
   6:      
   7:      add_edit: function(){
   8:          this._super();
   9:      }    
  10:  });

This example does nothing but create a new class. To make use of it at all though, we need to add the following to MY_fuel_modules in /config:
$config['modules']['news'] = array(
'js_controller' => 'fuel.controller.NewsController'
);

You may have many other module configurations in this array, but 'js_controller' is required to load the js file in the module. With that done, how do we apply a plugin to a field in the create / edit form?

Again, by convention, the JQX framework looks for plugins on a specific path, in v0.93 of FUEL CMS it is in /fuel/modules/fuel/assets/js/fuel/jquery/plugins. Add the plugin to that folder, and then add the following to the beginning of the js controller script, before the class initialisation (NB the .js file extension is not required):

jqx.load('plugin', 'charCount');

This plugin (charCount.js) happens to be a character countdown script which I want to apply to a textarea field called “summary” within the news module. To instance it we need to add a line to the add_edit function, and create a function that will call the plugin and apply it to the field in question. So our controller code becomes:

   1:  jqx.load('plugin', 'charCount');
   2:             
   3:  fuel.controller.NewsController = jqx.createController(fuel.controller.BaseFuelController, {
   4:      
   5:      init: function(initObj){
   6:          this._super(initObj);
   7:      },
   8:      
   9:      add_edit: function(){
  10:          this._super();
  11:          this.charCounter();
  12:      },
  13:      charCounter: function() {
  14:          // applies char countdown to summary field
  15:          $('#summary').charCount({
  16:              allowed: 120,        
  17:              warning: 20,
  18:              counterText: 'Chars left: '    
  19:          });
  20:      }    
  21:  });

The add_edit() now invokes the charCounter() function on line 11. In turn, this then applies (via JQuery) the character counting plugin code to an element with id “summary”, although as noted above, we could have used a class name instead of an id as a selector. If you are adding more plugins to the module, just use jqx.load() and do something similar with the add_edit function to invoke the plugin.

Powerful stuff, JQX!

I've added a similar walkthrough of adding JQuery functionality to admin modules here.
My top artists