Last active
March 3, 2017 02:53
-
-
Save brooke-heaton/5af86206cfbb69d1553f44b51ee777c5 to your computer and use it in GitHub Desktop.
Add node when taxonomy term is added
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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