Skip to content

Instantly share code, notes, and snippets.

@gwagroves
gwagroves / module.php
Last active April 7, 2024 13:02
Drupal 8 Paragraphs: Add a theme suggestion based on the paragraph and the type of the parent node / entity.
<?php
/**
* Implements theme_suggestions_HOOK_alter().
*/
function mytheme_theme_suggestions_paragraph_alter(array &$suggestions, array $variables) {
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $variables['elements']['#paragraph'];
/** @var \Drupal\Core\Entity\ContentEntityInterface $parent */
$parent = $paragraph->getParentEntity();
@gwagroves
gwagroves / copy_git_diff.sh
Created September 12, 2017 18:14
Copy git modified files to another directory
cp -pv --parents `git diff --name-only master..develop -- source/directory` target/directory
@gwagroves
gwagroves / drush.bash
Last active July 22, 2021 09:59
Drupal 8 Drush export config to gzip tar
# Change working directory to a temporary location
cd ~/temp
# Export config to "config" directory
# Change `@mysite.local` to your local drush alias
drush @mysite.local config-export --destination=~/temp/config
# Tar all files in "config" directory to `config.tar.gz`
tar -czf config.tar.gz -C config .
@gwagroves
gwagroves / gist:3fd1c8c2a342826671c0c17a42cd43b1
Last active October 10, 2019 14:38
MySQL drop all tables from a database
# The following assumes credentials are saved in .my.cnf (or similar)
#
# Create a file with a drop table command for each table.
# (Replace "database" with the name of the database.)
#
mysqldump --add-drop-table --no-data database | grep 'DROP TABLE' > ~/little_bobby_tables.sql
# Check.
#
@gwagroves
gwagroves / view_title.php
Created August 23, 2017 10:11
Get a Drupal 8 View title and URL from a path.
<?php
use Drupal\views\Views;
// Get the View title and URL from a path.
$path = '/my-path';
$url = \Drupal::service('path.validator')->getUrlIfValid($path);
@gwagroves
gwagroves / no-scroll.css
Created October 7, 2016 07:48
Prevent body scrolling on iOS
/* http://stackoverflow.com/a/19054794/698511 */
body.no-scroll {
left: 0;
-webkit-overflow-scrolling: touch;
position: fixed;
top: 0;
width: 100%;
}
@gwagroves
gwagroves / path.md
Created June 29, 2018 07:43
Pathauto token for path from entity reference field

Content type event has an entity reference field containing a location.

We want the following URL structure:

# For the location
/location-title

# For the event
/location-title/event/event-title
@gwagroves
gwagroves / lowercase.sh
Created March 29, 2018 17:18
Lowercase uuidgen
uuidgen | tr "[:upper:]" "[:lower:]"
@gwagroves
gwagroves / mymodule.php
Last active March 13, 2018 10:47
Drupal 8 calculate separate time for date time field
<?php
/**
* Implements hook_preprocess_HOOK().
*/
function module_preprocess_node__content_type(&$variables) {
$node = $variables['elements']['#node'];
// Get time as separate variable.
// Calculate using system timezone.
@gwagroves
gwagroves / path.php
Created September 14, 2017 15:43
Drupal 8 get absolute path to core / module / profile
<?php
// See https://api.drupal.org/api/drupal/core%21includes%21bootstrap.inc/function/drupal_get_path/8.4.x
$core_path = \Drupal::service('file_system')->realpath(
drupal_get_path('core', '')
);