Skip to content

Instantly share code, notes, and snippets.

View mortenson's full-sized avatar
💧
Did you try to clear cache?

Samuel Mortenson mortenson

💧
Did you try to clear cache?
View GitHub Profile
@mortenson
mortenson / netlifybug.sh
Last active March 27, 2019 04:04
Replicate a bug with failed manual deploys and Netlify
#!/usr/bin/env bash
# Use: Create an empty directory and download this file as netlifybug.sh
# Then, run "netlify link", then ./netlifybug.sh.
# Use default deploy path (".") at prompts.
# You should see a failed deploy first, then a successful deploy after.
mkdir -p subdir
touch index.html subdir/index.html
RAND=$(openssl rand -base64 12)
@mortenson
mortenson / metatag_example.php
Created February 26, 2019 23:29
OR statements in metatags...
<?php
/**
* Implements hook_metatags_attachments_alter().
*
* This function allows you to define fallback tokens in case a field is empty.
*
* If all fallback values are empty, the tag will be empty.
*
* Example: [node:field_image:medium]||[node:field_legacy_image:medium]||/fallback.png
@mortenson
mortenson / request.php
Last active December 29, 2018 23:23
A quick and dirty way to make Drupal requests over the command line. Useful with NodeJS.
<?php
/**
* @file
* Allows Node to make command line requests without a web server.
*
* Example use with Node to POST content:
* const { spawnSync } = require('child_process');
* const input = JSON.stringify({ path: '/some/path', content: 'request content' });
* spawnSync(drush, ['php:script', 'request.php', '--script-path=some/script/path'], { input: input });
@mortenson
mortenson / dump.php
Created July 16, 2018 21:34
Dump Drupal nodes as YML
<?php
// Download to Drupal root (where index.php is) and run "drush scr dump.php"
$storage = \Drupal::entityTypeManager()->getStorage('node');
$nodes = $storage->loadMultiple();
echo 'Processing ' . count($nodes) . ' nodes...' . "\n";
@mortenson
mortenson / chunk_graph.php
Created June 9, 2018 18:34
Sort a Drupal graph into chunks of dependencies that can be installed concurrently
<?php
use Drupal\Component\Graph;
function chunk_graph(Graph $graph_object) {
// Sort the graph by weight.
$graph = $graph_object->searchAndSort();
uasort($graph, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
// Ensure that graph nodes are sorted from least to most edges.
$graph = array_reverse($graph);
@mortenson
mortenson / concurrency.php
Created June 6, 2018 22:11
Playing around with concurrency using Symfony's process component
<?php
use Symfony\Component\Process\Process;
// The total sleep time of this queue is 22, but it only takes ~10 seconds to
// run as new commands are started as soon as another finishes and the
// $concurrency limit is not met.
$command_queue = [
'sleep 1',
'sleep 1',
@mortenson
mortenson / secpatch.sh
Last active January 17, 2019 18:46
Creates a patch of all security fixes (commits whose logs contain "SA-") between two Drupal tags
# This script attempts to create a patch file that includes all security fixes
# between two tags.
# Hypothetically, you could just apply the patch to your Drupal site after the
# script is finished successfully to be up to date.
# Biggest successful D7 patch - 7.36 => 7.58
# Biggest successful D8 patch - 8.4.0 => 8.4.7 / 8.6.0 => 8.6.7
# WARNING: This is an experimental patch generation method and may not be
# perfect. If you use this on production I take no responsibility for your
@mortenson
mortenson / notes.md
Last active April 16, 2018 23:40
Thinking about Twig.js re-rendering

I've been thinking about efficently re-rendering Twig template in the browser recently...

Given this template:

<div>{{ variable }}</div>
{% if flag == true %}
  <div>{{ variable2 }}</div>
{% endif %}
@mortenson
mortenson / library_renderer.php
Last active March 27, 2018 06:31
Get the HTML of arbitrary Drupal libraries
<?php
$libraries = ['core/jquery'];
// In this example I'm just concerned about JS assets, but you can use other
// services to render other assets.
/** @var \Drupal\Core\Asset\AssetResolver $asset_resolver */
$asset_resolver = \Drupal::service('asset.resolver');
/** @var \Drupal\Core\Asset\JsCollectionRenderer $js_renderer */
@mortenson
mortenson / Gruntfile.js
Created March 19, 2018 15:27
#2952390 off-canvas css renamer - download to docroot/renamer, run npm install, then "grunt"
const glob = require('glob');
let files = {};
glob.sync('../core/**/off-canvas*.css').forEach((file) => {
files[file] = file;
});
module.exports = (grunt) => {
grunt.loadNpmTasks('grunt-css-selectors');