Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
DanLaufer / Drupal 8 - Programmatically update canonical urls
Last active October 19, 2018 11:53
Drupal 8 - Programmatically find custom canonical urls and set default
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
->exists('field_meta_tags')
->execute();
$count = 0;
foreach($node_ids as $node_id) {
@DanLaufer
DanLaufer / Drupal 8 - Validate a minimum number of paragraphs
Last active March 25, 2025 06:27
Drupal 8 - Validate a minimum number of paragraphs
/**
* implements hook_form_node_form_alter()
* @author Daniel Laufer
* Add validation to the Resource Center content types.
*/
function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
$form['#validate'][] = '_mymodule_node_form_validate';
}
@DanLaufer
DanLaufer / Alexa - Adjust how Alexa says words using Phonetics
Created August 8, 2018 14:44
Alexa - Adjust how Alexa says words using Phonetics
// using https://open-dict-data.github.io/ipa-lookup/en/?#
<phoneme alphabet="ipa" ph="/ˈk æp/">cap</phoneme>
@DanLaufer
DanLaufer / Drupal 8 - Expand Paragraph Titles
Last active October 19, 2018 04:45
Drupal 8 - Expand Paragraph Titles
/**
* implements hook_form_node_form_alter()
* @author Daniel Laufer
* Add validation to the XXX content type.
*/
function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// expand paragraph titles
$form['#attached']['library'][] = 'mytheme/expand_paragraph';
}
@DanLaufer
DanLaufer / js-if-exists-shorthand.js
Last active May 12, 2019 17:48
JS - if exists shorthand
this.App || (this.App = {});
@DanLaufer
DanLaufer / Bash - Function to open Chrome
Created October 19, 2018 14:31
Bash - Function to open Chrome
function chrome(){
/usr/bin/open -a "/Applications/Google Chrome.app" "$1"
}
@DanLaufer
DanLaufer / Bash - Function add aliases
Created October 19, 2018 14:33
Bash - Function add aliases
# this command will add an alias and source the .zshrc
# $1 is the keys for the alias
# $2 is what will run. Put in quotes.
function newalias
{
if [ "$1" != "" ] && [ "$2" != "" ]; then
echo 'alias '$1'='"\""$2"\"" >> ~/.zshrc
alias "$1"="$2"
else
echo "The newalias command takes two arguments as follows: newalias <alias command> <alias function, in double quotes>"
@DanLaufer
DanLaufer / Bash - Function to check for an odd number of occurances of a string in a file
Last active October 19, 2018 14:35
Bash - Function to check for an odd number of occurances of a string in a file
# $1 is the name of the element to search
# $2 is the file to search
function checkodd(){
if [ $# -ne 2 ]; then
echo "Please use the following format: checkodd <string to check> <location>"
else
for f in "$2"/* "$2"/**/* ; do
if [ -f "$f" ]; then
stringcount=$(grep -o $1 $f | wc -l)
if [ $((stringcount % 2)) -eq 1 ]; then
@DanLaufer
DanLaufer / Bash - Function to make a new drupal 8 site with lando
Created October 19, 2018 14:36
Bash - Function to make a new drupal 8 site with lando
function drupalnew
{
if [ "$1" = "--help" ]; then
echo "The drupalnew command takes 2 arguments: drupalnew <sitename | ex: my_site> <version | ex: 8.6.x>"
return 1
fi
if [ "$1" = "--version" ]; then
echo "Drupalnew 0.0.1"
return 1
@DanLaufer
DanLaufer / Bash - Alias for grep with flags
Last active October 19, 2018 14:37
Bash - Alias for a better grep
function greppy
{
grep -Hnoir "$1" .
}