Monday, June 20, 2011

Working with drupal API

It's been a while since I started working with drupal, without a doubt the best CMS ever. I find working with drupal extremely interesting with all those API functions in hand.

Drupal has convenient ways to handle database work for us, therefore it is advisable to use drupal API as much as possible to get the work done. Moreover updating to a new drupal version from an older version is quite less conflicting if we have followed the drupal API.

recently I got the chance to work with some interesting drupal API functions and thought they might be useful for you as well at some point.

  1. node_load()
  2. Given nid(node id) as the parameter this function loads that node from drupal database. I'm not exaggerating if I say this functions helps us to get full control over any node.dreamy isn't it?

    Example usage 1
    <?php 
    $node=node_load(820);//loads the node with the nid equals to 820 and store it in $node variable
    print_r($node);//prints all the information associated with the node, yikes it's lengthy :P 
    ?>
    
    After loading a particular node it is possible to get almost every information related to it.

    Example usage 2
    <?php 
    echo $node->body;//prints the content of the node
    echo $node->title;//prints the title of the node
    ?>
    
  3. truncate_utf8()
  4. truncate_utf8() function truncates a string to a specified number of characters.

    Example usage
    <?php 
    $node=node_load(820);
    $content=truncate_utf8(strip_tags($node->body),400,true,true);//limits the body content of a node to 400 characters
    ?>
    
  5. l()
  6. Creating a link using html is super easy even for a web development newbie, it is possible to use the same method in drupal development, but if you follow drupal API that's not the best way to create a link. l() function comes to the stage at this moment as the drupal's way to create links.

    Example usage
    <?php 
    print l("click here","node/820");
    ?>
    
    above is exactly identical to following
    <a href="node/820">click here</a>
    
  7. url()
  8. url() generates and internal(eg: node/1) or external url.

    Example usage
    <?php 
    $path="node/820";
    $link=url($path,array('absolute'=>TRUE));
    ?>
    
  9. drupal_get_path_alias()
  10. Given an internal drupal path this function returns the alias set by admin.

    Example usage
    <?php 
    $path="node/820";
    $alias=drupal_get_path_alias($path);
    ?>
    

2 comments :

  1. You provide here Drupal Development function with example which is quite helpful to the beginners and in their application as well.

    ReplyDelete
  2. Looking at a CMS is very important I use Drupal. Because When I learned the top government websites use Durpal, it's a no brainer For hosting I went with Pantheon with Multidev What CMS did you chose?
    Drupal Hosting

    ReplyDelete