Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
DanLaufer / handlers.js
Created October 19, 2018 18:55
Alexa Skill - Find lunch near fishtown
const respondToAskAboutLunch = require('./lunch-handler');
module.exports = {
AskAboutLunchIntent: respondToAskAboutLunch,
LaunchRequest: respondToLaunch,
'AMAZON.HelpIntent': respondToHelp,
'AMAZON.StopIntent': respondToCancel,
'AMAZON.CancelIntent': respondToCancel,
};
@DanLaufer
DanLaufer / handlers.js
Last active October 19, 2018 18:48
Alexa Skill - Example including relevant JS files
const languageStrings = require('./languageStrings');
const getSession = require('./util').getSession; // getSession(handlerInput)
const setSession = require('./util').setSession; // setSession(handerInput, attributes)
const buildResponse = require('./util').buildResponse; // buildResponse(handlerInput, speakText, repromptText = null)
const buildFinalResponse = require('./util').buildFinalResponse; // buildFinalResponse(handlerInput, speakText)
const checkIntentType = require('./util').checkIntentType;
const checkIntentName = require('./util').checkIntentName;
const CancelAndStopIntentHandler = {
canHandle : function(handlerInput) {
@DanLaufer
DanLaufer / Drupal 8 - Module create content type and fields on install
Created October 19, 2018 17:54
Drupal 8 - Module create content type and fields on install
<?php
/**
* Implements hook_install().
*/
function gamecontenttype_install() {
node_types_rebuild();
$types = node_type_get_types();
foreach(_gamecontenttype_fields() as $field) {
field_create_field($field);
}
@DanLaufer
DanLaufer / Drupal 8 - SQL - Search for node in DB with specific field value
Created October 19, 2018 17:52
Drupal 8 - SQL - Search for node in DB with specific field value
//Join node table with a field table and search for published nodes of a certain type with a specific field value:
SELECT node.nid AS node_id,
LEFT(node.title, 10) AS node_title, field_data_field_reference_database_id.field_reference_database_id_value as ref_id
FROM node
LEFT JOIN field_data_field_reference_database_id
ON node.nid = field_data_field_reference_database_id.entity_id
WHERE type='location'
AND status='1'
AND field_data_field_reference_database_id.field_reference_database_id_value IS NULL;
@DanLaufer
DanLaufer / Drupal 8 - Make new variables in settings.php
Created October 19, 2018 17:46
Drupal 8 - Make new variables in settings.php
$config['configuration.prefix']['myVariable'] = 'Value';
@DanLaufer
DanLaufer / Bash - Drupal 8 - Script to add permissions for a role to a paragraph
Created October 19, 2018 17:44
Bash - Drupal 8 - Script to add permissions for a role to a paragraph
#!/usr/bin/env bash
# drushpap - drush paragraph-add-permission
# usage: ./scripts/drushpap <paragraph_machine_name>
# to view the list of current permissions and roles, type: lando drush role:list
if [ "$1" = "" ]; then
echo "Error: incorrect number of arguments."
echo " - The drushpap command takes 1 argument: the machine name of the paragraph. It then gives permission to authenticated users for create, delete, update, and view, and for anonymous users to view only."
@DanLaufer
DanLaufer / Bash - Drupal 8 - Script to add permissions for a role to a node
Created October 19, 2018 17:43
Bash - Drupal 8 - Script to add permissions for a role to a node
#!/usr/bin/env bash
# drushnap - drush node-add-permissions
# usage: ./scripts/drushnap <role_machine_name> <node_machine_name>
# to view the list of current permissions and roles, type: lando drush role:list
if [ "$1" = "" ] || [ "$2" == "" ]; then
echo "Error: incorrect number of arguments."
echo " - The drushnap command takes 2 arguments: the machine name of the role and the machine name of the node. It then gives permission to that role for create, delete any, delete own, edit any, edit own, delete revisions, revert revisions, view revisions, and translate for that node."
@DanLaufer
DanLaufer / mymodule.install
Created October 19, 2018 17:39
Drupal 8 - Remove all content of type on module uninstall
/**
* @file
* Install, update and uninstall functions for the sports_system module.
*/
/**
* Implements hook_uninstall().
*/
function sports_system_uninstall() {
$storage = \Drupal::entityManager()->getStorage('node');
@DanLaufer
DanLaufer / Drupal 8 - Act on files in php
Created October 19, 2018 17:36
Drupal 8 - Act on files in php
// load file from id
$file_entity = \Drupal::entityTypeManager()->getStorage('file')->load($target_id);
//Get Uri/Url of file
//in preprocess field
$svg_uri = $vars['items']['0']['content']['#file']->getFileUri();
//in preprocess node
$svg_uri = $node->field_svg_image->entity->get('uri')->getValue();
@DanLaufer
DanLaufer / Drupal 8 - Get user from node
Created October 19, 2018 17:32
Drupal 8 - Get user from node
$user_entity = $node->uid->entity;
$variables['author_name'] = $user_entity->get('field_display_name')->view();
$user_picture = $user_entity->get('user_picture')->getValue();
if (_user_has_picture($user_picture)) {
$target_id = $user_picture[0]['target_id'];
$variables['author_picture_uri'] = _get_user_picture_uri($target_id);
}
$variables['author_bio'] = $user_entity->get('field_bio')->view();
$variables['author_twitter'] = $user_entity->get('field_twitter_username')->value;
$variables['author_url'] = $user_entity->urlInfo()->toString();