Tuesday 18 November 2014

Print a Block Programatically



<?php
$block
= module_invoke('module_name','block_view', 'block id' );
print
$block['content'];?>



For Example which Views Module as  



<?php
$block
= module_invoke('views','block_view', 'article_block' );
print
$block['content'];?>



Similarly for search form 


<?php
$block
= module_invoke('search/form','block_view', 'search/form');
print
$block['content'];?>



Print a Logo in Drupal 7

<?php if ($logo): ?>
                <a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>" rel="home" >
                    <img src="<?php print $logo; ?>" alt="<?php print t('Home'); ?>" />
                </a>
            <?php endif; ?>

Find the Sql Version of Drupal Version using database

select name,type,info from system where type = 'module' and name = 'node';

To enable/disable all contributed Modules using Database

 UPDATE `system` SET `status` = '0' WHERE `filename` LIKE '%sites/all%' AND `status` =1


UPDATE `system` SET `status` = '0' WHERE `filename` LIKE '%profiles/opigno_lms/modules/contrib%' AND `status` =1

Monday 17 November 2014

Cache Techniques for Drupal 7

There are number of ways we can integrate our drupal website with caching. Some of best used techniques are by using memcache, varnish and boost.

By using memcache we will be able to store data fetched by our mysql queries in our server. This can improve our sites performance to a good extent. All the queries executed by our drupal code will be using this caching technique for storing the result set without executing the sql queries again and again. This caching requires an api named memcached along with php package memcached and a contributed module memcache which is available in drupal official website.

It works in a very simple way by storing the results returned by mysql server in a key and value pair for each sql query. For example for storing user row we will use a key "userrow:" and by the following function we can use memcache.

Normal way without using memcache:
function get_foo(int userid) {
data = db_select("SELECT * FROM users WHERE userid = ?", userid);
return data;
}

And memcached way:
function get_foo(int userid) {
/* first try the cache */
data = memcached_fetch("userrow:" + userid);
if (!data) {
/* not found : request database */
data = db_select("SELECT * FROM users WHERE userid = ?", userid);
/* then store in cache until next get */
memcached_add("userrow:" + userid, data);
}
return data;
}

In the above example the user row is fetched with the help of a key called userrow: and whenever this function is called then first our code checks if the result set is stored in memcache or not and based on the memcache results for the query our sql query is executed.

This reduced our query execution time and load on our mysql server. So for integrating memcache in drupal website we don't need to change all the drupal functions we just need to install a contributed module memcache available at URL: https://drupal.org/project/memcache. Before that we need to install memcached in our server and php PECL library memcached.
Installation on ubuntu server and php 5.2 or greater is very simple by using commands as below:
sudo apt-get install memcached
sudo apt-get install php5-memcached
cd path_to_your_drupal_website
drush dl memcache
//Install memcache module in your drupal site by running command in your drupal installation by drush or you can follow regular steps of your module installation in your site.
And then restart your apache server before memcache actually records your data for your drupal site.

As memcache provides caching for queries executed by drupal similarly varnish helps us to store html, css and javascript of our drupal site. Varnish is an http accelerator which accelerates the content delivery to users browser. It stores the content delivered by apache server for a specific expire time i.e. cache expire time. It increases the site delivery of content to upto 3000 times for static pages but it will not work for logged in users or authenticated users. We have to adopt different methods for improving authenticated users site performance.
Here comes the role of block caching and view caching. Drupal has best utilized by its blocks and views and these can be cached by inbuilt caching supported by drupal. All the blocks and views rendered can be cached by drupal itself by enabling the block caching in drupal configuration and views configuration.

The block level caching can be altered by a contributed module named block cache alter which is available in drupal site URL: https://drupal.org/project/blockcache_alter. This module further increases the caching level to a bit higher where admin can select which level of caching he needs for a specific block.