Archive for the ‘PHP’ Category

Automatically add a field to every new webform created

Posted on: October 30th, 2013 by admin 1 Comment

I recently had a need for users to be able to attach webforms to some node types to allow users to give feedback. Each webform could be attached to multiple nodes. When the webform data was submitted it needed to record which node it was attached to. This was easy enough by adding a hidden […]

Drupal Form with dynamically adding a collection of fields via ajax

Posted on: October 25th, 2013 by admin No Comments

I put this together by combining a couple of different examples from the examples module but wanted to keep it here for future reference. This example shows a holiday form where each holiday has a start date, end date and number of days field, and there can be multiple holidays. 1. Set up the page: […]

Add a class to each of Drupal 7 primary tab items

Posted on: October 18th, 2013 by admin 2 Comments

If you find yourself wanting to style each tab individually, or attaching a certain JavaScript attributes to a particular tab this this code is very useful. Just put this in your themes template.php. function YOUR_THEME_NAME_menu_local_tasks(&$variables) { if (isset($variables[’primary’])) { foreach($variables[’primary’] as $menu_item_key => $menu_attributes) { $variables[’primary’][$menu_item_key][’#link’][’localized_options’][’attributes’][’class’][] = strtolower(str_replace(’ ‘,’-‘, $menu_attributes[’#link’][’title’])); } } return theme_menu_local_tasks($variables); }function […]

Add “Delete All Terms” button to Drupal 7 Vocabulary Page

Posted on: October 10th, 2013 by admin No Comments

Sometimes it is necessary to delete all terms from a taxonomy however Druapl does not provide any functionality to do this. I’m using the jQuery Easy Confirm Dialog plugin for this. First download the library from here and put this in your themes js folder. We can then add a “Delete All Terms” button with […]

Create a new join in hook_views_query_alter

Posted on: October 8th, 2013 by admin 4 Comments

I tried to do this recently and found very few examples available online so am putting this up here for reference. My original query looked like this: 1 2 3 4 SELECT node.title, node.nid FROM node node WHERE node.status = ‘1’ AND node.type IN (’resource’) ORDER BY node_title ASCSELECT node.title, node.nid FROM node node WHERE […]

Get latitude and longitude from postcode

Posted on: September 12th, 2013 by admin No Comments

Great little function to fetch the latitude and longitude from any postcode using the Google Maps API: 1 2 3 4 5 6 7 8 9 10 11 function _get_lat_lng($address) { $address = str_replace(’ ‘,”,$address); $url = ‘http://maps.googleapis.com/maps/api/geocode/json?address=’.$address.’&sensor=false’; $output = json_decode(file_get_contents($url)); if(isset($output->results) && is_array($output->results) && isset($output->results[0]->geometry->location)){ $lat = $output->results[0]->geometry->location->lat; $long = $output->results[0]->geometry->location->lng; if($lat && $long) […]

Array to CSV

Posted on: June 19th, 2013 by admin No Comments

This is another useful function I often find myself needing: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 function array_to_csv($array, $filename=’data’, $use_key=false, $delm=’,’, $return_data=false) { function maybeEncodeCSVField($string) { if(strpos($string, ‘,’) !== false […]

Array to Table

Posted on: June 19th, 2013 by admin No Comments

I keep finding myself writing functions like this again and again so putting up here for future reference as this just does what it says on the tin: 1 2 function array_to_table($array) { $out = ‘function array_to_table($array) { $out = ‘ ‘.PHP_EOL.”.PHP_EOL; $first = true; foreach($array as $row) { if($first) { $out .= ”.PHP_EOL; foreach($row […]

Easily find specific block from ID in huge list of Drupal 6 blocks

Posted on: February 8th, 2013 by admin No Comments

Ever tried to find a specific block from it’s ID in a huge list of blocks in Drupal 6 admin? As it only shows block title, not ID’s in the admin screen it can be really tricky. This bookmarklet changes the configure link on the /admin/build/block page so it says the ID of the block […]

Drupal urlencode and Drupal urldecode

Posted on: February 7th, 2013 by admin No Comments

In Drupal when even I try to urlencode a url and pass it as an argument to a function then I get the error Not Found. It seems drupal does like the forward slashes, even if they are url encoded. To get round this I’ve written these two functions: 1 2 3 4 5 6 […]