Skip to content

Instantly share code, notes, and snippets.

@hannesl
hannesl / gist:1587809
Created January 10, 2012 08:14
A Drupal search result ranking scheme for just the post date.
/**
* Implements of hook_ranking(). Copied from node_ranking().
*/
function MYMODULE_ranking() {
$ranking = array();
// Add ranking based on just the creation date (node_ranking() also uses modified
// date).
if ($node_cron_last = variable_get('node_cron_last', 0)) {
$ranking['MYMODULE_recent'] = array(
'title' => t('Recently posted, ignore modification date'),
@hannesl
hannesl / gist:2778202
Created May 23, 2012 22:21
A method for including respond.js for IE < 9 only, and complex media queries for everyone else.
<!-- Include complex media queries for all browsers except IE < 9.
See bottom of http://www.quirksmode.org/css/condcom.html for info about
the comment tag. Using document.write() to aviod invalid markup. -->
<script>
document.write('<comment><link rel="stylesheet" href="/path/to/complex-media-queries.css" media="all" /></comment>');
</script>
<!-- Only include respond.js for IE < 9. -->
<!--[if lt IE 9]>
<script src="/path/to/respond.min.js?v=v1.1.0"></script>
@hannesl
hannesl / settings.php
Last active October 5, 2015 23:28
Drupal development settings
// ...
// Development settings
$conf['cache'] = 0;
$conf['preprocess_css'] = 0;
$conf['preprocess_js'] = 0;
$conf['error_level'] = 2;
@hannesl
hannesl / mymodule.install.php
Created September 6, 2012 08:27
Check user Drupal role id for Features sanity
/**
* Implements hook_requirements().
*/
function mymodule_requirements($phase) {
$requirements = array();
// Ensure translations don't break at install time
$t = get_t();
if (defined('MYMODULE_RID')) {
// Test role ID, make sure it equals MYMODULE_RID.
@hannesl
hannesl / truncate.js
Created September 18, 2012 15:31
Truncate text so that it fits in a fixed-size container. Add ellipsis after the last word. Also, markup is stripped off.
var $div = $('#container');
var divh = $div.height();
var text = $div.text();
var $p;
$div.html('<p>' + text + '</p>');
$p = $div.find('p');
var factor = $p.outerHeight() / divh;
@hannesl
hannesl / gist:3864416
Created October 10, 2012 09:43
Delete/cancel a Drupal user programmatically and safely.
// Tell Drupal to cancel this user.
// The third argument can be one of the following:
// - user_cancel_block: disable user, leave content
// - user_cancel_block_unpublish: disable user, unpublish content
// - user_cancel_reassign: delete user, reassign content to uid=0
// - user_cancel_delete: delete user, delete content
user_cancel(array(), $uid, 'user_cancel_reassign');
// user_cancel() initiates a batch process. Run it manually.
$batch =& batch_get();
@hannesl
hannesl / gist:3871001
Created October 11, 2012 08:32
Allow users to log in to Drupal 7 with their email address – without installing any contrib modules that do more than you need.
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if (in_array($form_id, array('user_login', 'user_login_block'))) {
$form['name']['#element_validate'][] = 'mymodule_user_login_validate';
}
}
/**
@hannesl
hannesl / gist:3892343
Created October 15, 2012 13:01
Override a stylesheet added by a Drupal 7 theme
/**
* Implements hook_element_info_alter().
*/
function mymodule_element_info_alter(&$type) {
// Rather than implementing hook_css_alter(), we'll add a custom pre render
// function for the styles elemement. This is to allow us to override
// seven_css_alter(), which always runs after any module's implementation.
if (isset($type['styles']['#pre_render'])) {
array_unshift($type['styles']['#pre_render'], 'mymodule_pre_render_styles');
}
@hannesl
hannesl / touchslider.js
Created October 23, 2012 20:38
A simple JS/jQuery pattern for making an object draggable on touch and desktop devices.
$(document).ready(function() {
var $handle = $("#handle"),
dragging = false;
// Make the slider handle draggable.
var dragStart = function(e) {
dragging = true;
$handle.addClass("dragging");
// Prevent text select while dragging.
@hannesl
hannesl / gist:3983323
Created October 30, 2012 21:56
A couple of Ruby functions for transliterating file names or URLs.
# Remove whitespace and special characters and replace with dashes (-).
def self.filename(input)
input.split(%r{ |!|/|:|&|-|$|,|\?|%|\(|\)}).map { |i| i.downcase if i != '' }.compact.join('-')
end
# Convert to ascii and replace non-ascii letters.
def self.transliterate(input)
replacements = {
'å' => 'a',
'ä' => 'a',