Skip to content

Instantly share code, notes, and snippets.

View brooke-heaton's full-sized avatar

Brooke Heaton brooke-heaton

  • NASWA
  • Asheville, Cackalacky
View GitHub Profile
@brooke-heaton
brooke-heaton / my_migration.install.php
Created June 19, 2018 20:50
Add custom mapping tables for a migration process that merges terms - these tables will be used to import data from a csv file
<?php
/**
* Add legacy and new term name mapping for Section and Subject vocabularies
*/
function my_migration_update_8001() {
$section_schema = [
'description' => 'Legacy section term name to new section term name',
'fields' => [
'legacy_id' => [
@brooke-heaton
brooke-heaton / MyMigrationMigrationEventSubscriber.php
Created June 19, 2018 20:44
Drupal 8 Event Subscriber to trigger a function to drop a mySql table and import data from a file
<?php
/**
* @file
* Contains \Drupal\migrate\Event\MigrateEvent.
*/
namespace Drupal\my_migration\Event;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigrateImportEvent;
@brooke-heaton
brooke-heaton / merge_ids_during_migration.php
Last active June 29, 2018 17:45
Merge and rename legacy terms based on a mapping on a my_migration.module
<?php
use Drupal\Core\Database\Database;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate\Plugin\MigrateSourceInterface;
/**
* Populate vocab renaming tables from our csv source file with the old name and new name - this
@brooke-heaton
brooke-heaton / Drupal8_views_query_alter.php
Created August 11, 2017 22:13
Alters a Drupal 8 view by adding a Left Join and Where condition
<?php
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;
/**
* Implementation of hook_views_query_alter
* @param type $view
* @param type $query
@brooke-heaton
brooke-heaton / D8_color_palate_selector.php
Created July 11, 2017 19:21
Drupal 8 Theme Color Palate Selector
<?php
function yourthemename_form_system_theme_settings_alter(&$form, Drupal\Core\Form\FormStateInterface $form_state) {
// add the proper js and css files from your admin them and the color_field module
$form['#attached']['library'][] = 'myadmintheme/color-field-widget-box';
$form['#attached']['library'][] = 'color_field/color-field-widget-box';
// define the dark colors to be used in the palette
$default_dark_colors = array(
@brooke-heaton
brooke-heaton / MapD7NodeUrlAlias.php
Last active June 23, 2017 14:29
Map Drupal 7 node and url_alias tables
<?php
/**
* The alias table entries are prefixed with the actual path
* /node/nid
*/
function return_node_titles_and_aliases() {
$results = db_query("
SELECT
@brooke-heaton
brooke-heaton / d8_redirect_regstration.php
Last active June 13, 2017 19:42
D8 Redirect on registration form
<?php
/**
* Implements hook_form_FORM_ID_alter().
*/
function capitalcamp_glue_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// @todo come up with a more elegant approach here.
// Alter login form and add own custom submit handler.
$form['actions']['submit']['#submit'][] = '_capitalcamp_glue_user_register_form_submit';
}
@brooke-heaton
brooke-heaton / HideAddressCityStateConditonally.js
Last active May 2, 2017 03:40
Conditionally Hide Subfields in Drupal 8 Webform Address Elements
(function ($, Drupal) {
'use strict';
Drupal.behaviors.registration_form = {
attach: function (context) {
// Conditionally hide the city/state unless user chooses United States.
$("#edit-address-country").change(function () {
if ($("#edit-address-country").val() === "United States") {
$('.form-item-address-city').show();
$('.form-item-address-state-province').show();
}
@brooke-heaton
brooke-heaton / Drupal8FormAlter.php
Created May 2, 2017 03:21
Drupal 8 Form Alter to redirect User from User Registration Form
<?php
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// @todo come up with a more elegant approach here.
// Alter login form and add own custom submit handler.
$form['actions']['submit']['#submit'][] = '_mymodule_user_register_form_submit';
}
@brooke-heaton
brooke-heaton / ConditionallyRequiredWebformFields.php
Created May 2, 2017 03:19
Conditionally require Drupal 8 fields and subfields in an address webform
<?php
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_webform_submission_registration_form_alter(array &$form, FormStateInterface $form_state) {
// Show 'Country' as first field.
$form['elements']['personal_information']['address']['#country__weight'] = -2;
// Show 'State' next, but conditionally.
$form['elements']['personal_information']['address']['#state_province__weight'] = -1;