Recent comments


Syndicate


Follow Me

Twitter Facebook

Drupal module development part 3

As the 3rd and hopefully the last part of our Drupal module development series, we're going to create the actual link function that enabled support for facebox and some test page to demonstrate our function. First of all, the purpose of this function is to add the rel attribute on any links for Facebox to recognize it. Now Drupal has this l function which is used in creating links and has a 3rd optional parameter where in we can set the rel attribute, but I'm kinda lazy so instead of doing that, we're going to create a function.

/**
 * A helper function for display facebox powered links
 */
function facebox_link($text, $path, $options = array()) {
  if (!empty($options['attributes'])) {
    $options['attributes']['rel'] = 'facebox';
  }
  else {
    $options['attributes'] = array('rel' => 'facebox');
  }
  return l($text, $path, $options);
}

This is just a wrapper function actually for Drupal's l function. Here you see how we explicitly pass the optional 3rd argument. Now we have our facebox_link function all set, the only thing thats left is a demo page. For that we need to use Drupal's hook_menu, its a hook to add our custom links programmatically.

/**
 * Implementation of hook_menu()
 */
function dfacebox_menu() {
  $items = array();
  $items['admin/settings/dfacebox'] = array(
    'title'            => t('Facebox for Drupal'),
    'description'      => t('Sample usage of Facebox for Drupal'),
    'page callback'    => 'dfacebox_test_form',
    'access callback'  => TRUE,
    'type'             => MENU_NORMAL_ITEM,
  );
  return $items;
}

hook_menu expects to return an array containing a key-value pair of each menu item we want to add. The key is that actual path to out menu and the value is another key-value pair that specifies attributes of our link. Here we set the title and description to be just a text, the t function is used to make sure the text can be translated if ever we decided to make our module multi-language. The "page callback" is the one which constructs the body of our page, here we set it to a custom function we're about to create. "access callback" means that our menu is accessible by everyone including anonymous users, now we don't want anonymous users to be testing our module and Drupal has rich set of access restriction functionality but we'll reserve that for another series. The "type" key tells Drupal what kind of menu we're about to create. You can read more about hook_menu and each parameters here.

/**
 * dfacebox page callback for tests
 */
function dfacebox_test_form() {
  $add_user = drupal_get_form('user_register');
  $out = '<fieldset>
            <legend>Image via Ajax</legend>
            '. facebox_link('Google', 'http://www.google.com.ph/intl/en_com/images/logo_plain.png') .'
          </fieldset>
          <fieldset>
            <legend>Divs</legend>
            <a href="#add-user" rel="facebox">
              Add User
            </a>
            <div id="add-user" style="display: none;">'. $add_user .'</div>
          </fieldset>';
  return $out;
}

 

Here we load the user registration form, every form in drupal has a key and user_register is for the user registration form. drupal_get_form is just a function to fetch the form and we assign to $add_user. Then we return a bunch of html pages plus the registration form but we set its display to none by default so its doesn't show up.

Our first example fetches Google's logo, you see how we conveniently call our custom function to generate a link. The second one is a hardcoded link tag that simply shows the hidden registration form by passing the div with "add-user" id as our link's href value. Now if you navigate to admin/settings/dfacebox, you should see our demo page and test our examples.

That concludes our module development series, I hope you learned something. If you have questions just throw them on the comments section and I'll be happy to read and respond accordingly.

Bookmarked, part 1,2 and

Bookmarked, part 1,2 and 3!Great posts!   

hehehe thanks! I'll try to do

hehehe thanks! I'll try to do more even though my head is full of merb right now.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.