Skip to content

Instantly share code, notes, and snippets.

View gavinblair's full-sized avatar

Gavin Blair gavinblair

View GitHub Profile
@gavinblair
gavinblair / template.php
Created April 7, 2011 15:14
Use the latest jQuery with your Drupal theme
<?php
function {theme_name}_preprocess(&$vars, $hook) {
if (arg(0) != 'admin' && $hook == "page") {
// Get an array of all JavaScripts that have been added
$javascript = drupal_add_js(NULL, NULL, 'header');
// Remove the original jQuery library and Drupal's JS include
unset($javascript['core']['misc/jquery.js']);
unset($javascript['core']['misc/drupal.js']);
@gavinblair
gavinblair / daterange.php
Created April 12, 2011 21:48
The user can input TO and FROM dates and times for an event. How do we display the event time nicely?
<?php
function daterange_range($starttime, $endtime = null) {
$return = "";
//set the endtime to the start time if there isn't an endtime provided
$endtime = empty($endtime) ? $starttime : $endtime;
//turn them into timestamps if they are not timestamps already
$endtime = ((int)$endtime == $endtime && is_int($endtime))? $endtime:strtotime($endtime);
$starttime = ((int)$starttime == $starttime && is_int($starttime))? $starttime:strtotime($starttime);
//boolean values to prevent test duplication,
@gavinblair
gavinblair / db_query.php
Created May 25, 2011 20:11
Drupal 7 allows you to pass an array as the second parameter in db_query(). However, in Drupal 6 you need to pass each value in as a separate parameter. Here's how to use call_user_func_array() to call db_query with an unknown number of fields and values
<?php
//$values is an associative array where the keys are field names and values are the values we want in the database
//create a string with enough %s's for fields and values. Wrap the values in single quotes.
$percents_fields = "";
$percents_values = "";
foreach($values as $field => $value) {
$percents_fields = "%s, ";
@gavinblair
gavinblair / ui-tabs.js
Created June 3, 2011 15:37
Make back/forward buttons work on jQuery UI tabs.
//change the # in the url when switching tabs
//makes refresh work!
$("#tabsMenu li a").click(function(){
window.location.hash = $(this).attr("href");
});
//make back/forward buttons work on tabs
if($("#tabsMenu").length > 0) {
//prevent scrolling when clicking on a tab
var doit = true;
@gavinblair
gavinblair / random_math_question_positive_only.php
Created June 6, 2011 17:26 — forked from SeanJA/random_math_question_positive_only.php
Generate a random math question that always has a positive answer
<?php
$ops = array('-', '+');
$answer = -1;
while($answer < 0 || $answer > 99) {
$num1 = rand(0, 100);
$num2 = rand(0, 100);
$num1 -= 50;
$num2 -= 50;
@gavinblair
gavinblair / resend.php
Created June 13, 2011 21:22
Drupal - resend activation email
<?php
$uid = 120;
$user = user_load(array('uid' => $uid));
$op = 'status_activated';
_user_mail_notify($op, $user);
@gavinblair
gavinblair / mymodule.php
Created June 14, 2011 18:01
Drupal 6 - make external image_buttons work like D7
<?php
//you could also add this to your theme's template.php file, by replacing "phptemplate_image_button" with "themename_image_button" in the function name.
function phptemplate_image_button($element) {
// Make sure not to overwrite classes.
if (isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = 'form-'. $element['#button_type'] .' '. $element['#attributes']['class'];
}
else {
$element['#attributes']['class'] = 'form-'. $element['#button_type'];
}
@gavinblair
gavinblair / page.tpl.php
Created June 14, 2011 19:33
Drupal - Get access to the $node variable in page.tpl.php
<?php
//we want the node, outside of node.tpl.php!
$node = $variables['node'];
@gavinblair
gavinblair / afterload.js
Created June 16, 2011 15:06
Trigger an event after specific background images have been loaded
​$('<img>')​.attr('src',function(){
return $('#mydiv1').css('background-image');
})​​​.load(function(){
​$('<img>')​.attr('src',function(){
return $('#mydiv2').css('background-image');
})​​​.load(function(){
$('<img>')​.attr('src',function(){
return $('#mydiv3').css('background-image');
})​​​.load(function(){
//done loading background images of all 3 divs
@gavinblair
gavinblair / shortcuts.xml
Created August 18, 2011 14:06
echo "<pre>".print_r($selectedvariable,true)."</pre>"; die;
<Macro name="Die Print_r" Ctrl="yes" Alt="yes" Shift="no" Key="68">
<Action type="0" message="2304" wParam="0" lParam="0" sParam="" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="d" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="i" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="e" />
<Action type="0" message="2326" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2326" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2326" wParam="0" lParam="0" sParam="" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="e" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="c" />