public static function checkTerms($vocabName, $termName)
 {
     /*@param $vocabName
      *@param $termName
      *@return $tid
      */
     $vocab = taxonomy_vocabulary_machine_name_load($vocabName);
     $vid = $vocab->vid;
     //check term
     $checkTerm = taxonomy_get_term_by_name($termName, $vocabName);
     if (empty($checkTerm)) {
         $term = new stdClass();
         $term->name = $termName;
         $term->vid = $vid;
         taxonomy_term_save($term);
         $termArray = taxonomy_get_term_by_name($termName, $vocabName);
         $term = current($termArray);
         $tid = $term->tid;
         return $tid;
     } else {
         $term = current($checkTerm);
         $tid = $term->tid;
         return $tid;
     }
 }
コード例 #2
0
function wyc_import_terms($vid, $tid, $text)
{
    $term = new stdClass();
    $term->vid = $vid;
    //$term->tid = $tid;
    $term->name = $text;
    taxonomy_term_save($term);
    var_dump($term);
    return $term->tid;
}
コード例 #3
0
 public function presave($entity_type, $entity, $field, $instance, $langcode, &$items)
 {
     foreach ($items as $delta => $item) {
         if ($item['target_id'] == 'autocreate') {
             $term = (object) $item;
             unset($term->tid);
             taxonomy_term_save($term);
             $items[$delta]['target_id'] = $term->tid;
         }
     }
 }
コード例 #4
0
ファイル: Drupal7.php プロジェクト: ian-yin/drupalextension
 /**
  * {@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 = \taxonomy_vocabulary_load_multiple(FALSE, array('machine_name' => $term->vocabulary_machine_name));
         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, $term->vocabulary_machine_name);
         if (!empty($parent)) {
             $parent = reset($parent);
             $term->parent = $parent->tid;
         }
     }
     if (empty($term->vid)) {
         throw new \Exception(sprintf('No "%s" vocabulary found.'));
     }
     \taxonomy_term_save($term);
     // 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;
 }
コード例 #5
0
 /**
  * Save specified taxonomy terms to vocabulary
  *
  * @param mixed $name
  *   A string or an array of strings
  * @param int|string $voc
  *   (optionally) Vocabulary id/name
  *
  * @return mixed
  *   Fetched and inserted tids
  */
 public static function setTaxonomyTerms($name, $voc = 0)
 {
     if (!is_numeric($voc)) {
         $voc = self::getVidFromName($voc);
     }
     if (!is_array($name)) {
         $name = array($name);
     }
     $tids = array();
     $existing = self::getTaxonomyIdByName($name, $voc);
     if (!empty($existing)) {
         $existing = taxonomy_term_load_multiple($existing, array('vid' => $voc));
         foreach ($existing as &$term) {
             $tids[drupal_strtolower($term->name)] = $term->tid;
             $term = NULL;
         }
     }
     unset($existing);
     foreach ($name as &$term) {
         if (!isset($tids[drupal_strtolower($term)])) {
             $t = new stdClass();
             $t->vid = $voc;
             $t->name = $term;
             taxonomy_term_save($t);
             $tids[$t->name] = $t->tid;
             $t = NULL;
             $term = NULL;
         }
     }
     return $tids;
 }
コード例 #6
0
ファイル: Entity.php プロジェクト: vishalred/redtest-core-pw
 /**
  * Saves the entity to database. This is copied from entity_save() function
  * since entity module may not be installed.
  */
 public function saveProgrammatically()
 {
     $info = entity_get_info($this->entity_type);
     if (method_exists($this->entity, 'save')) {
         $this->entity->save();
     } elseif (isset($info['save callback'])) {
         $info['save callback']($this->entity);
     } elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
         entity_get_controller($this->entity_type)->save($this->entity);
     } elseif ($this->entity_type == 'node') {
         node_save($this->entity);
     } elseif ($this->entity_type == 'user') {
         user_save($this->entity);
     } elseif ($this->entity_type == 'taxonomy_term') {
         taxonomy_term_save($this->entity);
     }
 }
