term_level.migrate.inc in Term Level Field 7
Term Level Migrate hooks and implementation.
File
term_level.migrate.inc
View source
<?php
function term_level_migrate_api() {
return array(
'api' => 2,
'field handlers' => array(
'MigrateTermLevelFieldHandler',
),
);
}
class MigrateTermLevelFieldHandler extends MigrateFieldHandler {
public function __construct() {
$this
->registerTypes(array(
'term_level',
));
}
public function fields($type) {
return array(
'level' => t("Subfield: The term's level"),
);
}
public function prepare($entity, array $field_info, array $instance, array $values) {
$arguments = array();
if (isset($values['arguments'])) {
$arguments = array_filter($values['arguments']);
unset($values['arguments']);
}
$tids = array();
if (isset($arguments['source_type']) && $arguments['source_type'] == 'tid') {
$tids = $values;
}
elseif ($values) {
$voc = taxonomy_vocabulary_machine_name_load($field_info['settings']['voc']);
$existing_terms = db_select('taxonomy_term_data', 'td')
->fields('td', array(
'tid',
'name',
))
->condition('td.name', $values, 'IN')
->condition('td.vid', $voc->vid)
->execute()
->fetchAllKeyed(1, 0);
if (isset($arguments['ignore_case']) && $arguments['ignore_case']) {
$existing_terms = array_change_key_case($existing_terms);
$values = array_map('strtolower', $values);
}
foreach ($values as $value) {
if (isset($existing_terms[$value])) {
$tids[] = $existing_terms[$value];
}
elseif (!empty($arguments['create_term'])) {
$new_term = new stdClass();
$new_term->vid = $voc->vid;
$new_term->name = $value;
taxonomy_term_save($new_term);
$tids[] = $new_term->tid;
$existing_terms[$value] = $new_term->tid;
}
else {
$migration = Migration::currentMigration();
$migration
->saveMessage(t("No matching taxonomy term found for source value '@value' in vocabulary %vocab.", array(
'@value' => $value,
'%vocab' => $voc->name,
)), MigrationBase::MESSAGE_INFORMATIONAL);
}
}
}
$language = $this
->getFieldLanguage($entity, $field_info, $arguments);
$delta = 0;
foreach ($tids as $tid) {
$return[$language][$delta] = array(
'tid' => $tid,
);
if (isset($arguments['level'])) {
if (is_array($arguments['level'])) {
$return[$language][$delta]['level'] = $arguments['level'][$delta];
}
else {
$return[$language][$delta]['level'] = $arguments['level'];
}
}
$delta++;
}
return isset($return) ? $return : NULL;
}
}