DPM Once

Posted on: February 11th, 2014 by admin No Comments

dpm is a function provided by the devel module that allows you to inspect objects and arrays in Drupal’s message area. Sometime I want to inspect something in a loop, but I only want to inspect the element once, not in every iteration of the loop. Especially it it is going to loop lots of […]

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 […]

JavaScript Date Add Function

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

I think doing anything with dates is horrible in JavaScript compared to PHP but here is a function to make adding x days to a date a little easier: function dateAdd(theDate, numDays) {   /** * Date must be YYYY-MM-DD or DD-MM-YYYY – other formats will not work */   numDays = parseInt(numDays);   if(theDate.indexOf("/") […]

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 […]

Select MySQL Timestamp at start or end of day or week

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

Code to select MySQL timestamp at the start, or beginning of the day. SELECT UNIX_TIMESTAMP(DATE_FORMAT(NOW(),"%Y-%m-%d 00:00:00"))SELECT UNIX_TIMESTAMP(DATE_FORMAT(NOW(),"%Y-%m-%d 00:00:00")) And for a timestamp at the end of the day: SELECT UNIX_TIMESTAMP(DATE_FORMAT(NOW(),"%Y-%m-%d 23:59:59"))SELECT UNIX_TIMESTAMP(DATE_FORMAT(NOW(),"%Y-%m-%d 23:59:59")) Timestamp for the beginning of the week (Monday morning): SELECT UNIX_TIMESTAMP(DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) day), "%Y-%m-%d 00:00:00"))SELECT UNIX_TIMESTAMP(DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) day), "%Y-%m-%d 00:00:00")) […]

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 […]