class MigrateTaxonomyTermReferenceMachineNameFieldHandler in Taxonomy Machine Name 7
Class MigrateTaxonomyTermReferenceMachineNameFieldHandler
Hierarchy
- class \MigrateHandler
Expanded class hierarchy of MigrateTaxonomyTermReferenceMachineNameFieldHandler
1 string reference to 'MigrateTaxonomyTermReferenceMachineNameFieldHandler'
- taxonomy_machine_name_migrate_api in ./
taxonomy_machine_name.module - Implements hook_migrate_api().
File
- ./
taxonomy_machine_name.migrate.inc, line 245 - Taxonomy Machine Name Migrate Module File.
View source
class MigrateTaxonomyTermReferenceMachineNameFieldHandler extends MigrateTaxonomyTermReferenceFieldHandler {
/**
* Implementation of MigrateFieldHandler::fields().
*
* @param string $type
* The field type.
* @param mixed $instance
* Instance info for the field.
* @param Migration $migration
* The migration context for the parent field. We can look at the mappings
* and determine which subfields are relevant.
*
* @return array
* Fields.
*/
public function fields($type, $instance, $migration = NULL) {
$fields = parent::fields($type, $instance, $migration);
$fields['machine_name'] = t('Option: Set to TRUE to use machine_name instead of name to determinate source ID');
return $fields;
}
/**
* Prepare destination field.
*
* @param stdclass $entity
* Entitiy.
* @param array $field_info
* Field info.
* @param array $instance
* Field instance.
* @param array $values
* Values.
*
* @return array
* Result.
*
* @throws \FieldValidationException
*/
public function prepare($entity, array $field_info, array $instance, array $values) {
if (isset($values['arguments'])) {
$arguments = $values['arguments'];
unset($values['arguments']);
}
else {
$arguments = array();
}
if (empty($values[0])) {
$values = array();
}
$tids = array();
if (isset($arguments['source_type']) && $arguments['source_type'] == 'tid') {
// Nothing to do. We have tids already.
$tids = $values;
}
elseif ($values) {
$vocab_name = $field_info['settings']['allowed_values'][0]['vocabulary'];
$names = taxonomy_vocabulary_get_names();
// Get the vocabulary for this term.
if (isset($field_info['settings']['allowed_values'][0]['vid'])) {
$vid = $field_info['settings']['allowed_values'][0]['vid'];
}
else {
$vid = $names[$vocab_name]->vid;
}
// Remove leading and trailing spaces in term names.
$values = array_map('trim', $values);
// Clean machine names if not matching default rule naming.
if (!empty($arguments['machine_name'])) {
$values = array_map('taxonomy_machine_name_clean_name', $values);
}
// Cannot use taxonomy_term_load_multiple() since we have an
// array of names. It wants a singular value. This query may return
// case-insensitive matches.
$field_name = empty($arguments['machine_name']) ? 'name' : 'machine_name';
$existing_terms = db_select('taxonomy_term_data', 'td')
->fields('td', array(
'tid',
$field_name,
))
->condition('td.' . $field_name, $values, 'IN')
->condition('td.vid', $vid)
->execute()
->fetchAllKeyed(1, 0);
// If we're ignoring case, change both the matched term name keys and the
// source values to lowercase.
if (isset($arguments['ignore_case']) && $arguments['ignore_case']) {
$ignore_case = TRUE;
$existing_terms = array_change_key_case($existing_terms);
foreach ($values as $value) {
$lower_values[$value] = strtolower($value);
}
}
else {
$ignore_case = FALSE;
}
foreach ($values as $value) {
if (isset($existing_terms[$value])) {
$tids[] = $existing_terms[$value];
}
elseif ($ignore_case && isset($existing_terms[$lower_values[$value]])) {
$tids[] = $existing_terms[$lower_values[$value]];
}
elseif (!empty($arguments['create_term'])) {
$new_term = new stdClass();
$new_term->vid = $vid;
$new_term->name = $value;
if (!empty($arguments['machine_name'])) {
$new_term->machine_name = $value;
}
$new_term->vocabulary_machine_name = $vocab_name;
// This term is being created with no fields, but we should still call
// field_attach_validate() before saving, as that invokes
// hook_field_attach_validate().
field_attach_validate('taxonomy_term', $new_term);
taxonomy_term_save($new_term);
$tids[] = $new_term->tid;
// Add newly created term to existing array.
$existing_terms[$value] = $new_term->tid;
}
else {
// No term is found for the source value and none is set to be
// created: warn that data has not been imported.
$migration = Migration::currentMigration();
$migration
->saveMessage(t("No matching taxonomy term found for source value '@value' in vocabulary %vocab.", array(
'@value' => $value,
'%vocab' => $names[$vocab_name]->name,
)), MigrationBase::MESSAGE_INFORMATIONAL);
}
}
}
$language = $this
->getFieldLanguage($entity, $field_info, $arguments);
$result = array();
$delta = 0;
foreach ($tids as $tid) {
if (is_array($language)) {
$current_language = $language[$delta];
}
else {
$current_language = $language;
}
$result[$current_language][] = array(
'tid' => $tid,
);
$delta++;
}
return $result;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MigrateFieldHandler:: |
function | Determine the language of the field. | ||
MigrateHandler:: |
protected | property | List of other handler classes which should be invoked before the current one. | |
MigrateHandler:: |
protected | property | List of "types" handled by this handler. Depending on the kind of handler, these may be destination types, field types, etc. | |
MigrateHandler:: |
public | function | ||
MigrateHandler:: |
public | function | ||
MigrateHandler:: |
public | function | Does this handler handle the given type? | 1 |
MigrateHandler:: |
protected | function | Register a list of types handled by this class | |
MigrateTaxonomyTermReferenceFieldHandler:: |
public | function |
Overrides MigrateHandler:: |
|
MigrateTaxonomyTermReferenceMachineNameFieldHandler:: |
public | function |
Implementation of MigrateFieldHandler::fields(). Overrides MigrateTaxonomyTermReferenceFieldHandler:: |
|
MigrateTaxonomyTermReferenceMachineNameFieldHandler:: |
public | function |
Prepare destination field. Overrides MigrateTaxonomyTermReferenceFieldHandler:: |