Showing posts with label drupal. Show all posts
Showing posts with label drupal. Show all posts

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;
}

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");

Check Node is Exists In Drupal

It is good way to check Node is exists or not bellow example code would be helpful 

In this example , If node is exists in our database, then It will be deleted. otherwise give a error message and redirect to perticular page.

Example :
  $bolIsNodeAvail = node_load(array('nid' => $intNodeId)); 
  if($bolIsNodeAvail != false)
  {
        node_delete($intNodeId);  
  }else{
        drupal_set_message(t("node id is not exists."), 'error');  
  }
  drupal_goto('ess_upload/album_images');
 

Insert and Update Record using drupal_write_record()

It saves a record to the database based upon the schema.

you can simply call the new drupal_write_record function telling it what database table to write to and the information to write and the function will do the rest. SO you'll find yourself writing less and less of those long db_query insert and update commands!

Insert Record To Table
This would insert all the data in the $node object into the node database table.

drupal_write_record('node', $node);

Example :
$objFile = new stdClass();
$objFile->uid = $uId;
$objFile->filemime = $arrFile['str_file_mime'];
$objFile->filesize = $arrFile['int_file_size'];
$objFile->status = FILE_STATUS_PERMANENT;
$objFile->timestamp = time();
drupal_write_record('files', $objFile);


Update Record In Table
This would update the node in the node database table with the data in the $node object 
where nid is equal to the $node->nid

drupal_write_record('node', $node, 'nid')


Example :
$strFileExtension = getFileExtension($arrFile['str_orgin_file_name']);
$strFileNewName = $objFile->fid.".".$strFileExtension;
$objFile->filename = $strFileNewName;
$objFile->filepath = $arrFile['str_rel_file_path'].$uId.DIRECTORY_SEPARATOR.$strFileNewName;
drupal_write_record('files', $objFile,'fid');

How to do saved Node and get that node Id

I was trying to create a new node programatically by inserting information in node, I used below snippet.

  Saved Node

  $objNode = new StdClass();
  $objNode->title = utf8_encode($strTitle);
  $objNode->type = 'album_image';
  $objNode->status = 1;
  $objNode->moderate = 0;
  $objNode->promote = 0;
  $objNode->sticky = 0;
  $objNode->revision = 0;
  $objNode->comment = 0;
  $objNode = node_submit($objNode);

  $intTimestamp = time();
  $objNode->uid = $uId;
  $objNode->created = $intTimestamp;
  $objNode->changed = $intTimestamp;
  node_save($objNode);
 

  How to get the nid after node_save() in drupal 6

  Previously node_save() returned the $nid but not anymore... so what do we do? 

  It's simple. You have just saved a variable as your node. in our case we use $objNode 

  intInsertedNodeID = $objNode->nid;

  It is easy to use 

How to disable resizable textarea in drupal

I am using the profile module but I remove the resizable textarea on the frontend of website.

There doesn't seem to be an option to enable/disable in drupal or even profile module so I tried implementing this:

Snippet :

function mymodule_form_alter(&$form,$form_state, $form_id){
if (
$form_id == 'user_register'){
$form['account_information']['profile_address']['#resizable'] = false;
}
}


It should only point to the profile module. I Couldn't find any other the right code.

Drupal .module extension treate as php file (syntax color) in Eclipse

Recently I started to work in Drupal. I came across strange little problem with Drupal’s module development, syntax coloring and general PHP support when it comes to files with extension different than .php or .phtml. Lucky for me,

Eclipse comes with a nice solution for these kind of situations.

All you need to do in order for Eclipse to treat .module extensions as .php is to go to

Window Menu > Preferences > General > Content Types > (Select) Text > PHP Content TYpe from the Content types area, then below under

File Associations click on Add button and write down any extension you wish. In Drupal CMS case, write down “.module”.

Now you should be able to work with .module files as with all other .php files.