Skip to content

Instantly share code, notes, and snippets.

@jonhattan
jonhattan / mycommands.drush.inc
Last active December 10, 2015 15:28
Example Drush commandfile. Place it in $HOME/.drush or /usr/share/drush/commands or any other valid location and clear drush cache afterwards.
<?php
function mycommands_drush_command() {
$items = array();
$items['one-command'] = array(
'description' => 'A command requiring two arguments.',
// Declare required arguments.
'arguments' => array(
'arg1' => 'arg1 description',
'arg2' => 'arg2 description',
@jonhattan
jonhattan / MODULE.module
Last active December 11, 2015 05:58
Definition and usage of a theme hook. Code provide two alternative options: using a template file or a theme function (commented out). It also implements a block to ilustrate the example.
<?php
/**
* Contents for file MODULE.module
*/
/**
* Implements hook_theme().
*
* Define here a theme hook to be implemented within MODULE.theme.inc as function theme_THEMEHOOK($vars) {}
@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 => {},
@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 / 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 / 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 / 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 / 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 / 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 / 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) {