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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / config.yml
Created April 22, 2019 14:53
Example Tugboat (tugboat.qa) configuration for Tome
services:
php:
image: tugboatqa/php:7.2-apache
default: true
depends: mysql
commands:
init:
- docker-php-ext-install opcache
- a2enmod headers rewrite
- wget -O /usr/local/bin/drush
@mortenson
mortenson / Dockerfile
Created June 5, 2019 13:24
Example render.com Dockerfile for Tome
FROM php:7.2-apache
RUN apt-get update && \
apt-get install -y --no-install-recommends git zip openssh-client sqlite3 libsqlite3-dev
# This is copied from the official Docker "drupal" image's Dockerfile.
# install the PHP extensions we need
RUN set -ex; \
\
if command -v a2enmod; then \