Showing posts with label module. Show all posts
Showing posts with label module. Show all posts

Wednesday, October 12, 2011

Making a Simple Form Module on Drupal 7.8

We have created a simple module before, that is here. But if you build a web site, you'll need to use forms. For that reason, i'll show you about making a simple form module in this article. Let's remember which files we've got when we create module on drupal.

Mymodule (folder)
  • mymodule.info
  • mymodule.module
  • mymodule.inc
"mymodule.info" file is for settings of the module, that's why we don't use it for this article. Other ones also are so important now.

If you check this article out, can remember what the system of drupal's work. When we code, we call the function which will use. Well, then code your mymodule.inc and mymodule.module pages:


mymodule.module page
function honda_menu() {
  $items['honda/hrv'] = array(
    'title' => 'Honda HR-V',
    'page callback' => 'honda_form',
    'access arguments' => array('access content'),
    'file' => 'honda.inc',
  );
  return $items;
}

As you see the bold row's got our function for the module with "page callback". honda_form() function is in;

mymodule.inc page


function honda_form() {
 return drupal_get_form('form_for_honda'); //This is the function that will use
}
function form_for_honda($form_state) {
 $form['text'] = array(
  '#type' => 'textfield', //Input type
  '#title' => t(Write something..'), Input label
 );
 $form['Okey'] = array(
  '#type' => 'submit', //Input type
  '#value' => 'I am Ok!', //Button value
 );
 return $form;
}

When we go to honda/hrv direction, we're going to see it like down here;

Output

Well done! Our form is ready to use. I've just added a textfield and submit button. If you want to add something else, you should add elements for array. For example;

'#type' => 'checkbox',
'#type' => 'fieldset',
'#type' => 'file',
'#type' => 'radios',

If you want to see more information about Drupal forms API, you can visit forms API.

We'll see you next article!

Wednesday, October 5, 2011

hook_help() function on Drupal: 'helping menu' for Our Module

We've learnt how to create a new module on Drupal before, here is How to create a new module! We'll been added new features on it after this article.

As first, we should explain what our module is. For that reason, can put a helping menu. We'd said there were so much functions on Drupal library, hook_help() is just one of'em. If we want to realize what this article says, must implements hook_help() function.

mymodule.module


* Implements hook_help().
*/
function honda_help($path, $arg) {
 if($path == 'admin/help#honda') { //The direction
  return t('<a href=honda/hrv>CLICK FAQ</a> for HONDA HR-V.'); //We help here!
 }
}
When checked t() function out, can see that able to use HTML tags, here is <a href=honda/hrv>CLICK FAQ</a>.


Click the "help?"!

We'll see you next article!

Tuesday, October 4, 2011

Creating a Basic Drupal Module!

It is quite easy creating a drupal module. The rule what you have to know is just selecting the function you need to use. Because, Drupal has got so many functions that you can use. But before coding, we must create a folder for our module. This folder has two important files:

  1. Mymodule (folder)
    • mymodule.info (must)
    • mymodule.module (must)
    • mymodule.inc
Let's view!


For step 1;

mymodule.info includes;

name = Honda HR-V //this is the name of the module
description = HONDA HR-V car is the best i have ever seen in this world! //the description
package = packageName //which package the module is in
core = 7.x //the core number
version = 7.8 //the version
files[] = honda.module //files that'll include to
dependencies[] = views //the modules which we must include



 
For step 2;

mymodule.module details are;

When we code a module of Drupal, we should know how to use the PHP tags. After the end of our code, we don't put the "<?>" tag on it.

    function honda_menu() {
      $items['honda/hrv'] = array(
        'title' => 'Honda HR-V',
        'page callback' => 'honda_page',
        'access arguments' => array('access content'),
        'file' => 'honda.inc',
      );
      return $items;
    }
    
     Please check "honda/hrv" out. This is the direction we go on the browser. If we click this, it's going to start on "honda.inc" page. Here is one of the most important things we have to know on drupal, is the way of how Drupal's work. "honda_page" is a function actually. When the module opened, this function'll be started.

    For step 3;

    Then let me show you the page which's name is honda.inc:

    function honda_page() {
      return t('This is the first module i created!');
    }
    
    As you see here, we've never used the ending tag ,"? >".
    This honda_page function's been used before When we coding mymodule.module page, page callback.


    By the way, you may ask me what the "t" is. t() is a function that used on drupal. This method handles the outputting works including the multilingual operations.

    If you want to see more information about t() function, you can visit official web site, here is api.drupal.org/t .


    We'll see you next article!