Drupal

FOSS CMS

Creating PDF Document using DomPDF

<?php
/**
* Convert the HTML into a PDF and store it in a particular location.
* In this example, the dompdf library is stored under libraries folder in Drupal.
*/
function ji_custom_convert_pdf($user_html){
 
//Get path of dompdf folder under libraries
 
set_include_path(libraries_get_path('dompdf'));
  require_once
"dompdf_config.inc.php";
 
$dompdf = new DOMPDF();
 
$dompdf->load_html($user_html);
 
$dompdf->render();
 
$output = $dompdf->output();
 
file_put_contents("sites/default/files/resume/".$user->name."_resume.pdf", $output);
}
?>

--

Rules cron based condition code for allowing execution only once

Technologies: 

<?php

/*
* Part of Rules condition code that returns true if the current time during cron is between 6 am and 7 am
*/
$ji_daily_status = variable_get("ji_daily_report","1"); //Check if the variable is set, if not set it to 1 [First Time]

/*
* This fragment checks if the time is between 6 and 7, if so enables the flag
* If any other time, sets the ji_daily_report so that next time it executes when cron runs between 6 and 7
*/
$time = date('H'); // Get the current time
/* Any arbitrary time - Checks if the time is not between 6 am and 7 am */
if($time < 6 || $time > 7){

Site Benchmarking and Performance Improvement

Improving Site Performance:
1. Disable all caching in the site and do benchmark for anon user
2. Benchmark for auth user for a particular page
3. Check logs to see if any notice or error is happening in the site. Solve the issue and notice BM
4. Disable unwanted modules if any and do another benchmark. Note the diff.
5. Modify or update apache or mysql setting minor if any to see noticiable change in benchmark
6. Optimize PHP and see if noticeable difference occurs
7. Install Devel and check queries and page rendering times

Saving Form Selection using Form API

During custom form submission we would want to remember the previous selection. There are many ways to do that, which includes using Sessions and variable_set to set the default value but this is an overkill if you wanted to simply preserve/remember the selection in the next page or after submission. The best way is to use $form_state['storage'] in your submit handler to hoard values between steps.

Consider an example:
<?php
function custom_form_submit($form, &$form_state) {
$start_date = $form_state['values']['startdate'];
$end_date = $form_state['values']['enddate'];

How to Set Filters for a View in Drupal 7 programmatically

Technologies: 

Accessing a view programmatically

<?php
$view = views_get_view('view_name');
$view->init();
$view->set_display('default'); // or display id like page_1, block_1
$view->set_arguments(array($ARGS));
//display_name = 'default' or 'page' and so on
//filter_name is the name of the filter
//debug($view->display['display_name']->handler->options['filters']['filter_name']);
$view->display['default']->handler->options['filters']['date_filter']['value']['min']='2012-05-01';
$view->display['default']->handler->options['filters']['date_filter']['value']['max']='2012-06-30';

Add Sites to search in Apache Multicore Configurations

Solr is the popular, blazing fast open source enterprise search platform from the Apache Lucene project. Its major features include powerful full-text search, hit highlighting, faceted search, dynamic clustering, database integration, rich document (e.g., Word, PDF) handling, and geospatial search. Solr is highly scalable, providing distributed search and index replication, and it powers the search and navigation features of many of the world's largest internet sites.
--
Source: http://lucene.apache.org/solr/

Extract Result Data From Drupal View

Technologies: 

The code to do the trick is provided below:

<?php
    $view
= views_get_view('view_name');
   
$view->init();
   
$view->set_display('default'); // or display id like page_1, block_1
    //$view->set_arguments(array('arg_data'));
    //$view->set_arguments(array(arg(0),'arg_data2'));  // 2nd  Argument
   
$view->pre_execute();
   
$view->execute();
   
//print_r($view->result);
   
drupal_set_message("Field data value: ".$view->result[0]->node_data_field_as_it_appears_in_views_query_sql);
?>

Update for Drupal 7:

<?php

How to check if an image is present in the site or not

Technologies: 

You might want to display an image if the image is present or a template image if it does not exist. There are various ways of doing the same from client side using JavaScript or from server end.

The below example provides another way to check if the image file exists, if so display the image in HTML generated or display the template HTML in Drupal
<?php
if(file_exists($relative_file_path_in_server)){
$image_url= " < img src=".$base_url."/".$relative_file_path_in_server.">";
}else{