Monday, March 9, 2009

Add filter format to textarea in Drupal custom form

To add filter format to textarea in Drupal custom form, We can use following code

$form['']['format'] = filter_form($node->format);

R.Navaneethakrishnan.

Saturday, March 7, 2009

Exclude one CCK from drupal search

To exclude one cck in drupal search please follow as in the given steps.

1.Add this into hook_form in your custom module

case 'search_form':
$excluded_types = variable_get('search_excluded_node_types', array());
$types = array_map('check_plain', node_get_types('names'));
foreach($excluded_types as $excluded_type) {
unset($types[$excluded_type]);
}
$form['advanced']['type']['#options'] = $types;
$form['basic']['inline']['keys']['#prefix'] = '<div id="pref">';
$form['basic']['inline']['keys']['#suffix'] = '</div>';
// echopre($form['basic']['inline']);
break;



2.Add this in to hook_search in your custom module

function <modulename>_search($op = 'search') {

if ('admin' == $op) {
$form = array();
$form['search_excluded_node_types'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Excluded Node Types'),
'#default_value' => variable_get('search_excluded_node_types', array()),
'#options' => node_get_types('names'),
'#size' => 9,
'#description' => t('Node types to exclude from search results.'),
);
return $form;
}
}

3.Add this in to hook_db_rewrite_sql in your custom module

function <modulename>_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
if ($query == '' && $primary_table == 'n' && $primary_field = 'nid' && empty($args)) {
$excluded_types = variable_get('search_excluded_node_types', array());
if (!empty($excluded_types)) {
$where = " n.type NOT IN ('". join("','", $excluded_types) ."') ";
return array('where' => $where);
}
}
}


Note : This is for Drupal 5.x

R.Navaneethakrishnan.

Wednesday, February 18, 2009

Module to backup drupal site

Backup and Migrate Module[http://drupal.org/project/backup_migrate]. is to take backup your drupal site. :)

R.Navaneethakrishnan

Drupal user table : Users list joined before a week

Drupal stores dates in Unix time format. To find email addresses for users who joined after a certain date Use SQL query in phpmyadmin and then exported into an OpenOffice spreadsheet:

SELECT mail, FROM_UNIXTIME(created) FROM users WHERE created > 1199145600 LIMIT 500;

That returns 500 emails from users who registered since January 1, 2008. The FROM_UNIXTIME(created) converts the Unix formatted time into something human-readable.

Thursday, February 12, 2009

Effective Sample code to get category tree in drupal or php

<?php
echo '<select name="category">
<option value="0">Root</option>';
echo $allcats = getTree();
foreach($allcats as $key=>$value) {
echo "<option value='$key'>$value</option>";
}
echo '</select>';
$con = mysql_connect("localhost","root","password") or die();
mysql_select_db('test',$con)or die();
function getTree($id=0) {
static $cates = array();
static $times = 0;
$times++;

$result = mysql_query("SELECT category_id,category_name FROM recusrsive WHERE parent_id=$id ORDER BY category_name") or die();
while($row = mysql_fetch_assoc($result))
{
$cates[$row['category_id']] = str_repeat("&nbsp;&nbsp; ",$times-1)."|-".$row['category_name'];
getTree($row['category_id']);
}
$times--;
return $cates;
}
?>




Table Content

category_id parent_id category_name
1 0 CMS
2 0 Blogs
3 0 Forums
4 0 E-Commerce
5 1 Joomla
6 1 Mambo
7 6 Templates
8 6 Mods/Components
9 4 OSCommerce
10 2 Wordpress
11 10 Themes
12 10 Plugins

Wednesday, February 11, 2009

To add taxonomy in drupal custom form.

By using taxonomy_form() we can form a texonomy in form.

For Ex:
$vid=4;
$category1 = taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy');
$form['taxonomy_info']['taxonomy1'] = array(
'#type' => 'select',
'#title' => t('Terms for the Website'),
'#options' => $category1,
'#description' => t('Website category'),
);

R.Navaneethakrishnan
http://navaneethakrishnan-drupal.blogspot.com/

Wednesday, January 28, 2009

Drupal Set Taxonomy Breadcrumb Programmatic

To Set Breadcrumb to Taxonomy Programmatically.
Add this code in Template.php

function theme_breadcrumb($breadcrumb) {
if (!empty($breadcrumb)) {
$cat = taxonomy_get_term(arg(1)) ;
if(taxonomy_get_parents($cat->tid) == array()) {
$breadcrumb[] = l($cat->name , str_replace(" ","-",(strtolower($cat->name))));
}
else {
$parent_term = taxonomy_get_parents($cat->tid);
foreach($parent_term as $key => $value){
$name = $parent_term[$key]->name;
}
$breadcrumb[] = l($name , str_replace(" ","-",(strtolower($name))));
$breadcrumb[] = l($cat->name , str_replace(" ","-",(strtolower($name))."/".str_replace(" ","-",(strtolower($cat->name)))));
}
}
}