Drupal

FOSS CMS

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{

Apache Benchmarking

Technologies: 

Apache benchmarking is a tool used for benchmarking apache based websites.

The syntax for anonymous users:
ab -c no_of_concurrent_users -n no_of_requests_per_user URL

To capture the benchmarking information for authenticated users, we need to capture the cookie information for any authenticated user from the browser and provide it in a name=pair cookie value in the command

ab -c no_of_concurrent_users -n no_of_requests_per_user -C cookie_name=cookie_value URL

The data we are interested in looking for benchmarking is:

PHP Tuning

Technologies: 

For development site Change the php.ini file setting for apache:
display_errors=On so that PHP errors can be known
and change the setting
error_report = E_ALL && ~E_NOTICE (Report all errors except notice type)

Apache Tuning

Technologies: 

Enable Mod Expires
Drupal states via .htaccess that if mod_expires is on, it caches all files for 2 weeks. This means that the browser would cache static content.

Set KeepAlive On For Apache to wait until all the elements of a page requests are provided.

MaxKeepAliveRequests -> How many download requests per page.. [Check with Firebug]

KeepAliveTimeOut 15 --> How many seconds required for the page to load completely in the browser. 4 sec is a good threshold.

Theming CCK Fields

Technologies: 

There may be some situations wherein one would like to dispaly each CCK field in particular way.For example by default you have a lot of free space available in the right hand side of the node.One can theme CCK Fields so that it appears in free space.

How to do this?

MySQL Tuning Best Practices

Technologies: 

Based on video from lullabot, the following settings in my.cnf file in mysql for drupal performance and handling:

Before beginning to start using MySQL it is recommended to run the script provided by MySQL to provide some security features.
The link is: /usr/bin/mysql_secure_installation . The location can vary. It basically requests for root password, disabling remote root login, remove anonymous users and other security setting.

Character Set: utf8. Drupal mostly expects the default character set to be utf8.
default_character_set=utf8

Display different home page based on User Roles

Technologies: 

Use Panels!
Create layout as you like and set the panel as the home page of the site in the Site Information section. Add content in the panels with view permissions for specific user role. This would ensure that a user would be able to view specific content based on their role.
Take care to ensure that a user does not have 2 roles :)

How to get the total number of nodes displayed by the view

Technologies: 

There are times where we would want to view the total number of rows or data rows displayed in a view. The best way to do is to add a PHP snippet in the header section of a view. Fortunately, drupal views allow for adding code snippet into header and footer of a view.

However, this code snippet will work only if pager is enabled in the view. The code snippet:

<?php
  $view
= views_get_current_view();
  print
"<h4> Total Blood Banks: ".$view->total_rows." </h4>";
?>

In-case, you would want to get data from a particular view, you can provide the snippet: