Skip to content

Instantly share code, notes, and snippets.

View kevinquillen's full-sized avatar

Kevin kevinquillen

View GitHub Profile
@kevinquillen
kevinquillen / prune.sh
Created July 24, 2018 14:06
Blow away branches that are older than a year.
#!/bin/sh
for branch in $(git branch -a | sed 's/^\s*//' | sed 's/^remotes\///' | grep -v 'master$'); do
if [[ "$(git log $branch --since "12 months ago" | wc -l)" -eq 0 ]]; then
# I have to do this because "git log" for me returns current directory contents... not sure why yet.
if [[ ${branch} == *".sh" ]]; then
continue
fi
local_branch_name=$(echo "$branch" | sed 's/remotes\/origin\///')
@kevinquillen
kevinquillen / SurpriseMeController.php
Created July 14, 2018 13:35
Fetch a random node by type and return it as a JSON response.
<?php
declare(strict_types = 1);
namespace Drupal\harlib_surprise_me\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\Entity\Node;
use Symfony\Component\HttpFoundation\JsonResponse;
@kevinquillen
kevinquillen / SurpriseMeController.php
Created July 14, 2018 13:35
Fetch a random node by type and return it as a JSON response.
<?php
declare(strict_types = 1);
namespace Drupal\harlib_surprise_me\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\Entity\Node;
use Symfony\Component\HttpFoundation\JsonResponse;
<?php
/**
* Class ResourceNotFoundController.
*/
class ResourceNotFoundController extends ControllerBase {
/**
* Display the Resource Not Found message.
*
@kevinquillen
kevinquillen / OpenNowCheckController.php
Last active July 12, 2018 19:15
Simple Controller in Drupal via Symfony, returns a JsonResponse cached for 30 seconds.
<?php
declare(strict_types = 1);
namespace Drupal\harlib_open_now\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Serialization\Json;
@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.
*
@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 / 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 / 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 / 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"