Skip to content

Instantly share code, notes, and snippets.

@andeersg
andeersg / onlinestatus.js
Last active May 20, 2019 08:15
A simple React hook to get online status and listen for changes
import React, { useState } from 'react';
import useOnlineStatus from './useOnlineStatus';
const Example = () => {
const [onlineStatus, setOnlineStatus] = useState(navigator.onLine);
useOnlineStatus((isOnline) => {
setOnlineStatus(isOnline);
});
@andeersg
andeersg / handlebars_test.js
Created March 5, 2019 14:14
Simple test of raw blocks in Handlebars
const Handlebars = require('handlebars');
var source = `{{{{raw}}}}
{{#if ready}}
<p>Ready</p>
{{/if}}
<p>Something else!</p>
{{{{/raw}}}}`;
Handlebars.registerHelper('raw', function(content) {
@andeersg
andeersg / mapYrToWeathericons.json
Created October 1, 2017 19:38
A simple JSON object that maps symbol codes from Yr.no to weathericons: https://erikflowers.github.io/weather-icons/
{
"01d": {
"label": "Clear sky",
"icon": "wi-day-sunny"
},
"01n": {
"label": "Clear sky",
"icon": "wi-night-clear"
},
"02d": {
@andeersg
andeersg / watchdog.php
Created March 14, 2016 13:54
Simple implementation of `hook_watchdog` to send messages to external service.
function hook_watchdog(array $log_entry) {
$severity_list = array(
WATCHDOG_EMERGENCY => t('Emergency'),
WATCHDOG_ALERT => t('Alert'),
WATCHDOG_CRITICAL => t('Critical'),
WATCHDOG_ERROR => t('Error'),
WATCHDOG_WARNING => t('Warning'),
WATCHDOG_NOTICE => t('Notice'),
WATCHDOG_INFO => t('Info'),
@andeersg
andeersg / copy_field.php
Last active January 25, 2016 08:17
Simple script for copying fields from one bundle to another.
<?php
$entity_type = '';
$old_bundle = '';
$new_bundle = '';
$fields = array(
'field_name',
);
foreach ($fields as $field) {
@andeersg
andeersg / delete-field.php
Last active November 3, 2015 09:56
Delete all instances and the field in Drupal 7
$field_name = '';
$entity_name = '';
$entity = entity_get_info($entity_name);
foreach ($entity['bundles'] as $key => $value) {
$field_instance = field_info_instances($entity_name, $key);
$instance = $field_instance[$field_name];
field_delete_instance($instance);
}
@andeersg
andeersg / gist:b317b976f801d3103686
Created October 13, 2015 19:28
Simple JavaScript object for saving and restoring localStorage items.
var StorageUnit = (function() {
var is_supported = function() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
};
var _ = window.StorageUnit = {
@andeersg
andeersg / gulpfile.js
Created April 7, 2014 12:22
A gulpfile.js layout i use alot.
// Load plugins
var gulp = require('gulp'),
sass = require('gulp-ruby-sass'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
clean = require('gulp-clean'),
@andeersg
andeersg / Localstorage Form Backup
Created February 5, 2014 15:41
Perform backup and restoration of form values.
jQuery.support.localstorage = function() {
var mod = 'modernizr';
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
} catch(e) {
return false;
}
}
@andeersg
andeersg / drupal_field_data
Last active December 31, 2015 12:29
List out two arrays with all the field data you need for hook_install().
include_once DRUPAL_ROOT . '/includes/utility.inc';
$nodetype = '';
$instances = field_info_instances('node', $nodetype);
$fields = array();
foreach ($instances as $instance) {
unset($instances[$instance['field_name']]['id'], $instances[$instance['field_name']]['field_id']);