コード例 #7
0
 /**
  * @param \Behat\Gherkin\Node\TableNode $tagsTable
  * @throws \Exception
  * @Given tag <tag> for course <course>:
  */
 public function createTags(TableNode $tagsTable)
 {
     foreach ($tagsTable->getHash() as $data) {
         if (!isset($data['tag']) or !isset($data['course'])) {
             throw new Exception('Missing information to create tags. Need tag and course.');
         }
         $vocab = taxonomy_vocabulary_machine_name_load('annotation_tags');
         $course = $this->findNodeByTitle('course', $data['course']);
         $term = new stdClass();
         $term->name = $data['tag'];
         $term->vid = $vocab->vid;
         $term->field_term_course[LANGUAGE_NONE][0]['target_id'] = $course->nid;
         if (!taxonomy_term_save($term)) {
             throw new Exception(sprintf('Could not create term %s'), $data['tag']);
         }
     }
 }
コード例 #8
0
<?php

// Create some variables.
$clear_old_terms = TRUE;
$vocabulary = 'vocab_machine_name';
$new_terms = array('Example 1', 'Example 2', 'Example 3');
// Load the vocabulary ID.
$vid = taxonomy_vocabulary_machine_name_load($vocabulary)->vid;
// Check if we want to clear out the old terms from the vocabulary.
if ($clear_old_terms == TRUE) {
    $terms = taxonomy_get_tree((int) $vid);
    foreach ($terms as $t) {
        taxonomy_term_delete($t->tid);
    }
}
// Create new terms from the $new_terms array.
foreach ($new_terms as $item) {
    $new_term = new stdClass();
    $new_term->name = $item;
    $new_term->vid = $vid;
    taxonomy_term_save($new_term);
}
コード例 #9
0
    if ($res_muni->code != '200') {
        fwrite($stdout, "Could not load municipalities for region: {$region_data->nr}\n");
        die;
    }
    // Create municipality array with id's as keys.
    $municipalities = array();
    array_walk(json_decode($res_muni->data), function ($v) use(&$municipalities) {
        $municipalities[$v->nr] = $v;
    });
    $reg->municipalities = $municipalities;
    // Add region to regions array.
    $regions[$region->nr] = $reg;
}
$vocab = taxonomy_vocabulary_machine_name_load('user_type');
foreach ($regions as $id => $region) {
    $term = new stdClass();
    $term->name = $region->region->navn;
    $term->vid = $vocab->vid;
    // $term->parent = $region_tid;
    $term->parent = 107;
    taxonomy_term_save($term);
    $region_tid = $term->tid;
    foreach ($region->municipalities as $id => $muni) {
        $term = new stdClass();
        $term->name = $muni->navn;
        $term->vid = $vocab->vid;
        // $term->parent = $region_tid;
        $term->parent = 108;
        taxonomy_term_save($term);
    }
}
コード例 #10
0
ファイル: topic_cron.php プロジェクト: sanjay9267/dnaindia
/**
 * Create a taxonomy term with status as api and return the tid.
 */
function custom_create_taxonomy_term($name, $vid, $tag_status)
{
    $term = new stdClass();
    $term->name = $name;
    $term->vid = $vid;
    $term->field_type['und'][0]['value'] = 'base';
    //field: type
    $term->field_verified['und'][0]['value'] = $tag_status;
    //field: verified
    $term->field_version['und'][0]['value'] = 'api';
    //field: version
    taxonomy_term_save($term);
    return $term->tid;
}
コード例 #11
0
/**
 * Populate initial terms into the newsletter_sections taxonomy.
 */
