term of taxonomy used as menu item in drupal

sometime our requirement is different that Drupal behaviour.
client wants to put some Taxonomy term as Primary menu. The taxonomy menu module adds links to the navigation menu for taxonomy terms.

(1) Install taxonomy menu module
(2) make some setting of taxonomy menu module

now we want to change path of these term in menu. it provides some hook for ..

/**
* Implementation of hook_taxonomy_menu_path.
*
* @return array
* function name => Display Title
* a list of the path options.
*/
function ess_public_taxonomy_menu_path() {

$output = array('ess_public_menu_path_default' => t('Ess Category Search'));

return $output;
}

/**
* Callback for hook_taxonomy_menu_path
*/

function ess_public_menu_path_default($vid, $tid) {
//if tid = 0 then we are creating the vocab menu item format will be taxonomy/term/$tid+$tid+$tid....

if ($tid == 0) {
//get all of the terms for the vocab
$path = "ess_public/search_result/category/";
}
else {
$path = taxonomy_term_path(taxonomy_get_term($tid));

if (variable_get('taxonomy_menu_display_descendants_'. $vid, FALSE)) {
//Use 'all' at the end of the path
$term = taxonomy_get_term($tid);
$path = "ess_public/search_result/category/".$term->name;

if (variable_get('taxonomy_menu_end_all_'. $vid, FALSE)) {
// $path .= '/all';
$term = taxonomy_get_term($tid);
$path = "ess_public/search_result/category/".$term->name;
}
else {
//we wait to run this instead of durning the if above
//because we only wan to run it once.
$terms = taxonomy_get_tree($vid, $tid);
foreach ($terms as $term) {
$tids[] = $term->tid;
}
if ($tids) {
$end = implode(' ', $tids);
$path .= $end;
}
}
}
else
{
$term = taxonomy_get_term($tid);
$path = "ess_public/search_result/category/".$term->name;
}
}

return $path;
}

MySql sorting fields in which certain order.

MySql provides nice functionality to sort fields in certain order. This is very useful in scenarios where we have an enum Field represented as varchar and the values that it can hold are fairly static.

CHANNELS
——————————–
| ID | NAME | TYPE |
——————————–
TYPE field is a varchar that can take only SD, HD and 3D.

PROBLEM:

To get a list of channels sorted by TYPE in an order of SD, HD and 3D. This can be done in couple of different ways:
-- Using by FIELD
SELECT * FROM CHANNELS ORDER BY FIELD(TYPE, 'SD', 'HD', '3D');

-- Using by FIND_IN_SET
SELECT * FROM CHANNELS ORDER BY FIND_IN_SET(TYPE, 'SD,HD,3D');

programmatically create or add new Taxonomy Terms in drupal

To create a new taxonomy term through PHP you can use taxonomy_save_term(&$form_values) which is mainly intended for use as a helper function to by the real drupal taxonomy add term form.
The relevant term details are passed as parameter and the Term Id of the newly created taxonomy term is placed back in this array and can be accessed by the calling code.

here I explain small code to assign taxonomy term to node if taxonomy is new , first you save in taxonomy in db.

/**
* Save Node
*/
foreach($arrNodeIds as $intNodeId){
$objNodeInfo = node_load($intNodeId);
$arrTermsIds = getTermIds($_POST['txt_tag_val_'.$intNodeId]);
$objNodeInfo->taxonomy = $arrTermsIds;
node_save($objNodeInfo);
}
/**
* See If the term exists in the chosen vocabulary and return the tid; otherwise, add a new record.
* @param $strTerms
* - Terms Name
* @return
* - Terms Ids
*/
function getTermIds($strTerms){
$arrTerms = explode(",",$strTerms);
if(!is_array($arrTerms)){
$arrTerms[0] = $strTerms;
}
foreach ($arrTerms as $strTerm){
if(strlen($strTerm) > 0){
$possibilities = taxonomy_get_term_by_name(trim($strTerm));
$intTermTid = NULL;
foreach ($possibilities as $possibility){
$intTermTid = $possibility->tid;
}
// Check new term , If yes then insert this new term in db
if (!$intTermTid){
$arrParam = array('vid' => 2, 'name' => $strTerm);
$status = taxonomy_save_term($arrParam);
$intTermTid = $arrParam['tid'];
}
$arrTermIds[] = $intTermTid;
}
}
return $arrTermIds;
}

/**
* add a new record as Term of Texonomy.
* @param $arrParam
* - param (vid , name)
* @return
* - status
*/
function addTaxonomyTerm($arrParam){
$vid = $arrParam['vid'];
$name = trim($arrParam['name']);
return taxonomy_save_term($form_values);
}

Create block by drupal code

If you most likely want to generate a block using code / programming , it is simple develope following code for that as I explain here.

Once you know the module and the delta, It is simple for creating the block.
The trick is you need to send an object with the right parameters. The code below should get you started. Remember this code may be different in Drupal7

/**
* Load drupal Block By Delta & Module
*
* @param $strModule
* - Module Name
*
* @param $intDelta
* - Module Name
*
* @return
* - Themed Block HTML
*/

function loadDrupalBlock($strModule, $intDelta) {
$block = new stdclass(); // empty object
$module = $strModule;
$delta = $intDelta; // could also be a string

$array = module_invoke($module, 'block', 'view', $delta);
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->$k = $v;
}
}
$block->module = $module;
$block->delta = $delta;
return theme('block', $block);
}


This is calling by this way
$staticBlockHtml = loadDrupalBlock('block', 7);

Delete many thousands of nodes

We recently had reason to delete over a thousand Drupal 6 nodes of the same type,
but we wanted to leave all other content on the site intact.

What do you do when you want to delete multiple nodes from a drupal site?
You go to admin » content and then delete nodes one by one or delete up to 50 nodes at one time?

In this example we will delete all nodes of a particular type (page), It is quick way to delete


$node_type = 'image_album'; //fetch the nodes we want to delete
$result = db_query("SELECT nid FROM {node} WHERE type='%s'",$node_type);
while ($row = db_fetch_object($result)){
node_delete($row-?>nid);
$deleted_count+=1;
}
//simple debug message so we can see what had been deleted.
drupal_set_message("$deleted_count nodes have been deleted");

conformation alert using javascript

Sometimes it is useful to display a confirmation message before processing any action. Some application often asks for confirmation before doing something irreversible, like deleting content. 
the user to conform once again the deleting process. Here the user can change his mind and can cancel the operation or conform his action by clicking the submit button.

function confirmDelete(item){
 var strMsg = "Are you sure you want to delete this "+item+" ? ";
 var intAgree = confirm(strMsg);
 if (intAgree)
  return true ;
 else
  return false ;
}


Pass an array from an HTML form using Hidden field

It is simple way to pass your array using hidden field of form and get to another side  where you want to use this array's value


Snippet

Create Array

$arrNodeIds = array();
foreach ($data as $k => $objNode) {
$arrNodeIds[] = $objNode->nid;
}

Assign array to hideen field of form
<form method="post" action="/ess_upload/save_images">
<input type="hidden" name="hdnArrNodeIds" value="">
</form>

Get value from other side

$arrNodeIds = unserialize(urldecode($_POST["hdnArrNodeIds"]));
foreach($arrNodeIds as $intNodeId){
    echo $intNodeId;
}