Skip to content

Instantly share code, notes, and snippets.

<script type="text/javascript">
jQuery(document).ready(function () {
if(jQuery.browser.msie){
if(parseFloat(jQuery.browser.version) < 9){
//Versions of IE less than 8
window.location = "http://google.com";
}
}
@esafwan
esafwan / drupal_render_view.php
Last active December 22, 2015 13:29
Load view and then set its display, limit how much to load, pass argument and then render all programatically. Easy snippet or example code.
<?php
//Load the view.
$view = views_get_view('news_master');
//set the display.
$view->set_display('block');
//set the limit.
$view->set_items_per_page(8);
@esafwan
esafwan / drupal_create_block.php
Created September 7, 2013 23:07
Create drupal block in code or programatically. The code has to be placed in module file.
<?php
/**
* Implements hook_block_info().
*/
function mymodule_block_info() {
$blocks = array();
$blocks['my_block'] = array(
'info' => t('Master News Sticky'),
);
return $blocks;
@esafwan
esafwan / AppDelegate.h
Last active December 26, 2015 14:09
Getting Value from test field in objective C and setting it to another textfield.
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
//The UI have a pushbutton, textfield1 & textfield2.
//create an action for push button with below code. (ctrl + drag to AppDelegate.h)
- (IBAction)button:(id)sender;
@esafwan
esafwan / tar-exclude-folder.sh
Created November 29, 2013 12:11
Simple command in Linux terminal to tar file by excluding individual directory within which you don't want.
tar --exclude='path/to/exclude' -zcvf filename-you-want.tar.gz /directory/to/archive
#also possible in multiple
tar --exclude='path/to/exclude1' --exclude='path/to/exclude2' -zcvf filename-you-want.tar.gz /directory/to/archive
@esafwan
esafwan / ccl_block.module
Last active December 30, 2015 05:48
Add custom link to the contextual links of block in Drupal 7.
<?php
function ccl_block_contextual_links_view_alter(&$element, &$items) {
if (isset($element['#element']['#block']) && $element['#element']['#block']->delta == "custom_module") {
$element['#links']['edit_f_block'] = array(
'title' => 'Change Content',
'href' => url('admin/config/front/block', array('absolute' => TRUE)),
);
}
}
@esafwan
esafwan / views_performance_improve.php
Last active December 30, 2015 08:19
View Query Alter in Drupal to dramatically increase the query performance. This converts left joins to inner join and disables distinct query.
<?php
/**
* Implements hook_views_query_alter().
*
* This disables distinct and turns LEFT joins into INNER joins. This increases
* performance.
*/
function my_module_views_query_alter(&$view, &$query) {
if ($view->name == "my_view") {
$query->table_queue['content_type_article']['join']->type = 'INNER';
@esafwan
esafwan / add_product.module
Created December 6, 2013 17:37
Function to add product to cart programatically in drupal commerce.
<?php
function my_module_product_add($pid,$quantity=1){
if ($product = commerce_product_load($pid) ) {
global $user;
//uid of the logged in user
$uid = $user->uid;
$line_item = commerce_product_line_item_new($product, $quantity);
$line_item->field_ticket_type['und'][]['value'] = 0;
@esafwan
esafwan / unblock_user.module
Last active December 30, 2015 14:09
Add unblock user to Views Bulk Operation of User. By default only block user exist. It is possible via "Modify entity values" but its confusing to most. And includes one additional step.
<?php
/* Unblock user in bulk operations
* Implements hook_action_info().
*/
function unblock_user_action_info() {
return array(
'unblock_user_unblock_action' => array(
'label' => t('Unblock the user'),
'type' => 'user',
<?php
function my_module_html_head_alter(&$head_elements) {
foreach ($head_elements as $key => $element) {
if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
unset($head_elements[$key]);
}
}