Skip to content

Instantly share code, notes, and snippets.

View aklump's full-sized avatar

Aaron Klump aklump

View GitHub Profile
<?php
namespace AKlump\Knowledge\Helpers;
/**
* Retrieves the home directory of the current user.
*
* The method first attempts to fetch the home directory using the `HOME`
* or `USERPROFILE` environment variables. If neither is set, it combines
* the `HOMEDRIVE` and `HOMEPATH` environment variables to determine the
@aklump
aklump / HasTypedEntitiesTrait.php
Last active May 7, 2026 14:21
Provides access to the typed entity repository manager in Drupal custom classes.
<?php
use Drupal\typed_entity\RepositoryManager;
/**
* Provides access to the typed entity repository manager.
*
* Prefer dependency injection when the consuming class supports it:
*
* @code
@aklump
aklump / ClassLoggerTrait.php
Last active May 7, 2026 14:22
Provides class-scoped logging helpers for Drupal custom classes.
<?php
use Drupal\Core\Logger\LoggerChannelInterface;
use Psr\Log\LogLevel;
/**
* Provides class-scoped logging helpers.
*
* Classes using this trait get a logger channel derived from the consuming
* class name. The channel uses the short class name so Drupal's log UI remains
#!/usr/bin/env bash
#
# @file
# Handle NPM dependencies in a directory and/or subdirectories
#
# ========= Bootstrap =========
s="${BASH_SOURCE[0]}";[[ "$s" ]] || s="${(%):-%N}";while [ -h "$s" ];do d="$(cd -P "$(dirname "$s")" && pwd)";s="$(readlink "$s")";[[ $s != /* ]] && s="$d/$s";done;__DIR__=$(cd -P "$(dirname "$s")" && pwd)
basepath="$(cd -P "$__DIR__/.." && pwd)"
@aklump
aklump / Upfind.php
Last active November 8, 2025 17:21
Search for a path moving up the file tree, e.g. "locate .git from child path"
<?php
/**
* The Upfind class provides functionality to locate a file or directory
* by walking up the directory tree from a starting directory.
*/
class Upfind {
public function __invoke(string $basename): string {
$result = $this->upfind($basename);
@aklump
aklump / is_windows.php
Last active March 8, 2026 18:50
Detect if Windows
(defined('PHP_OS_FAMILY') && PHP_OS_FAMILY === 'Windows')
|| strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'
|| DIRECTORY_SEPARATOR === '\\';
@aklump
aklump / drupal_user_timezone.php
Created July 2, 2025 15:05
Get timezone name per current user.
<?php
if (!$account instanceof \Drupal\Core\Session\AccountInterface || $account->isAnonymous()) {
$timezone_name = \Drupal::config('system.date')->get('timezone.default');
}
else {
$timezone_name = $account->getTimeZone();
}
@aklump
aklump / GetUrlTemplateByRouteName.php
Created May 30, 2025 00:00
Generate a URL template from a Drupal route name (with optional/partial parameter replacement).
<?php
namespace Drupal\commons_core\Helper;
use http\Exception\InvalidArgumentException;
class GetUrlTemplateByRouteName {
/**
* Generate a URL template from a route.
@aklump
aklump / vendor_autoload.php
Last active November 8, 2025 17:07
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
$ds = DIRECTORY_SEPARATOR;
foreach ([
__DIR__ . $ds . '..' . $ds . '..' . $ds . 'autoload.php',
@aklump
aklump / todo.md
Created March 5, 2025 20:08
Task list scaffolding.

Critical

Normal

Complete