Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / EXAMPLE.module
Created April 30, 2014 10:54
Example of a block with custom theme.
<?php
/**
* Implements hook_theme().
*/
function EXAMPLE_theme() {
$path = drupal_get_path('module', 'EXAMPLE');
$hooks = array(
'iu_license' => array(
'template' => 'iu-license',
@jonhattan
jonhattan / bakupninja-handler-mysql
Last active August 29, 2015 14:01
Backupninja handler script for mysql. Declare in `nodata_any` the list of tables to exclude in all databases (if present).
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
# vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
#
# mysql handler script for backupninja
#
getconf backupdir /var/backups/mysql
getconf databases all
getconf ignores
getconf nodata
diff --git a/core/rebuild.php b/core/rebuild.php
index c915aff..f26eb6f 100644
--- a/core/rebuild.php
+++ b/core/rebuild.php
@@ -15,6 +15,9 @@
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpKernel\Event\GetResponseEvent;
+use Symfony\Component\HttpKernel\KernelEvents;
@jonhattan
jonhattan / gist:5384246f05cbb114dd65
Last active August 29, 2015 14:11
Sql to obtain views using php. #drupal
;; Select views using php
SELECT vd.vid, name, id FROM views_display vd LEFT JOIN views_view vv ON vd.vid=vv.vid WHERE display_options LIKE '%php%';
;; View php snippets
mysql DBNAME -e "SELECT vd.vid, name, id, display_options FROM views_display vd LEFT JOIN views_view vv ON vd.vid=vv.vid WHERE display_options LIKE '%php%' \G" | less
@jonhattan
jonhattan / db_merge_example.php
Created March 3, 2015 14:32
db_merge() example
<?php
// Track # of places sold for an event. The event is addressed by $entity_id and $date.
// $quantity is a positive number of places sold or negative, if places are cancelled.
db_merge('places_sold')
->key(
array(
'entity_id' => $entity_id,
'date' => $date->format(DATE_FORMAT_DATE),
)
@jonhattan
jonhattan / sb_surgery.drush.inc
Created September 29, 2015 18:46
Drush command to inspect cache config of blocks, views and panels
<?php
function sb_surgery_drush_command() {
$items = array();
$items['cache-status'] = array(
'description' => 'Show cache status for each block, view, panel or minipanel',
);
return $items;
}
@jonhattan
jonhattan / drush-install.sh
Last active September 30, 2015 12:56
<strike>oneliner</strike> several-liner to install composer and drush system-wide
#!/bin/sh
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
COMPOSER_HOME=/opt/drush COMPOSER_BIN_DIR=/usr/local/bin COMPOSER_VENDOR_DIR=/opt/drush composer require drush/drush:7
cd /opt/drush/drush/drush/
composer install
@jonhattan
jonhattan / example_ajax.php
Last active November 17, 2023 15:28
Drupal 8 - detect if we're in an ajax request, and if building the form for the first time, or re-building within the input processing.
<?php
use Drupal\Core\Form\FormStateInterface;
function example_ajax_form_alter() {
$ajax_form_request = \Drupal::request()->query->has(FormBuilderInterface::AJAX_FORM_REQUEST);
if ($ajax_form_request) {
if (!$form_state->isProcessingInput()) {
\Drupal::logger('example')->notice('first pass');
}