function osha_newsletter_create_taxonomy()
{
    $voc = taxonomy_vocabulary_machine_name_load('newsletter_sections');
    $terms = taxonomy_get_tree($voc->vid);
    if (empty($terms)) {
        $new_terms = array('highlight' => 'Highlights', '' => 'OSH matters', 'publication' => 'Latest publications', 'newsletter_article' => 'Coming soon', 'blog' => 'Blog', 'news' => 'News', 'event' => 'Events');
        $cont_type_term_map = array();
        $new_terms_ct = array_flip($new_terms);
        $weight = 0;
        foreach ($new_terms as $term_name) {
            $term = new stdClass();
            $term->name = $term_name;
            $term->language = 'en';
            $term->vid = $voc->vid;
            // Weight must be an integer.
            $term->weight = $weight++;
            taxonomy_term_save($term);
            if ($term->name == 'Coming soon') {
                variable_set('osha_newsletter_coming_soon_tid', $term->tid);
            }
            $cont_type_term_map[$new_terms_ct[$term->name]] = $term->tid;
        }
        variable_set('osha_newsletter_term_ct_map', $cont_type_term_map);
    }
}
コード例 #12
0
ファイル: FeatureHelper.php プロジェクト: aleph-n/opencholar
 /**
  * Create a term in a given vocabulary.
  *
  * @param $term_name
  *   The name for the new term.
  *
  * @param $vocab_name
  *   The name of the vocabulary.
  *
  * @throws Exception
  *   When vocab not found or not mapped correctly.
  */
 public static function CreateTerm($term_name, $vocab_name)
 {
     // A mapping for existing vocabularies.
     $vocabs = array('authors' => 'authors_personal1', 'biology' => 'biology_personal1', 'math' => 'math_personal1', 'science' => 'science_personal1');
     $params = array('@vocab-name' => $vocab_name);
     if (!array_key_exists($vocab_name, $vocabs)) {
         throw new Exception(t('The vocabulary "@vocab-name" is not mapped to any existing vocabularies.', $params));
     }
     // Load the vocabulary.
     $machine_name = $vocabs[$vocab_name];
     if (!($vocab = taxonomy_vocabulary_machine_name_load($machine_name))) {
         throw new Exception(t('No vocabulary with the name "@vocab-name" was found.', $params));
     }
     // Create and save the new term.
     $term = new stdClass();
     $term->name = $term_name;
     $term->vid = $vocab->vid;
     taxonomy_term_save($term);
 }
