Drupal Dump

Drupal Snippets, Hacks and more....

495 posts since 2011

Altering weight of elements in arrays used for rendering

When changing weight of element in array, it is not enough to just change weight, #sorted must be set to FALSE so elements are resorted.

$variables['form']['element']['#weight'] = -10;
$variables['form']['element']['#sorted'] = FALSE;

Contextual filter(views) for date ranges

I was building a view for RestWS to output JSONon data, to have date show for specific date range you need to pass filter range. To do that you need to pass filter as two dates, with separator beeing "--" e.x. 2015-06-24--2015-06-28

Or in full link it would be

Add to cart form, change output - separating attributes from buy button

This one was not easy. Changing product display, which is node is possible in different ways, but then you have a field_product_reference, which outputs buy button and attributes buttons. So you moved around price in your node template, body/description etc, but you want to separate attributes changing widget and buy. You just can't.

Choosing which PHP to run with Drush, changing CLI php

Using drush on mac with mamp gave me some problems with default setup. I didnt set
DRUSH_PHP variable anywhere so default php was used, also with that no php.ini setting was set and in my drush status I got empty "Drush configuration" variable. So to set this up, I needed to add this to either .profile or to .bash_profile (depends of load order, later goes first if you have it)

Changing Product attributes in drupal commerce, maintain data with ajax reload

What I needed is to preprocess this whole group of data, that consist add to cart button and attributes fields. "Add to cart form" is basically the one that consists attributes and add to cart button. To do that the only way to make it work with AJAX is with form alter. So change the forms data in

Get address lat and long with google api

This will give you a nice array of all the data.

 $request = drupal_http_request('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA');
 $out = drupal_json_decode($request->data);

Change Views filters, add dynamic date value as range

Using EVA field added to Order entity I needed to use order created date as starting range and ending range for dates.
UI by default doesn't offer this in views, so some preprocessing was done. You could fetch date from argument or as I did directly from