Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / NodeExcludeProcessor.php
Created October 24, 2017 09:43
Exclude nodes from search API based on field value
<?php
namespace Drupal\my_module\Plugin\search_api\processor;
use Drupal\search_api\Processor\ProcessorPluginBase;
/**
* Excludes entities marked as 'excluded' from being indexes.
*
* @SearchApiProcessor(
@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', '')
);
@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 / get_module_path.php
Created September 12, 2017 17:59
Drupal 8 get module path
<?php
$module_handler = \Drupal::service('module_handler');
$path = $module_handler->getModule('your_module')->getPath();
@gwagroves
gwagroves / drupal_format_date.php
Created September 2, 2017 05:36
Format date to be saved in Drupal.
<?php
/**
* Format a date to be saved in Drupal.
*
* @param string $date
*
* @return string
*/
private function formatDate($date) {
@gwagroves
gwagroves / module.php
Last active August 25, 2017 11:54
Drupal 8: Prevent node pages (full view) being shown to non-admin users.
<?php
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Implements hook_preprocess_HOOK().
*/
function my_module_preprocess_node(&$variables) {
$node = $variables['node'];
$nodetype = $node->getType();