Skip to content

Instantly share code, notes, and snippets.

View aklump's full-sized avatar

Aaron Klump aklump

View GitHub Profile

Keybase proof

I hereby claim:

  • I am aklump on github.
  • I am aklump (https://keybase.io/aklump) on keybase.
  • I have a public key ASB_LuPX2hRCGOYe-MYF_LW--oONccyBnVe-8WfoqQzSdgo

To claim this, I am signing this object:

@aklump
aklump / font-size-range.scss
Last active August 8, 2017 22:01
Mixin for variable font sizing between a minimum and maximum using media queries and the vw unit.
//
//
// Mixin for variable font sizing between two font sizes with min and max constraints.
//
// Ever want to declare a title to grow from 28px to 60px based on browser width, but always be at least 28px and never
// more than 60px? This SASS mixin does the heavy lifting in terms of math and media queries to figure the necessary CSS
// to do just that.
//
// @code
// @include loft-sass-font-size-range(28px to 46px at 960px);
@aklump
aklump / image_cache_buster.php
Last active January 19, 2018 22:27
How to add cache-busting to itok image derivative urls in Drupal 7 for entity image fields.
<?php
function my_module_node_view_alter(&$build)
{
if (!empty($build['field_image']) && user_is_logged_in()) {
$build['field_image']['#post_render'][] = 'my_module_cache_buster';
}
}
function my_module_cache_buster($html, $build)
{
@aklump
aklump / notabledrag.php
Created July 9, 2016 15:52
remove drupal tabledrag in your theme
/**
* Implements hook_preprocess_HOOK().
*/
function THEMENAME_preprocess_field_multiple_value_form(&$vars) {
if (isset($vars['table']['#tabledrag'])) {
unset($vars['table']['#tabledrag']);
array_pop($vars['table']['#header']);
foreach ($vars['table']['#rows'] as &$row) {
foreach (array_keys($row['data']) as $key) {
if (array_intersect($row['data'][$key]['class'], [
@aklump
aklump / node_edit_form_d8.php
Last active August 22, 2018 06:43
Programmatically embed a node or entity edit form in Drupal 8
// Without the use of D.I.
$form = \Drupal::service('entity.manager')
->getFormObject('node', 'default')
->setEntity($node);
$build[] = \Drupal::formBuilder()->getForm($form);
// Within a class using D.I.
class someClass {
public function __construct(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
@aklump
aklump / HOOK_is_dark_image.php
Last active January 19, 2018 23:30
Drupal 7 snippet to determine if an image is mostly dark.
<?php
/**
* Determine if an image is mostly dark.
*
* @param string $uri The uri of the image file.
* @param array $options
* - resolution int The larger the number the more measurements will be taken
* and the longer it will take. Defaults to 10.
* - threshold int The luminance threshold. 170 is equal to #acacac.
* @param array $info Pass an array to be filled with data used to calculate
@aklump
aklump / higher_jquery_in_theme.php
Created November 14, 2015 16:45
Include a higher version of jQuery in your Drupal 7 theme using hook_js_alter in template.php
/**
* Implements hook_js_alter().
*
* Replace the system-provided jquery with our own higher version for the theme.
*/
function my_theme_js_alter(&$javascript) {
// Go through and replace the jquery that is being provided by the system
// with out own higher version for the theme. We use an array
// here in case the jquery update module is/is not being used.
@aklump
aklump / my_theme_has_content.php
Last active October 19, 2015 22:16
A handy function to add to Drupal 7 theme to determine if a renderable arrays contain output.
/**
* Checks one or more render arrays for any visible content.
*
* @param ... array One or more renderable arrays.
*
* @return bool
*/
function my_theme_has_content() {
$build = func_get_args();
@aklump
aklump / drupal7-field-node-access.php
Last active January 19, 2018 23:36
Drupal 7 node access per entity using a field dropdown, callback functions and the node access API.
<?php
/**
* Shows how to create a field that controls node access in Drupal 7 based
* on callback functions using the node access API and a field select list;
* so that content managers can easily control per-entity access control
* using a simply dropdown.
*
* 1. Create a custom module and insert the following 3 functions.
* 2. Create a List(text) field called field_access with widget select and
* attach it to the entity where you want to control access.
@aklump
aklump / array_set_nested_element
Last active August 29, 2015 14:16
Set a multidimensional array element based on an array of nested keys.
/**
* Set a multidimensional array element based on an array of nested keys.
*
* @code
* $vars = array();
* $string = 'some.string.of.keys';
* $keys = explode('.', $string);
* $value = 'final value';
* array_set_nested_element($vars, $keys, $value);
* @endcode