Skip to content

Instantly share code, notes, and snippets.

@jonhattan
jonhattan / drupal_backtrace_watchdog.php
Created February 14, 2014 14:02
When you want a quick lightweight backtrace in #drupal. Alternative to devel's ddebug_backtrace(). Use this snippet in conjunction with `drush wd-show --tail`
<?php
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $call) {
watchdog('backtrace', $call['function']);
}
@jonhattan
jonhattan / drupal-install.sh
Last active September 17, 2016 08:48
Helper script to set up a Drupal installation. It assumes a Debian-like OS with Apache 2.4 and Drush installed. Also requires a .my.cnf file in the current user's home directory with mysql root-alike credentials. Site is installed at /var/www/${SITE}/docroot. A database, a virtualhost and an entry at /etc/hosts named ${SITE} are created. The scr…
#!/bin/bash
# Check arguments and requirements.
if [ ${#@} -lt 1 ]; then
echo "This script expects one argument (site machine name)."
exit 1
fi
if [ -x ${HOME}/.my.cnf ]; then
echo "This script requires ${HOME}/.my.cnf."
@jonhattan
jonhattan / date-formats.php
Created December 13, 2013 10:37
Snippet to create date formats in Drupal.
<?php
// Array of type => format.
$formats = array(
'day_monthname_year' => 'j \d\e F \d\e Y',
'weekday_day_monthname_year' => 'l j \d\e F \d\e Y',
);
$query_types = db_insert('date_format_type')->fields(array('type', 'title'));
$query_formats = db_insert('date_formats')->fields(array('format', 'type'));
foreach ($formats as $type => $format) {
@jonhattan
jonhattan / diskfree.py
Created October 16, 2013 11:04
Script to check free disk space, and disk space consumption and print an atert. Quick instructions: place it in /root/scripts and configure a daily or weekly cron.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# set the minimum disk space available to alert on (in GB).
minimum = 5
# disk space variation (in GB) threshhold. 'delta GB have been consumed between the last two checks'.
delta = 1
import commands
import pickle
@jonhattan
jonhattan / MODULENAME_handler_field_cart_line_item_link_edit.inc
Last active December 25, 2015 16:19
Extends "edit line item" (see https://drupal.org/node/1211278) functionality to allow editing within the product display node, in the same way as when add-to-cart in the first instance. Sadly it needs overriding the views handler to set our own link.
@jonhattan
jonhattan / commerce_order_status_change.php
Last active December 25, 2015 00:39
Snippet to detect an order status change.
<?php
/**
* Implements hook_commerce_order_update().
*/
function HOOK_commerce_order_update($order) {
// Detect a status change.
if ($order->status != $order->original->status) {
$status = commerce_order_status_load($order->status);
$state = commerce_order_state_load($status['state']);
@jonhattan
jonhattan / dbselect2themetable.php
Last active December 18, 2015 09:49
Example of building a table with rows from a db table in Drupal
<?php
/**
* Menu callback.
*/
function this_is_a_menu_callback() {
$header = array('nid' => 'ID', 'title' => 'Title');
$query = db_select('node', 'n', array('fetch' => PDO::FETCH_ASSOC))
->fields('n', array('nid', 'title'))
@jonhattan
jonhattan / thingie2drupal.php
Created June 4, 2013 08:28
PHP script using curl to login to a website using POST, store the cookie and request pages subsequently. The script also provides placeholders to parse data from a page and create a node in Drupal. It is intended to be used as a drush script (drush php-script --script-path=/path/to/script/folder thingie2drupal)
<?php
$URL = 'http://example.com/login.php';
$user = 'USER';
$pass = 'PASS';
$cookie_path = dirname(__FILE__).'/cookie.txt';
/**
* Hace login en la web enviando un POST con el usuario y contraseña.
@jonhattan
jonhattan / fieldautovalue.module
Last active December 6, 2017 07:28
Hide a field in node edit form and provide its value programatically.
<?php
/**
* Implements hook_form_FORM_ID_alter() for node type 'test'.
*
* Hide field_texto.
*/
function fieldautovalue_form_test_node_form_alter(&$form) {
$form['field_texto']['#access'] = FALSE;
}
@jonhattan
jonhattan / roundcube-init.pp
Created March 28, 2013 17:47
Pattern for creating non-homogeneous resource types in puppet from a hiera definition. Roundcube plugins as an use case. NOTE: filenames are intended to be a directory structure. Replace hypens with slashes.
class roundcube(
$dbprovider,
$dbuser = 'roundcube',
$dbpass = unset,
$dbname = 'roundcube') {
#...
$defaults = {
config => {},