private function saveTerms() { $weight = 0; foreach ($this->dbhWp->query('SELECT t.* FROM wp_term_taxonomy tt INNER JOIN wp_terms t USING(term_id) WHERE taxonomy = \'category\'', PDO::FETCH_ASSOC) as $term) { $drupalTerm = array(); $drupalTerm['vid'] = $this->vid; $drupalTerm['name'] = $term['name']; $drupalTerm['weight'] = $weight++; taxonomy_save_term($drupalTerm); $this->dbhImport->query('INSERT INTO terms VALUES (' . $term['term_id'] . ', ' . $drupalTerm['tid'] . ')'); } }
function _create_new_term_for_taxon_field($bundle_name, $field_name, $term_name) { $vid = _get_vocab_id_from_taxon_field($bundle_name, $field_name); if (!$vid) { return FALSE; } $new_term = array('vid' => $vid, 'name' => $term_name); taxonomy_save_term($new_term); return $new_term['tid']; }
/** * {@inheritdoc} */ public function termCreate(\stdClass $term) { // Map vocabulary names to vid, these take precedence over machine names. if (!isset($term->vid)) { $vocabularies = \taxonomy_get_vocabularies(); foreach ($vocabularies as $vid => $vocabulary) { if ($vocabulary->name == $term->vocabulary_machine_name) { $term->vid = $vocabulary->vid; } } } if (!isset($term->vid)) { // Try to load vocabulary by machine name. $vocabularies = $this->taxonomyVocabularyLoadMultiple(array($term->vid)); if (!empty($vocabularies)) { $vids = array_keys($vocabularies); $term->vid = reset($vids); } } // If `parent` is set, look up a term in this vocab with that name. if (isset($term->parent)) { $parent = \taxonomy_get_term_by_name($term->parent); if (!empty($parent)) { $parent = reset($parent); $term->parent = $parent->tid; } } if (empty($term->vid)) { throw new \Exception(sprintf('No "%s" vocabulary found.')); } // Attempt to decipher any fields that may be specified. $this->expandEntityFields('taxonomy_term', $term); // Protect against a failure from hook_taxonomy_term_insert() in pathauto. $current_path = getcwd(); chdir(DRUPAL_ROOT); $term_array = (array) $term; \taxonomy_save_term($term_array); chdir($current_path); // Loading a term by name returns an array of term objects, but there should // only be one matching term in a testing context, so take the first match // by reset()'ing $matches. $matches = \taxonomy_get_term_by_name($term->name); $saved_term = reset($matches); return $saved_term; }
<?php $vid = 5; //vocab we're adding to $parent = 1; //parent tid $terms = array('Ireland', 'Scotland', 'Wales', 'England'); foreach ($terms as $term) { $edit = array("vid" => $vid, "name" => $term, "parent" => $parent); $msg = taxonomy_save_term($edit); print $edit['name'] . ": {$msg}<BR />\n"; }
/** * (non-PHPdoc) * @see Entity::_update() */ protected function _update($term, $tid) { $edit = (array) $term; $edit['tid'] = (int) $tid; $this->__restoreData($edit); if ($this->getSettings()->getOption('merge_duplicates', FALSE)) { $this->__mergeExisting($edit); } taxonomy_save_term($edit); }