Nov 07

How to access Drupal functions and sesions in a stand alone PHP script

If you want to access Drupal functions and sessions in your stand alone PHP scripts which is present any where in your project directory then how you will do this?

Let say you are going to create one websevices in drupal thats mean you have to create one PHP stand alone file then how you will access the Drupal functions?

First of all I am clearing this that this solution is for Drupal 7, not sure this will work in Drupal 6 or not, as I have not tested this in Drupal 6 environment.

Below are the code you have to write in you PHP stand alone file.

$path = getcwd();
$pos = strpos($path, 'sites');
$path = substr($path, 0, $pos);
$path = explode('\\', $path);

$length = sizeof($path);
unset($path[$length - 1]);
$path = implode('/', $path);

// Get the base path
define('DRUPAL_ROOT', $path);

//Include the bootstrap file for using the DRUPLA function in our file
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Now you can access any function of Drupal 7 in this stand alone PHP file :)

Aug 05

How to create new block region in Drupal 7

Then following are the steps demonstrate how to create a new block region in Drupal 7.

Step 1:
Open up your theme’s .info file and add the following line:
regions[new_region] = New Region

Step 2:
Now go to the your site url/admin/structure/block and select which block you want to show in your newly create region, and save the your choice.

Step 3:
Then you’ll need to print out your new region in your page.tpl.php file which goes something like this now:

<?php if ($page['new_region']): ?>
<div id=”new_region”><div>
<?php print render($page['new_region']); ?>
</div></div>
<?php endif; ?>

Aug 05

Drupal node indexing

How to index a node at time of its creation/modification in Drupal7?

Drupal would not indexing nodes at the time of its creation/modification.
At the time of executing the cron script drupal indexing all the nodes those are yet not indexed.
That is why if we want to search a node in site(at user end) just after its creation/updation we can’t get this node as the node is not indexed.So our objective is indexing node at the time of its creation/updation(means just after saving it into the database).Drupal provides us a hook(hook_node_insert and hook_node_update) which is execute just after node saved into the database and also provide a function(_node_index_node) which can index a node.
So, we just need to add the following functions in any custom module.
/* Implementing hook_node_insert */
function {module_name}_node_insert($node) {
 //calling the the below function to index the current node.
 _node_index_node($node);
}
/* Implement hook_node_update */
function {module_name}_node_update($node) {
 //calling the the below function to index the current node.
 _node_index_node($node);
}
*NOTE: replace {module_name} by your module name.
After adding the above functions if we create/updation any node it will immediately indexed and you are able to get the created node in your search result at user end.