Drupal Dump

Drupal Snippets, Hacks and more....

Bash script for cloning database on server

If you need to copy DB, lets say from production to develop you can use this script and call it like "develclone.sh original_db cloned_db"
For faster cloning it will exclude cache tables data.

Messages api, make message programmatically for orders

I wanted to add a message to orders when I did some auto approving of orders. To add some trace of that you need to load user which will be the one that did that. Send $values that will be used for token replacement in message and also select which message type you are going to use. In here I made a clone of default message "commerce_order_admin_comment" and used that.

How to style elements that change ids when ajax actions replace them

In drupal, when you have ajax refreshing some content, elements get new ids when they refresh. So some input element that was "edit-get-score"  becomes "edit-get-score-2" then ""edit-get-score-3" etc.
So to match them properly by ID you can use this. Which is CSS attribute selector using sort of a wildcard

Life of a view and what and where to hook or alter

Often I have a need to alter a bit output of a view, either the markup or just query, it's important to know where to hook in a life of a view, here is the list of hooks and alters.
 

Get list of all the hooks your site currently uses

In includes/module.inc, find the module_implements() function (around line 715 in Drupal 7). At the top of the function, just add this below and you will get huge output of all the hooks that are called.

 

function module_implements($hook, $sort = FALSE, $reset = FALSE) {
  drupal_set_message("hook_$hook");
  ......

 

Find out which theme function to call to alter each element on page

This is a hack on how to get output of which theme function to call to alter each element on page. So you will need to go to In includes/theme.inc in the Drupal 7 theme() function, around line 1161 and change $output from one line to two lines and to have this code below. You will get theme suggestions all around the page, which will be messy but will help you find what you need.

Add a field to entity programmatically

Wanting to add field to some entity (we here pick order entity) by code, for example when deploying to production, use this snippet of code and you will have this new field added.

Clear cache of particular entity

For debugging purposes I need to clear cache for particular drupal commerce order, but this can be done for any entity, just replace table name and id's in code below

 $table = 'cache_entity_commerce_order';
 $id ="776764";
 cache_clear_all($id, $table);