Skip to content

Instantly share code, notes, and snippets.

View aklump's full-sized avatar

Aaron Klump aklump

View GitHub Profile
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 {
@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;