Breaking News
You are here : Home / Drupal / Drupal Module Development-Understanding Variables
Drupal module Development Understanding Variables

Drupal Module Development-Understanding Variables

Drupal Module Development-Understanding Variables:

Hello  Friends.This is my second post for Drupal Module Development.I recommend you to read first Post Drupal Module Development Basics first and then come to read this post.This Post is about Drupal Module development and usage of the Drupal variables.So i assume that before reading this post you have already read my first post on drupal module Development .In this Post WE gonna see how to get ,set,delete variables in the Drupal.


Drupal Module Development-Understanding Variables ::intro

Now Lets Gets Started and we understand how we use drupal variables with your custom drupal modules.The use of these variables is very essential as you always have to store some info and then retrieve it back.In Drupal the modules are stored in the variables table.

So that Information keep stored in the table and we can retrieve it back whenever we want.So there are certain functions in the drupal which are used for creating variables ,read variables and delete variables from the database.


Drupal Module Development -Understanding Variables ::Functions:

Lets take a look at important variable functions used in drupal module development.

Drupal Module Development imporant functions
variable_set($name,$value) Sets the varible with name and it‘my_var’,’hello’)
variablle_get($name,$default=NULL)  Gets the variable by its name,if variable is not set then use the default value.Eg:$var= variable_get(‘my_var’,0) means the find my_var in db and set=$var with its valueif not found then initialize var with 0.
varible_del(name) Deletes the variable by its‘my_var’).

These are the variable functions in drupal we use to set ,get and delete the variables from the drupal database.Now we move on and make a sample module which will help us understand variables in Drupal module development.


Drupal Module Development-Understanding variables :: Sample Module:

So we create a sample module here which will demonstrate us how to use variables in the Drupal.So I assume you read the post on Drupal module Development basics.So i’ll Simply put code here as it is pretty similar module that we create in that post.So i am gonna write some steps here .just do exactly the same.

  1. Create a Folder name “myvariables” in sites/all/modules/custom/  in your Drupal installation,so you end up as sites/all/modules/custom/myvariable .Then open “myvariable” folder.
  2. Now create a file and write the text below in it.
    name = My Variable
    description = Understanding variable in the Drupal.
    core = 7.x
    package = My package
    version = "7.x"
  3. Now Create myvariable.module file and put the code below in it.

    It is quite simple above ,we have just implemented the menu hook.In this the Function myvariables uses the variable functions.

  4. Now Enable this Module From Drupal Admin panel.

    Drupal Module Development variables

    Enabling Sample Variable Module

  5. Now Visit “http://localhost/your Drupal ProjectName/myvariables” and see the Variables sample in action.You can see every time you visit the page the variable is can delete it by setting clear=1 get param.
So that was the sample module Demonstrating drupal Varibles.Now lets move on and Understand the fucntion defined in the module in which we have used the Drupal variables.


Drupal Module Development – Understanding Variables:: sample module detail:-

Now we understand What happens inside the call back function that we used in the Drupal.

Here is that code:

This Function actually Firstly checks for variable in the database if its not found than initialize the $variable with 0. then it increments it and then set it to the database using variable_set.In the Next Statement it checks for the clear variable and if the clear GET param is set then we delete variable from the datbase and set a message using the drupal_set_message.And the last is we return the $output which we output the current value of the variable using varible_get function.When the variable is set to 1 means set for the first time then we also set the custom message.So every time variable is incremented by 1 we visit the URL.I assume that was easy.

Thats all ,Thanks for reading this article on ,Have a nice day and keep visiting.

Tags : , , , , ,

1 Reply

Leave a Reply

Want to join the discussion ? Feel Free to contribute !