function assignNodeFieldValuesAsApprovedTags($nodID)
{
    $n = node_load($nodID);
    $nodeHasBeenChanged = false;
    $tagLabelsToAssign = array();
    // Check the Ownership field for this program/service
    if (!empty($n->field_program_owner_share['und'])) {
        foreach ($n->field_program_owner_share['und'] as $fieldValueInfo) {
            $fieldValue = $fieldValueInfo['value'];
            $tagLabelsToAssign[] = $fieldValue;
        }
    }
    // Check the Need field for this program/service
    if (!empty($n->field_program_needs['und'])) {
        foreach ($n->field_program_needs['und'] as $fieldValueInfo) {
            $fieldValue = $fieldValueInfo['value'];
            $tagLabelsToAssign[] = $fieldValue;
        }
    }
    // Check the Company Maturity field for this program/service
    if (!empty($n->field_program_comp_maturity['und'])) {
        foreach ($n->field_program_comp_maturity['und'] as $fieldValueInfo) {
            $fieldValue = $fieldValueInfo['value'];
            $tagLabelsToAssign[] = $fieldValue;
        }
    }
    // Check the Exporting Maturity field for this program/service
    if (!empty($n->field_program_exporting_maturity['und'])) {
        foreach ($n->field_program_exporting_maturity['und'] as $fieldValueInfo) {
            $fieldValue = $fieldValueInfo['value'];
            $tagLabelsToAssign[] = $fieldValue;
        }
    }
    // Check the Company Size field for this program/service
    if (!empty($n->field_program_company_size['und'])) {
        foreach ($n->field_program_company_size['und'] as $fieldValueInfo) {
            $fieldValue = $fieldValueInfo['value'];
            $tagLabelsToAssign[] = $fieldValue;
        }
    }
    // Get the vocabulary ID for User Submittted Tags
    $vid = createVocabIfNotExist('User Submittted Tags', 'user_submittted_tags');
    // createVocabIfNotExist() returns the vid (Vocab ID)
    // Create the parent term.
    $parent = 'Assumed From Field Values';
    $termAlreadyExistsWithTidOf = db_query("SELECT tid FROM taxonomy_term_data WHERE name='{$parent}' LIMIT 1")->fetchField();
    if ($termAlreadyExistsWithTidOf === false) {
        taxonomy_term_save((object) array('name' => $parent, 'vid' => $vid));
        // [!!] WARNING [!!] - taxonomy_term_save() WILL create duplicate taxonomy terms, ALWAYS check if a taxonomy terms with the same titles exists before calling this fucntion
    }
    // Get the parent-term's ID.
    $parentTermID = db_query("SELECT tid FROM taxonomy_term_data WHERE name='{$parent}' LIMIT 1")->fetchField();
    // Create all terms
    foreach ($tagLabelsToAssign as $term) {
        // Create the child term.
        $termAlreadyExistsWithTidOf = db_query("SELECT tid FROM taxonomy_term_data WHERE name='{$term}' LIMIT 1")->fetchField();
        if ($termAlreadyExistsWithTidOf === false) {
            taxonomy_term_save((object) array('name' => $term, 'vid' => $vid, 'parent' => array($parentTermID)));
            // [!!] WARNING [!!] - taxonomy_term_save() WILL create duplicate taxonomy terms, ALWAYS check if a taxonomy terms with the same titles exists before calling this fucntion
        }
        // Get the child term's ID
        $termID = db_query("SELECT tid FROM taxonomy_term_data WHERE name='{$term}' LIMIT 1")->fetchField();
        // Ensure there is an array for the "Approved Tags" field in this node
        if (empty($n->field_tagged_terms)) {
            $n->field_tagged_terms = array('und' => array());
        }
        // Assign this $termID under the "Approved Tags" field in this node
        $thisNodeAlreadyHasThisTerm = false;
        foreach ($n->field_tagged_terms['und'] as $alreadyExistingTermInfo) {
            $alreadyExistingTermID = $alreadyExistingTermInfo['tid'];
            if (intval($alreadyExistingTermID) === intval($termID)) {
                $thisNodeAlreadyHasThisTerm = true;
                break;
            }
        }
        if (!$thisNodeAlreadyHasThisTerm) {
            $n->field_tagged_terms['und'][] = array('tid' => $termID);
            $nodeHasBeenChanged = true;
        }
    }
    if ($nodeHasBeenChanged) {
        //dsm($n);
        node_save($n);
    }
}
コード例 #14
0
function set_article_categories($articleobj, $bundle, $categoryObj = NULL)
{
    $type = variable_get('brafton_existing_type');
    //Grabs the categories from the feed.
    if ($categoryObj != NULL) {
        $categories = array($categoryObj);
        $video_loader = true;
        $type = 'b_video';
    } else {
        $categories = $articleobj->getCategories();
        $video_loader = false;
    }
    switch ($type) {
        case 'b_news':
            $vocab = 'b_news_t';
            break;
        case 'b_video':
            $vocab = 'b_news_v';
            break;
        default:
            $info = field_info_field(variable_get('brafton_custom_taxonomy'));
            $vocab = $info['settings']['allowed_values'][0]['vocabulary'];
            break;
    }
    //Checks to see if the terms already exist in the Brafton Taxonomy Vocabulary.  If they do not, new terms are created.
    $i = 0;
    $brafton_vocabulary = taxonomy_vocabulary_machine_name_load($vocab);
    $vid = $brafton_vocabulary->vid;
    $cat_array = array();
    foreach ($categories as $category) {
        if ($video_loader) {
            $name = $categories[0]->name;
        } else {
            $name = get_category($categories, $i);
            $i = $i + 1;
        }
        $check_cat = taxonomy_get_term_by_name($name);
        $found = 0;
        foreach ($check_cat as $term) {
            if ($term->vid == $vid) {
                $tid = $term->tid;
                $found = 1;
            }
        }
        if ($found == 0) {
            $new_term = array('vid' => $vid, 'name' => $name);
            $new_term = (object) $new_term;
            taxonomy_term_save($new_term);
            $tid = $new_term->tid;
        }
        array_push($cat_array, $tid);
    }
    //Returns an array of valid term ids for the given article.
    return $cat_array;
}
コード例 #15
0
 /**
  * Implement the save function for the entity.
  */
 public function entity_save($entity)
 {
     taxonomy_term_save($entity);
 }
コード例 #16
0
ファイル: Drupal7.php プロジェクト: acbramley/DrupalDriver
 /**
  * {@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 = \taxonomy_vocabulary_load_multiple(FALSE, array('machine_name' => $term->vocabulary_machine_name));
         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, $term->vocabulary_machine_name);
         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);
     \taxonomy_term_save($term);
     return $term;
 }
コード例 #17
0
 /**
  * Create term in a vocabulary.
  *
  * @param string $term_name
  *    Name of the term.
  * @param string $vocabulary_name
  *    Name of the vocabulary.
  *
  * @Given the term :term_name in the vocabulary :vocabulary_name exists
  *
  * @Then I create a new term :term_name in the vocabulary :vocabulary_name
  */
 public function iCreateNewTermInTheVocabulary($term_name, $vocabulary_name)
 {
     $term = new \stdClass();
     $term->name = $term_name;
     $term->vid = $this->getTaxonomyIdByName($vocabulary_name);
     $term->parent = 0;
     taxonomy_term_save($term);
 }