Skip to content

Instantly share code, notes, and snippets.

View aklump's full-sized avatar

Aaron Klump aklump

View GitHub Profile
@aklump
aklump / CurrentUserIpWidget.php
Created March 29, 2024 14:42
Drupal form widget to preload with current user's IP.
<?php
namespace Drupal\se_core\Plugin\Field\FieldWidget;
use Drupal;
use Drupal\Component\Utility\Color;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
@aklump
aklump / CurrentUserWidget.php
Created March 29, 2024 14:42
Drupal form widget to preset entity ref with current user.
<?php
namespace Drupal\se_core\Plugin\Field\FieldWidget;
use Drupal;
use Drupal\Component\Utility\Color;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
@aklump
aklump / QueryAddDateCondition.php
Last active March 28, 2024 22:36
Query conditions for dates with automatic timezone handling.
<?php
namespace Drupal\se_core\Helpers;
use DateTimeInterface;
use DateTimeZone;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldStorageConfig;
use RuntimeException;
@aklump
aklump / drupal_version.php
Created March 28, 2024 00:38
Get any Drupal version.
$version = class_exists('\Drupal') ? Drupal::VERSION : NULL;
$version = $version ?? (defined('VERSION') ? constant('VERSION') : NULL);
@aklump
aklump / ThrowShellError.php
Last active November 21, 2023 01:38
Throws a PHP exception based a shell script error with accurate basename and best-guess line-number.
<?php
/**
* Throw an exception for a bash file, message and exit code.
*
* This class creates an exception where getFile returns a path with the correct
* basename, and in some cases the correct line number where the exit code
* occurred.
*/
class ThrowShellError {
@aklump
aklump / HumanList.php
Created October 12, 2023 20:22
csv using "and"
class HumanList {
use \Drupal\Core\StringTranslation\StringTranslationTrait;
public function __invoke(array $items): string {
$last = array_pop($items);
return $this->t(':items and :item', [
':items' => implode(', ', $items),
':item' => $last,
class UpperCamel {
public function __invoke(string $value): string {
$value = preg_replace('/[\s\-_]/s', ' ', $value);
$value = trim(preg_replace('/[A-Z]/', ' \0', $value));
$value = preg_replace('/(\s)\s+/s', '\1', $value);
$value = ucwords($value);
$value = preg_replace('/\s+/s', '', $value);
return $value;
@aklump
aklump / EscapeDoubleQuotes.php
Last active September 22, 2023 02:52
Add backslash to double quotes in a string where not already present.
<?php
/**
* Add backslash to double quotes unless already present.
*/
class EscapeDoubleQuotes {
public function __invoke(string $query): string {
// This is four-backslashes because of preg--confusing--that's why this
// class exists to remove the confusion.
<?php
namespace AKlump\PHPUnit;
/**
* Given a mock object make it iterable using $data.
*
* @url https://stackoverflow.com/a/32422586
*
* @code
@aklump
aklump / _lifted-corners.scss
Last active January 20, 2018 21:04
Create that cool lifted corners 3-D effect on an image: `span>img`.
//
// For a given html structure:
//
// <span class="image"><img src="..."></span>
//
// Use this in your SCSS file:
//
// .image {
// @include lifted-corners()
// }