Skip to content

Instantly share code, notes, and snippets.

View klaasvw's full-sized avatar

Klaas Van Waesberghe klaasvw

View GitHub Profile
@klaasvw
klaasvw / call-protected-method.php
Created April 25, 2024 07:05
Call protected method in PHP
‎‎​
@klaasvw
klaasvw / schema.ts
Last active December 7, 2023 10:27
Yup validation based on other data
import * as Yup from 'yup';
import type { MyType } from 'my-types-package';
import { MyOtherSchema } from 'kotk-vdb';
export const MySchema = (
otherData: MyType,
otherValidatorFn: (value: string, dataId: string) => Promise<boolean>
) => {
return {
simpleSelect: Yup.string()
@klaasvw
klaasvw / module_name.install
Created December 1, 2023 14:14
Add base field to existing entity type
/**
* Add the entity_type field_name field.
*/
function module_name_update_9001() {
$type_manager = \Drupal::entityTypeManager();
$type_manager->clearCachedDefinitions();
$entity_type = $type_manager->getDefinition('entity_type');
$storage_definition = EntityTypeClass::baseFieldDefinitions($entity_type);
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition(
@klaasvw
klaasvw / nextjs-basepath.ts
Created November 29, 2023 16:06
Access Next.js basePath without hooks
import Router from "next/router";
export const functionName = () => {
console.log(Router.basePath);
};
@klaasvw
klaasvw / composer.json
Created October 15, 2021 11:45
Use Drupal issue fork in composer
‎‎​
@klaasvw
klaasvw / getscheme.php
Created December 6, 2020 08:46
Get scheme from URI in Drupal 9
<?php
$uri = 'public://path/to/file.txt';
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
$scheme = $stream_wrapper_manager->getScheme($uri);
// $scheme = 'public';
@klaasvw
klaasvw / stripscheme.php
Created December 6, 2020 08:44
Remove or strip the scheme from a file stream wrapper in Drupal 9
<?php
$uri = 'public://path/to/file.txt';
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
$target = $stream_wrapper_manager->getTarget($uri);
// $target = 'path/to/file.txt';
@klaasvw
klaasvw / download.php
Last active December 6, 2020 08:28
Download file in Drupal using Guzzle
<?php
$external_file = 'https://www.example.com/test.png'
$destination = 'public://file.png';
$response = \Drupal::httpClient()->get($external_file, ['sink' => $destination]);
@klaasvw
klaasvw / your_module.pages_default.inc
Created October 13, 2013 10:38
Export custom variants for existing page manager pages. This allows you to for example export a custom version of the user_view page.
<?php
/**
* @file
* Default page manager content.
*/
/**
* Implements hook_default_page_manager_handlers().
*/
@klaasvw
klaasvw / your_module.module
Created October 10, 2013 10:28
Fix missing addthis toolbox buttons
<?php
/**
* Implements hook_addthis_markup_alter().
*
* Fix addthis bug where services aren't added.
*/
function your_module_addthis_markup_alter(&$markup) {
if (isset($markup['#display']['type']) && $markup['#display']['type'] == 'addthis_basic_toolbox') {
$addthis_services = array_filter(variable_get('addthis_enabled_services', array()));