You are hereModules
Modules
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.
$name = '';
if ($orig_name) {
/* filter characters (look at user_validate_name) */
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));
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.
Read more about it here
https://www.computerminds.co.uk/drupal-code/add-stuff-node-and-configure...
http://www.vdmi.nl/blog/attach-extra-or-pseudo-fields-any-entity-drupal
Update and insert only specific fields of your entity
TO not need to use hook_entity_presave and save whole entity try this
$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);
Using CRUD with RESTful WS module
There is not much docs about this, but watching this video
https://www.youtube.com/watch?v=DJQnDZCbUHk
will help you with testing your drupal site and RESTful WS and Advanced REST Client.
For more info, look this one
https://www.youtube.com/watch?v=a2nHIWlCXSw
has all the drupal service modules in it and good examples how to use them.
One big GOTCHA is that.
X-CSRF-TOKEN is working on my localhost as
Enable errors and logging with drush or with settings.php
You have error, your site is not accessable and you need to see what is the problem but logging and errors are set to none.
Type this with drush
drush vset error_level x
Where x is 0/1/2, depending on what you want
0: none
1: errors and warnings
2: all
You can also do the same with settings.php, adding to it this
$conf['error_level'] = 2;
Varnish makes forms like node add/edit "timeout" or not submit
I had a strange problem with Varnish, when users used form for longer period, like they inputed data and then submitted if it was 120 seconds or more, edit page would just refresh and nothing would be submitted. So I played with TTLs https://www.linode.com/docs/websites/varnish/getting-started-with-varnis..., then with pipe_timeout
Drupal commerce checkout redirect pause
When using different payment methods for checkout, sometimes you will have a payment gateway that will redirect you to different page, like paypal. But what if you want to have your frontender a chance to look at a page before redirect to style it?
You will need to stop the redirect, to do that just find that payment method info function in module, like the one below and change
'offsite_autoredirect' => TRUE, to 'offsite_autoredirect' => FALSE,
$payment_methods = array();
Create a view with specific number of columns per row without table
My frotend guy asked me to put 3 columns of data per one row in views output so he can put zen grids over them. Not sure if he really needs that but I made it for him with a view template. Selected unformmated display style type, created template
views-view-unformatted--taxonomy-term.tpl.php and added the code that detects when number 3 occurs, or to say what it in code, we check the ID and if it is divided by 3 and has no leftover (% module operator) then we detect it is time for a new group. Here it is: