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 / RollbackMigrationIdList.php
Last active April 20, 2017 21:06
Rollback a subset idlist of a specific Drupal 7 migration
<?php
/**
* Rollback only 'articles' type nodes using the idlist option
* This is useful if you can't for some reason rollback the whole migration
*/
function migration_update_7001() {
// Get the 'migrate' database and query for nodes that should not have been
// in our migration class - 52 is the term id for 'articles'
$results = Database::getConnection('default', 'migrate')
@brooke-heaton
brooke-heaton / RewriteAbsoluteURLAsAlias.php
Last active March 3, 2017 20:53
Find and replace absolute Drupal 6 URLS with aliases so that Migrate module and redirect them
<?php
/**
* Helper class to pass parameters to callback function within preg_replace_callback
* to replace absolute D6 urls with D6 aliases
*/
class InternalLinksConverter {
function replace($matches) {
if (isset($this->link_aliases)) {
$link_aliases = $this->link_aliases;
@brooke-heaton
brooke-heaton / GetMigratedNodeNewNID.php
Last active March 3, 2017 03:02
Get New Entity ID for Migrated Entity Based on Old Entity ID in Migration Map
<?php
/**
* Helper function to fetch the new Drupal 7 ID of a migrated entity
* if we know the old id
*
* @param $old_nid - old nid of biography node from legacy node ref
* @return tid of the new term
*/
@brooke-heaton
brooke-heaton / DeleteOldNodesByNIDCutoff.php
Created March 3, 2017 02:52
Delete all Drupal 7 Nodes prior to a certain node ID
<?php
/**
* Delete all old nodes prior to a certain node id
* to prepare DB for migration
*/
function DeleteOldNodesByNIDCutoff($nid) {
$old_nodes = db_select('node')
->fields('node', array('nid'))
->condition('node.nid', $nid, '<')
@brooke-heaton
brooke-heaton / GetCountTaxonomyTermsByVocab.php
Last active March 3, 2017 02:49
Get Count of Taxonomy Terms per Vocabulary ID
<?php
/**
* This Drupal 7 helper function will return and echo the count of
* taxonomy terms by vocabulary
* @param $vid - integer for vocabulary id
*/
function GetCountTaxonomyTermsByVocab($vid) {
$query = new EntityFieldQuery;
@brooke-heaton
brooke-heaton / checkTermExistsByName.php
Last active March 3, 2017 02:53
Check if a Taxonomy Term exists by Term Name
<?php
/**
* This helper Drupal 7 function can look up if a term exists and is useful
* if adding terms in a postImport() method on a migration or
* if needing to avoid duplicates or relate entities to the term.
*/
public function checkTermExistsByName($name) {
$query = new EntityFieldQuery();
@brooke-heaton
brooke-heaton / checkNodeExistsByTitle.php
Last active March 3, 2017 02:53
Check if a node exists by node title using Entity Field Query (EFQ)
<? php
/**
* This Drupal 7 helper function can look up if a node exists and is useful
* if adding nodes in a postImport() method on a migration or
* if needing to avoid duplicates or relate nodes.
*/
public function checkNodeExistsByTitle($title) {
$query = new EntityFieldQuery();
@brooke-heaton
brooke-heaton / addNATNode.php
Last active March 3, 2017 02:53
Add node when taxonomy term is added
<?php
/**
* This Drupal 7 helper function adds a node of bundle 'topic' which has a nat term reference
* field 'field_about' which should correspond to the parent term for this node's
* sibling 'node'.
*/
public function addNATNode($tid, $name) {
$parent = db_query('SELECT parent FROM taxonomy_term_hierarchy WHERE tid = :tid', array(':tid' => $tid))->fetchField();
$topic = entity_create('node', array('type' => 'topic'));