Skip to content

Instantly share code, notes, and snippets.

View kevinquillen's full-sized avatar

Kevin kevinquillen

View GitHub Profile
@kevinquillen
kevinquillen / mymodule.php
Created March 7, 2018 20:11
How to update a field storage length in hook_install (or hook_update). Note this does not cover more difficult things, like changing storage type altogether.
<?php
use Drupal\Core\Database\Database;
/**
* Implements hook_install().
*/
function mymodule_install() {
$connection = Database::getConnection('default');
@kevinquillen
kevinquillen / SearchController.php
Last active January 30, 2018 20:14
Creating a route that makes a call from the server to an external service which is firewalled, with data sent back to the caller (like a ReactJS component)
<?php
namespace Drupal\harlib_search\Controller;
use Drupal\Core\Controller\ControllerBase;
use GuzzleHttp\Client;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use GuzzleHttp\Exception\RequestException;
@kevinquillen
kevinquillen / EmailMatcherTest.php
Last active January 30, 2018 20:12
Example PHPUnit test with UnitTestCase testing a method of a utility class https://gist.github.com/kevinquillen/d7efe7e2bce417289f8fabc2e2f1fe62
<?php
namespace Drupal\Tests\iana_netforum_auth\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\iana_netforum_auth\Utility\EmailMatcher;
/**
* Test email matching component class.
*
@kevinquillen
kevinquillen / EmailMatcher.php
Created July 28, 2017 20:12
Utility class with a helper method that lets me check if the incoming email address input by the user matches a given domain I am verifying against. See the corresponding test: https://gist.github.com/kevinquillen/85baa1fa69f49d1c34aa81369405d47e
<?php
namespace Drupal\iana_netforum_auth\Utility;
/**
* Class EmailMatcher.
*
* @package Drupal\iana_netforum_auth\Utility
*/
class EmailMatcher {
@kevinquillen
kevinquillen / build.sh
Created January 30, 2018 19:20
Detect branch being built with TravisCI and and use different gulp parameters depending on where the deployment is going.
#!/usr/bin/env bash
branch=`echo ${TRAVIS_BRANCH}`;
if [ $branch = "master" ]; then
echo "Building assets for production.";
gulp build --env production;
else
echo "Building assets for development.";
gulp build --no-watch;
@kevinquillen
kevinquillen / module.php
Last active July 10, 2017 20:39
Importing to a SQL table from a CSV file in Drupal, so you don't have to parse exceptionally large CSVs line by line and instead can do so with MySQL querying. This snippet assumes you have read the column headers already from the CSV and passed them as parameters, as well as the temp table name you want to use.
<?php
/**
* Creates a temporary table to hold values from an uploaded CSV.
* @param $table_name
* @param $columns
* @param $message
* @param $context
*/
function csv_import_create_temp_table($table_name, $columns) {
@kevinquillen
kevinquillen / .vimrc
Last active June 10, 2017 03:48
my vimrc file
set nocompatible " be iMproved, required
filetype off " required
syntax enable
set number
set showcmd
set hlsearch
set incsearch
set nobackup
set noswapfile
@kevinquillen
kevinquillen / changeFieldFormatValueTo.sql
Created June 2, 2017 20:56
Stored procedure for MySQL - mass update *_format value tables in Drupal to a specified format. This is useful for when you change default formats on a field after the field already has values, so that the user is not presented with a blank textbox on forms.
CREATE PROCEDURE changeFieldFormatValueTo (IN format_name VARCHAR(32))
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE table_name_value VARCHAR(64);
DECLARE column_name_value VARCHAR(64);
DECLARE cursor_fields CURSOR FOR SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE 'field_%_format';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor_fields;
@kevinquillen
kevinquillen / .htaccess
Created March 31, 2017 15:25
Example htaccess rule to force all traffic over HTTPS
# Force all traffic to HTTPS, except a local instance
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} !^mysite\.local$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
@kevinquillen
kevinquillen / gist:307f9ec2f6e2b685c64f1bc6ce2b745c
Last active January 8, 2017 17:07
Custom bash function to generate a new markdown file for Jekyll, first arg is assumed to be post title, which becomes the filename in the format Jekyll is expecting. Auto open and save in Vim, if the post doesn't exist.
function new_post() {
cd ~/Sites/kevinquillen.com/_posts
today=$(date +"%F")
title=$1:l
clean_title=${title//[^a-zA-Z0-9]/-}
filename="$today-$clean_title.markdown"
if [[ ! -a ./$filename ]]; then
vim $filename -c 'w'
else