Skip to content

Instantly share code, notes, and snippets.

View kevinquillen's full-sized avatar

Kevin kevinquillen

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / example.go
Last active March 11, 2018 18:20
Just learning a little bit about Go.. using http client to get a response and look at the data.
package main
import "fmt"
import "net/http"
import "io/ioutil"
import "time"
import "log"
func main() {
var url = "[redacted]/api/v1/room"
@kevinquillen
kevinquillen / mytheme.theme
Last active April 19, 2018 14:26
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.
<?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 / mytheme.theme.php
Last active April 19, 2018 14:35
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.
<?php
/**
* Implements hook_preprocess_paragraph().
*/
function mytheme_preprocess_paragraph(&$variables) {
$paragraph = $variables['elements']['#paragraph'];
$bundle = $paragraph->getType();
if ($bundle == 'fancy_links') {
@kevinquillen
kevinquillen / NodeEntityNormalizer.php
Created April 23, 2018 14:03
Provides an entity normalizer for certain node types.
<?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 / RandomEntityFieldFormatter.php
Created June 25, 2018 19:04
This field formatter can be used on entity reference fields to select a random entity from the list to render. Note that the more items you have, the better the 'random' pick will seem. Also note that this will only fire once per cache write, then again on cache clear.
<?php
namespace Drupal\harlib_random_entity_display\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter;
/**
* Plugin implementation of the 'random_entity_field_formatter' formatter.
*