Skip to content

Instantly share code, notes, and snippets.

@mglaman
mglaman / TrimMiddleware.php
Created January 10, 2023 15:23
TrimMiddleware for Drupal
<?php
declare(strict_types=1);
namespace Drupal\mymodule\StackMiddleware;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
@wayne-weibel
wayne-weibel / reset_hook_post_update.php
Last active January 24, 2023 10:44
Drupal 9.x Drush 10.x PHP Script to Reset Post Update Hooks, see https://drupal.stackexchange.com/q/238682/11788
<?php
use Drush\Commands\DrushCommands;
/**
* @file
* Reset module hook_post_update_NAME
*
* A tool for developing and debugging post_update hooks.
*
@twfahey1
twfahey1 / example-wp.sh
Last active August 4, 2022 11:04
Pantheon - run php-script (php eval alternative) via terminus against a site
cat hello.php | terminus remote:wp ${SITE}.${ENV} -- eval-file -
@davidjguru
davidjguru / HumansTxtBasicTest.php
Created April 18, 2020 20:28
Functional Testing in Drupal 8|9: Humans.txt Basic Test Class based in BrowserTestBase / PHPUnit / Mink (Final version)
<?php
namespace Drupal\Tests\humanstxt\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Url;
/**
* Tests basic functionality of configured humans.txt files.
*
@Erikdekamps
Erikdekamps / module.install
Last active May 27, 2021 12:56
Drupal 8 - Load taxonomy terms sorted by weight
/**
* Get taxonomy terms sorted by weight.
*
* @param int $vid
* The vocabulary id.
*
* @return array
* Returns an array of term id | name.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
@jasonwaters
jasonwaters / karabiner-rules.json
Last active August 16, 2024 13:27
Remap the home and end keys in macOS using Karabiner Elements
{
"rules": [
{
"description": "change home key to command + left_arrow",
"manipulators": [
{
"from": {
"key_code": "home"
},
"to": [
@DanLaufer
DanLaufer / Drupal 8 - EntityQuery stuff
Last active November 6, 2021 12:37
Drupal 8 - EntityQuery stuff
// chaining with .entity.
$query = \Drupal::entityQuery('node');
$articles_by_name = $query->condition('type', 'article')
->condition('uid.entity.name', 'admin')
->execute();
//Equal/>=/<=/etc (3rd parameter)
// Find particular nodes published in the last year.
$query = \Drupal::entityQuery('node');
@jez500
jez500 / d8-views-exp-form-alter.php
Last active October 12, 2023 12:20
Drupal 8 - Hook form alter views exposed form
<?php
/**
* Implements hook_form_alter().
*/
function MYMODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$view_names = array('my_view_name');
$view = $form_state->getStorage('view');
if ($form_id == 'views_exposed_form' && in_array($view['view']->id(), $view_names)) {
// Do some shilzzle.
function THEMENAME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
if (isset($variables['elements']['content']['#block_content'])) {
$suggestions[] = 'block__' . $variables['elements']['content']['#block_content']->bundle();
}
}
function MYMODULE_template_preprocess_default_variables_alter(&$variables) {
$current_path = \Drupal::service('path.current')->getPath();
$variables['current_path'] = $current_path;
}