Skip to content

Instantly share code, notes, and snippets.

Avatar

Kevin kevinquillen

View GitHub Profile
@kevinquillen
kevinquillen / NodeEntityNormalizer.php
Created Apr 23, 2018
Provides an entity normalizer for certain node types.
View NodeEntityNormalizer.php
<?php
declare(strict_types = 1);
namespace Drupal\harlib_restful_normalizer\Normalizer;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Drupal\harlib_libcal_api\LibCalApi;
use Drupal\image\Entity\ImageStyle;
@kevinquillen
kevinquillen / mytheme.theme.php
Last active Apr 19, 2018
Another theme example - preprocessing fields on a paragraph. Formats links with SVG icons next to the title value. FormattableMarkup creates a markup for each link which is being passed as a new variable into the template. This could also be done via hook_preprocess_field, but demonstrates that you can do this from other preprocess hooks.
View mytheme.theme.php
<?php
/**
* Implements hook_preprocess_paragraph().
*/
function mytheme_preprocess_paragraph(&$variables) {
$paragraph = $variables['elements']['#paragraph'];
$bundle = $paragraph->getType();
if ($bundle == 'fancy_links') {
@kevinquillen
kevinquillen / mytheme.theme
Last active Apr 19, 2018
Various ways of adding classes to anchors, as well as title markup within that anchor, from hook_preprocess_field in Drupal 8. These are all link fields, which do not have a base twig to modify the output of the a tag.
View mytheme.theme
<?php
/**
* Implements hook_preprocess_field().
*/
function mytheme_preprocess_field(&$variables) {
if ($variables['element']['#entity_type'] == 'paragraph' && $variables['field_name'] == 'field_reference_link') {
foreach ($variables['items'] as $key => $item) {
$variables['items'][$key]['content']['#options']['attributes']['class'][] = 'hl__link-tag';
}
@kevinquillen
kevinquillen / example.go
Last active Mar 11, 2018
Just learning a little bit about Go.. using http client to get a response and look at the data.
View example.go
package main
import "fmt"
import "net/http"
import "io/ioutil"
import "time"
import "log"
func main() {
var url = "[redacted]/api/v1/room"
@kevinquillen
kevinquillen / mymodule.php
Created Mar 7, 2018
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.
View mymodule.php
<?php
use Drupal\Core\Database\Database;
/**
* Implements hook_install().
*/
function mymodule_install() {
$connection = Database::getConnection('default');
@kevinquillen
kevinquillen / SearchController.php
Last active Jan 30, 2018
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)
View SearchController.php
<?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 / build.sh
Created Jan 30, 2018
Detect branch being built with TravisCI and and use different gulp parameters depending on where the deployment is going.
View build.sh
#!/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 / EmailMatcher.php
Created Jul 28, 2017
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
View EmailMatcher.php
<?php
namespace Drupal\iana_netforum_auth\Utility;
/**
* Class EmailMatcher.
*
* @package Drupal\iana_netforum_auth\Utility
*/
class EmailMatcher {
@kevinquillen
kevinquillen / EmailMatcherTest.php
Last active Jan 30, 2018
Example PHPUnit test with UnitTestCase testing a method of a utility class https://gist.github.com/kevinquillen/d7efe7e2bce417289f8fabc2e2f1fe62
View EmailMatcherTest.php
<?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 / replaceFieldTextTo.sql
Created Jul 10, 2017
Does a mass find+replace on Drupal field_*_value fields within tables - useful for when you need to replace a string in a huge amount of tables.
View replaceFieldTextTo.sql
CREATE PROCEDURE replaceFieldTextTo (IN v_string_from VARCHAR(255), IN v_string_to VARCHAR(255))
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_%_value';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor_fields;