Skip to content

Instantly share code, notes, and snippets.

View aklump's full-sized avatar

Aaron Klump aklump

View GitHub Profile
@aklump
aklump / vendor_autoload.php
Created April 30, 2025 21:41
snippet to load composer autoload
// https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-autoloader-from-a-binary
if (isset($GLOBALS['_composer_autoload_path'])) {
// As of Composer 2.2...
$_composer_autoload_path = $GLOBALS['_composer_autoload_path'];
}
else {
// < Composer 2.2
foreach ([
__DIR__ . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
@aklump
aklump / todo.md
Created March 5, 2025 20:08
Task list scaffolding.

Critical

Normal

Complete

@aklump
aklump / xdebug_controller.sh
Last active February 18, 2025 00:07
Xdebug Lando Controller
#!/usr/bin/env bash
#
# @file
# Xdebug Lando Controller (Supports Xdebug versions 2 and 3)
#
# Save this file to: ./install/lando/xdebug_controller.sh.
#
# This script is meant to be used for Lando tooling to control Xdebug. When
# using with version 2, use the version 3 syntax and it will be properly
@aklump
aklump / GetShortPath.php
Created October 30, 2024 21:53
Create a pretty/short path, removing CWD or basepath.
<?php
/**
* @code
* // Print a shortened, nice-to-read path when possible.
* echo (new GetShortPath(getcwd())($long_path)
* @endcode
*/
class GetShortPath {
@aklump
aklump / RotateImageDataURI.php
Last active September 16, 2024 21:36
Invokable class to rotate an image data URI
<?php
use InvalidArgumentException;
/**
* Rotate an image DataURI string.
*
* Because I'm working with a string, not a file, the Drupal image API
* (image.factory) didn't seem to be an appropriate solution, therefor I'm using
* the native PHP GD library.
*/
@aklump
aklump / alter_exception_message.php
Created May 17, 2024 00:37
How to alter an Exception message.
<?php
$augmented_message = $exception->getMessage() . "\nMy augementation";
$reflected_exception = new \ReflectionObject($exception);
$message_property = $reflected_exception->getProperty('message');
$message_property->setAccessible(TRUE);
$message_property->setValue($exception, $augmented_message);
throw $exception;
@aklump
aklump / countPalindromes
Created May 10, 2024 01:23
Count the total number of palindromes in a word.
function countPalindromes($word) {
$palindromes = 0;
$word_length = strlen($word);
for ($chunk_size = $word_length; $chunk_size > 0; --$chunk_size) {
for ($pos = 0; $pos <= ($word_length - $chunk_size); $pos++) {
$substr = substr($word, $pos, $chunk_size);
if ($substr === strrev($substr)) {
++$palindromes;
}
}
class FormatPhoneNumber {
const FORMAT = '(%d) %d-%d';
const SMS_FORMAT = '+1%d%d%d';
public function __invoke(string $number, string $format = NULL) {
$number = preg_replace('#[^0-9]#', '', $number);
preg_match('#(.+)?(\d{3})(\d{3})(\d{4})$#', $number, $matches);
array_shift($matches);
array_shift($matches);
@aklump
aklump / GetHostDelta.php
Last active March 30, 2024 21:49
Get the delta on the host entity for a paragraph.
<?php
use Drupal\paragraphs\ParagraphInterface;
/**
* Get the delta on the host entity for a paragraph.
*/
class GetHostDelta {
public function __invoke(ParagraphInterface $paragraph): ?string {
<?php
namespace Drupal\se_core\Traits;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
trait GetFormModeTrait {
protected function getFormMode(FormStateInterface $form_state): string {