Skip to content

Instantly share code, notes, and snippets.

@brooke-heaton
Last active March 3, 2017 02:53
Show Gist options
  • Save brooke-heaton/5af86206cfbb69d1553f44b51ee777c5 to your computer and use it in GitHub Desktop.
Save brooke-heaton/5af86206cfbb69d1553f44b51ee777c5 to your computer and use it in GitHub Desktop.
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'));
$topic->uid = 1;
$topic_wrapper = entity_metadata_wrapper('node', $topic);
$topic_wrapper->title = $name;
$topic_wrapper->title = $topic_wrapper->title->value();
if ($parent > 0) {
$topic_wrapper->field_about->set(array($parent));
}
$topic_wrapper->status = 1;
$topic_wrapper->save();
$node_id = $topic_wrapper->getIdentifier();
$data = array(
'nid' => $node_id,
'tid' => $tid,
'vid' => 49
);
switch (drupal_write_record('nat', $data)) {
case FALSE:
$message = "NAT record save failed\n";
echo $message;
$this->queueMessage($message, MigrationBase::MESSAGE_INFORMATIONAL);
break;
case SAVED_NEW:
$message = "New NAT record saved for $name \n";
echo $message;
$this->queueMessage($message, MigrationBase::MESSAGE_INFORMATIONAL);
break;
case SAVED_UPDATED:
$message = "NAT record updated for $name \n";
echo $message;
$this->queueMessage($message, MigrationBase::MESSAGE_INFORMATIONAL);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment