Drupal Dump

Drupal Snippets, Hacks and more....

495 posts since 2011

Quickly import DB on server

If you want to quickly import DB on your linux do this combo command, before just create that Database you want to have.

gunzip < data.mysql.gz | mysql -u root -p db_name

Quick out of memory or out of time debugging

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
ini_set('memory_limit', ''728M');
ini_set('max_execution_time', 120)

add this to index.php and see what is the output, act on that.

Huge load time, over 700 MB of memory and WSOD when adding/editing node

On a big site with more then 20 000 nodes, when creating particular node I first experienced WSOD, then as I reported errors, max_time_execution passed, then when this was raised memory limit passed. So I raised both to high limits, memory to 1gb and maxe_execution time to forever. Then I run the node/add and what I have to see is

Remove some crayze chars like ? ?

I was doing an import of data from some old DB, that had some chars like ? ? in them, so how to remove them and keep all the other UTF-8 chars in them?

I used iconv("UTF-8", "UTF-8//IGNORE", $vars). And it saved me from going crayze myself.

Create unique username before user_save

When using user_save to create new user, you want to have username unique, if you are importing data or just want to be safe that someone hasn't have chosen username you already have in DB (and not to get nasty error PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry...). Here is some nice code to run on that username variable before saving an object.

Krumo/Devel output in watchdog

If you want to have Krumo/Devel style output in watchdog, just do this.

watchdog('debug',kprint_r($var, TRUE, NULL));

$_SERVER['HTTPS']='on' on reverse proxies - nginx and SSL

When using nginx and SSL and you have a problem that CSS or JS files are using HTTP in their urls and everything else is in HTTPS. The thing you need to do is use this magic

$_SERVER['HTTPS']='on';

put it in settings.php and also if you have wordpress, do the same, put it in wp-config.php it will do the same and solve the same problem.

Attaching Extra or Pseudo Fields to any Entity in Drupal

This thing is so cool and only recently I found about it. You can create pseudo fields with your entites, most often content types. So you can make some add, edit, delete buttons or any kind of forms on your entites or nodes. And it is pretty quick and straight fwd solution.

Update and insert only specific fields of your entity

TO not need to use hook_entity_presave and save whole entity try this

//Get the id of your field
$field_name = 'name_of_your_field';
$info = field_info_field($field_name);
$fields = array($info['id']);

//Execute the storage function
field_sql_storage_field_storage_write($entity_type, $entity, 'update', $fields);