Friday, February 18, 2011

Customizing the Search form - Drupal


To change the label text and text inside the search box and the text on the submit and changing the Submit button image, you can use the following function to do the customizations. You have to add this code in template.php file.

function mytheme_preprocess_search_theme_form(&$vars, $hook) {
// Change the "Search this site" label from the form.
$vars['form']['search_theme_form']['#title'] = t('googling');

// Set a default value for text inside the search box field.
$vars['form']['search_theme_form']['#value'] = t('Find it ..');

// Add a custom class and placeholder text to the search box.
$vars['form']['search_theme_form']['#attributes'] = array('class' => 'NormalTextBox txtSearch', 'onblur' => "if (this.value == '') {this.value = '".$vars['form']['search_theme_form']['#value']."';} ;", 'onfocus' => "if (this.value == '".$vars['form']['search_theme_form']['#value']."') {this.value = '';} ;" );

// Change the text on the submit button
//$vars['form']['submit']['#value'] = t('Go');

// Rebuild the rendered version (search form only, rest remains unchanged)
unset($vars['form']['search_theme_form']['#printed']);
$vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);

$vars['form']['submit']['#type'] = 'image_button';
$vars['form']['submit']['#src'] = path_to_theme() . '/images/search.gif';

// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
$vars['search']['submit'] = drupal_render($vars['form']['submit']);

// Collect all form elements to make it easier to print the whole form.
$vars['search_form'] = implode($vars['search']);
}

Wednesday, February 16, 2011

Dfont module - Drupal

The Dfont module is very useful. This module is very useful in cases like when the Client needs the Site in particular font which is also a rare font which wont be available in most of the Users machine, so in that case only the default font will get called this is where the issue raises. In this case few options available are,
  •  We can add the fonts as images, but you cant add every text as Images then the site will be very slow.
  •  Another thing is embedding flash which is also not a good option.
  •  Compartively the Dfont module option is Good. You need to add that font to the server, from there the font will get called, the user's machine doesnt need to have that Font. Therefore every user can view the site in Client requested Font.

Steps for installing this module,
  •  Enable the module asusual.
  •  After enabling, go to the dfont settings page (admin/settings/dfont), there also the instructions will be available.
  •  The Next step is you have to Upload the .ttf and .eot files, for each font, to directory sites/default/files/fonts.
  •  Then Scroll down and click Generate font styles.
  •  Then Enable dynamic font filter in admin/settings/filters and Refresh this page to ensure your fonts are rendered below.

You can download the fonts from http://www.tti-us.com/uvn/p_buyscript.htm. Also to convert the fonts to .eot format, you can use this link to do that http://www.kirsle.net/wizards/ttf2eot.cgi.

Then you have to add some Css properties for including the font family, size etc ...
Ex:
h1, h2, h3, h4, h5, h6 {
   font-family: UVNDamCuoi_H;
}

"UVNDamCuoi_H" is the Font name (which I have used). So after including this, automatically all the header tags (h1,h2 etc ..) will be displayed in the "UVNDamCuoi_H" font. You can also use the default options like [dfont=df1m],[dfont=df1l],[dfont=df1x] in the dfont settings page for various presets.

Tuesday, February 15, 2011

Floating Blocks - Drupal

The Floating block module allows you to keep html blocks or contents, selected using jquery selectors in a fixed position on the page as you scroll.  This means that when a user scrolls the browser, some parts of the site can be maintained in the user's view by fixing their position.

Install the module asusual.
Go to the Floating block admin settings page (admin/settings/floating_block), there you will find a Textarea where you can enter the CSS classes or Id's of the blocks which needs to be floating.

Examples :
#block-chatroom-0
#search
#myarea

It has to be entered one by one as above, as you can see the first and second Css Id's are drupal blocks from contributed modules while the third one is customely added DIV by me, which means it also works for the Custom DIV's or regions not only the Drupal Blocks.

Friday, February 11, 2011

Drupal - Module hooks invoke procedure


How does all the hooks inside all the modules (custom & contributed) gets invoked and executed,

module_implements() - this is the function which Determine which modules are implementing a hook.

function module_implements($hook, $sort = FALSE, $refresh = FALSE) {
  static $implementations;

  if ($refresh) {
    $implementations = array();
    return;
  }

  if (!isset($implementations[$hook])) {
    $implementations[$hook] = array();
    $list = module_list(FALSE, TRUE, $sort);
    foreach ($list as $module) {
      if (module_hook($module, $hook)) {
        $implementations[$hook][] = $module;
      }
    }
  }


In this function it will call the module_list() function which returns all the enabled modules, further on which module_hook() function is called which checks whether the module uses that hook function

function module_hook($module, $hook) {
  return function_exists($module .'_'. $hook);
}

Tuesday, February 8, 2011

Perfomance tunings for Drupal Websites


Steps to follow to increase the site's performance,

Install a PHP script cache - eAccelerator

Turn on MySQL query caching

Turn on Drupal page caching

Compress your content

combine your CSS using Css aggregator

Combine your JS using js aggregator

Y-slow can be used for checking the performance.

For more information, visit the following link

http://nadeausoftware.com/articles/2007/01/essential_performance_tuning_drupal_web_sites

Monday, January 24, 2011

Downloading a File using CURL

/*
This is usefull when you are downloading big files, as it
will prevent time out of the script :
*/
set_time_limit(0);
ini_set('display_errors',true);//Just in case we get some errors, let us know....

$fp = fopen (dirname(__FILE__) . '/a.rss', 'w+');//This is the file where we save the information
$ch = curl_init('http://www.webdigity.com/rss.php');//Here is the file we are downloading
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Friday, January 21, 2011

Creating an RSS Feed in PHP

Query from the DB to retrieve the contents. 
$query = "SELECT title, link, description, pubDate FROM
   articles LIMIT 10";
$result = mysql_query($query); 
Then add the XML format as shown below and finally print the xmlString variable. 
 

Generating a PDF using FPDF


 require('fpdf.php');

class PDF extends FPDF
{
//Page header
function Header()
{
    //Logo
    $this->Image('logo.png',20,10,20,20);
    //Arial bold 15
    $this->SetFont('Arial','B',15);
    //Move to the right
    $this->Cell(70);
    //Setting the text in the centre 'C'
    $this->Cell(50,10,'Generating PDF',0,0,'C');
    //Line break
    $this->Ln(20);
}
}

//Instanciation of inherited class
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
for($i=1;$i<=10;$i++)
    // printing a text center aligned.
    $pdf->Cell(100,10,'Printing line number '.$i,0,1,'C');
$pdf->Output();
?>

Thursday, January 20, 2011

Email address validation in PHP using REGEX

Code to validate:

if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',$_REQUEST['email'])) { 
    echo 'Valid'; 
       } else { 
    echo 'Invalid'; 
       }

Regular Expression Samples

Regx Expression for removing repeated words (case insensitive)

$text = preg_replace("/\s(\w+\s)\1/i", "$1", $text);

Output : ‘Keep your your head’ becomes ‘Keep your head’

Password complexity

Tests if the input consists of 6 or more letters, digits, underscores and hyphens.
The input must contain at least one upper case letter, one lower case letter and one digit.

'\A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])[-_a-zA-Z0-9]{6,}\z'

Customizing the Search form - Drupal

To change the label text and text inside the search box and the text on the submit and changing the Submit button image, you can use the fo...