Skip to content

Instantly share code, notes, and snippets.

View angry-dan's full-sized avatar

Dan James angry-dan

  • Kent, United Kingdom
View GitHub Profile
<?php
/**
* Join a string with a natural language conjunction at the end.
*/
function natural_language_join(array $list, $conjunction = 'and') {
$last = array_pop($list);
if ($list) {
return implode(', ', $list) . ' ' . $conjunction . ' ' . $last;
}
return $last;
<?php
db_query("
INSERT IGNORE INTO {field_data_FIELD_NAME} (entity_type, bundle, deleted, entity_id, revision_id, language, delta, VALUE_COLUMNS...)
SELECT
'ENTITY TYPE' AS entity_type,
'BUNDLE' AS bundle,
0 AS deleted,
ENTITY ID AS entity_id,
REVISION ID IF EXISTS ELSE ENTITY ID AS revision_id,
'und' AS language,
@angry-dan
angry-dan / create-custom-tokens.php
Last active August 29, 2015 14:06
Create custom tokens (Drupal token_custom module) with an update hook.
<?php
$token = new stdClass();
$token->machine_name = 'machine-name-like-this';
$token->name = 'Name like this';
$token->description = 'Description like this';
$token->type = 'bundle_name';
$token->content = 'Default value';
$token->format = 'filtered_html';
$token->is_new = TRUE;
<?php
// If you are unfortunate enough to still be using nodequeues (see entity queue)
// this will should work for creating new queues:
// Note I built the array from a drupal_var_export and a nodequeue load operation
// Then I added the add_subqueue argument and removed the qid (both mandatory).
function mymodule_update_N() {
$queue = (object) array(
'name' => 'machine_name',

If you ever need to run an update hook again with drush you can often do this:

drush eval "module_load_instal('module_name'); module_name_update_7012();"

But! If that update is run as a batch then that won't work. So instead you can do this:

drush eval "drupal_set_installed_schema_version('module_name', '7011');"
drush updb

Which effectively rolls the module back to it's old version number.

@angry-dan
angry-dan / acquia-log-management.sh
Created February 3, 2015 13:01
Bash one-liners for reading and analyzing Acquia's log files.
# Find and print all PHP errors that found their way into watchdog, sorted by number of ocurrences
zgrep '|php|' drupal-watchdog.log* | cut -d'|' -f9 | sed -r "s/(.*?)request_id\=\"[0-9a-f-]*\"/\1/" | sort | uniq -c | sort -rn | less
@angry-dan
angry-dan / benchmark-drupal-bootstrap.php
Created April 16, 2015 10:27
Benchmark the Drupal bootstrap phases. Since this is something that happens on every page load it needs to be really fast. You should probably be worried if bootstrap is taking more than 300ms per page load.
<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
$phases = array(
'CONFIGURATION' => DRUPAL_BOOTSTRAP_CONFIGURATION,
'PAGE_CACHE' => DRUPAL_BOOTSTRAP_PAGE_CACHE,
'DATABASE' => DRUPAL_BOOTSTRAP_DATABASE,
@angry-dan
angry-dan / Configuring Jenkins.md
Last active August 29, 2015 14:27
Configuring Jenkins/CI for Drupal
  1. Create a project using the VDD site machine name

  2. Tick "discard old builds"

  3. Set strategy to "Log rotation" and keep 10 builds.

  4. Set source management to git and enter the repository URL.

  5. Checkout develop or master (develop for dev, master for stage).

  6. Set the build to be a shell command and use the following commands:

     ln -s docroot public_html
     echo "This project is managed by Jenkins" > README-jenkins
    

rsync -va --delete --exclude=sites/default/files --exclude=.git . /var/www/vhosts/[site]

@angry-dan
angry-dan / drupal_run_as.php
Created October 24, 2015 13:25 — forked from anonymous/drupal_run_as.php
Safely run a function in Drupal as another user.
<?php
function drupal_run_as($user, $func, $arg1) {
global $user;
$original_user = $user;
$old_state = drupal_save_session();
drupal_save_session(FALSE);
$args = func_get_args();
$user = array_shift($args);