/** * Override Migration::prepare(). * * Set the term parent for heirarchical terms * * @param $term * @param $row */ public function prepare($term, $row) { // Handle og_vocab $vocab_name = $this->bundle; if (!($vocabulary = taxonomy_vocabulary_machine_name_load($vocab_name))) { // Create a new vocabulary $vocabulary = (object) array('name' => 'Meter categories for ' . $row->account_id, 'description' => 'Meter categories for ' . $row->account_id, 'machine_name' => $vocab_name); taxonomy_vocabulary_save($vocabulary); } // Create an OG-vocab and relate new vocabulary with OG. $account_id = $term->account_id['destid1']; $settings = array('cardinality' => 1, 'required' => TRUE); // Loop for all meter content-types and create og-vocabulary. $node_types = node_type_get_types(); foreach ($node_types as $content_type) { if (strpos($content_type->type, '_meter') === FALSE) { // Not a meter type, skip. continue; } $og_vocab = og_vocab_create_og_vocab($vocabulary->vid, 'node', $content_type->type, OG_VOCAB_FIELD, $settings); $og_vocab->save(); } og_vocab_relation_save($vocabulary->vid, 'node', $account_id); // Save vocabulary id. $term->vid = $vocabulary->vid; // Handle parent. $term->name = ucwords(trim($term->name)); $parent = ucwords(trim($row->parent)); $parent_term = taxonomy_get_term_by_name($parent, $vocab_name); $parent_term = reset($parent_term); if ($parent_term) { $term->parent = $parent_term->tid; } }
public function synchronize(NodeInterface $node, Context $context, $dirtyAllowed = false) { /* @var $node EntityNode */ $machineName = $node->getBundle(); $object = $node->getValue(); if (!is_array($object)) { $object = []; } if (empty($object['description'])) { $description = ''; } else { $description = $object['description']; } if ($node->isMerge() && ($existing = $this->getExistingObject($node, $context))) { $info = ['machine_name' => $machineName, 'description' => $description] + $object + $existing; } else { $info = ['machine_name' => $machineName, 'description' => $description] + $object + self::$defaults; if ($vocabulary = taxonomy_vocabulary_machine_name_load($machineName)) { // So an existing object is here, but the vid parameter is // actually never loaded (we don't want to export it when we // are building a yml file) - therefore we need to load it // once again. In case we didn't set the vid, the taxonomy // save method will attempt an SQL INSERT and cause bad SQL // STATE errors (constraint violation) $info['vid'] = $vocabulary->vid; } } if (empty($info['name'])) { $context->logWarning(sprintf('%s: has no name', $node->getPath())); $info['name'] = $machineName; } taxonomy_vocabulary_save((object) $info); }
/** * Create vocabulary. * * @param string $name * Name of the taxonomy. * * @Given the vocabulary :name exists * * @When I create a new vocabulary :name */ public function iCreateNewVocabulary($name) { $vocabulary = array('name' => $name, 'machine_name' => $this->transliterate->getMachineName($name), 'description' => '', 'module' => 'taxonomy'); taxonomy_vocabulary_save((object) $vocabulary); // Save the vocabulary for cleaning. $this->vocabularies[] = $name; }
/** * Create a vocabulary. * * @param string $machine_name * Vocabulary machine name. * @param string $name * Vocabulary human readable name. * @param string $description * Vocabulary description. * @param int $hierarchy * If hierarchical or not. * * @return object * Return vocabulary object. */ public function createVocabulary($machine_name, $name, $description = '', $hierarchy = 1) { $vocabulary = new \stdClass(); $vocabulary->name = $name; $vocabulary->machine_name = $machine_name; $vocabulary->description = $description; $vocabulary->hierarchy = $hierarchy; return taxonomy_vocabulary_save($vocabulary); }
public function build() { if ($voc = taxonomy_vocabulary_machine_name_load($this->bundle)) { $voc->name = $this->label; $voc->description = $this->description; } else { $voc = (object) array('machine_name' => $this->bundle, 'name' => $this->label, 'description' => $this->description); } taxonomy_vocabulary_save($voc); }
function _create_vocabs() { $new_vocabs = array('vocab_machine_name_1' => 'Human readable name 1', 'vocab_machine_name_2' => 'Human readable name 2', 'vocab_machine_name_3' => 'Human readable name 3'); foreach ($new_vocabs as $vocab_name => $name) { if (!taxonomy_vocabulary_machine_name_load($vocab_name)) { $vocab = new stdClass(); $vocab->name = $name; $vocab->machine_name = $vocab_name; taxonomy_vocabulary_save($vocab); } //if } //foreach }
/** * Create test vocabulary. */ protected static function createTestVocabulary() { $name = self::randomName(8); $vocabulary = (object) array('name' => $name, 'machine_name' => $name); taxonomy_vocabulary_save($vocabulary); self::$vocabulary = $vocabulary; }
/** * Implements Drupal\configuration\Config\Configuration::saveToActiveStore(). */ public function saveToActiveStore(ConfigIteratorSettings &$settings) { $vocabulary = (object) $this->getData(); if (!empty($vocabulary->vid)) { unset($vocabulary->vid); } $existing = taxonomy_get_vocabularies(); foreach ($existing as $existing_vocab) { if ($existing_vocab->machine_name === $vocabulary->machine_name) { $vocabulary->vid = $existing_vocab->vid; break; } } taxonomy_vocabulary_save($vocabulary); $settings->addInfo('imported', $this->getUniqueId()); }
function createVocabIfNotExist($vocabLabel, $vocabMachineName) { $vid = db_query("SELECT vid FROM taxonomy_vocabulary WHERE machine_name = '{$vocabMachineName}'")->fetchField(); if ($vid !== false) { return $vid; // This vocabulary already exists } $edit = array('name' => $vocabLabel, 'machine_name' => $vocabMachineName, 'description' => t('Use keywords to identify contents.'), 'module' => 'taxonomy'); $vocabulary = (object) $edit; taxonomy_vocabulary_save($vocabulary); return db_query("SELECT vid FROM taxonomy_vocabulary WHERE machine_name = '{$vocabMachineName}'")->fetchField(); }