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.
- node_load() 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?
- truncate_utf8() truncate_utf8() function truncates a string to a specified number of characters.
- l() 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.
- url() url() generates and internal(eg: node/1) or external url.
- drupal_get_path_alias() Given an internal drupal path this function returns the alias set by admin.
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 ?>
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 ?>
Example usage
<?php print l("click here","node/820"); ?>above is exactly identical to following
<a href="node/820">click here</a>
Example usage
<?php $path="node/820"; $link=url($path,array('absolute'=>TRUE)); ?>
Example usage
<?php $path="node/820"; $alias=drupal_get_path_alias($path); ?>
You provide here Drupal Development function with example which is quite helpful to the beginners and in their application as well.
ReplyDeleteLooking 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?
ReplyDeleteDrupal Hosting