Skip to content
Snippets Groups Projects
Commit 8a15b8f4 authored by guypaddock's avatar guypaddock Committed by Megachriz
Browse files

Issue #3091682 by GuyPaddock, MegaChriz: Fixed term autocreate caching when...

Issue #3091682 by GuyPaddock, MegaChriz: Fixed term autocreate caching when multiple term reference fields point to the same vocabulary.
parent b011ee3b
No related branches found
No related tags found
No related merge requests found
......@@ -103,7 +103,10 @@ function taxonomy_feeds_set_target(FeedsSource $source, $entity, $target, array
if (!isset($cache['allowed_vocabularies'][$target])) {
foreach ($info['settings']['allowed_values'] as $tree) {
if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
$cache['allowed_vocabularies'][$target][$vocabulary->vid] = $vocabulary->machine_name;
$vid = $vocabulary->vid;
$cache['allowed_vocabularies'][$target][$vid] = $vocabulary->machine_name;
$cache['vocabulary_targets'][$vid][] = $target;
}
}
}
......@@ -159,9 +162,10 @@ function taxonomy_feeds_set_target(FeedsSource $source, $entity, $target, array
}
}
elseif ($mapping['autocreate'] && strlen($term)) {
$term_vid = key($cache['allowed_vocabularies'][$target]);
$term = (object) array(
'name' => drupal_substr($term, 0, 255),
'vid' => key($cache['allowed_vocabularies'][$target]),
'vid' => $term_vid,
'vocabulary_machine_name' => reset($cache['allowed_vocabularies'][$target]),
);
// Set language if the taxonomy is multilingual.
......@@ -173,8 +177,16 @@ function taxonomy_feeds_set_target(FeedsSource $source, $entity, $target, array
}
taxonomy_term_save($term);
$tid = $term->tid;
// Add to the list of allowed values.
$cache['allowed_values'][$target][$tid] = $term->name;
// Invalidate caches for other fields targeting the same vocabulary.
foreach ($cache['vocabulary_targets'][$term_vid] as $clear_target) {
if ($clear_target !== $target) {
unset($cache['allowed_values'][$clear_target]);
}
}
}
break;
......
"guid","title","tag1","tag2"
"1","Lorem Ipsum 1","Alpha","Beta"
"2","Lorem Ipsum 2","Kiwi","Kiwi"
......@@ -50,8 +50,9 @@ class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase {
);
field_create_field($field);
// Add term reference field to feed item bundle.
$this->instance = array(
// Add a term reference field to the "article" node bundle. Tests use this
// content type as "feed item": tests imports nodes of this type.
$this->article_tags = array(
'field_name' => 'field_tags',
'bundle' => 'article',
'entity_type' => 'node',
......@@ -64,10 +65,12 @@ class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase {
),
),
);
field_create_instance($this->instance);
field_create_instance($this->article_tags);
// Add term reference field to feed node bundle.
$this->instance = array(
// Add a term reference field to the "page" node bundle. Tests use this
// content type as "feed node type": this type is used to attach importers
// to.
$this->page_tags = array(
'field_name' => 'field_tags',
'bundle' => 'page',
'entity_type' => 'node',
......@@ -80,7 +83,7 @@ class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase {
),
),
);
field_create_instance($this->instance);
field_create_instance($this->page_tags);
// Create an importer configuration with basic mapping.
$this->createImporterConfiguration('Syndication', 'syndication');
......@@ -348,6 +351,124 @@ class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase {
$this->assertEqual($term2->tid, $entity->field_tags[LANGUAGE_NONE][0]['tid']);
}
/**
* Tests how term caching works across multiple term reference fields.
*
* This specifically verifies that terms added to a vocabulary by one mapping
* are available for use by other fields that are mapping to the same
* vocabulary.
*
* @see https://www.drupal.org/project/feeds/issues/3091682
*/
public function testAutoCreateUpdatesAllCaches() {
// Create a second term reference field base.
$field = array(
'field_name' => 'field_tags2',
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'tags',
'parent' => 0,
),
),
),
);
field_create_field($field);
// Add a second term reference field instance to to feed node bundle.
$this->article_tags2 = array(
'field_name' => 'field_tags2',
'bundle' => 'article',
'entity_type' => 'node',
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'default' => array(
'type' => 'taxonomy_term_reference_link',
),
),
);
field_create_instance($this->article_tags2);
// Create a CSV importer configuration.
$this->createImporterConfiguration('Node import from CSV', 'node');
$this->setSettings('node', NULL, array(
'content_type' => '',
));
$this->setPlugin('node', 'FeedsFileFetcher');
$this->setPlugin('node', 'FeedsCSVParser');
$this->setSettings('node', 'FeedsNodeProcessor', array(
'bundle' => 'article',
'update_existing' => TRUE,
));
$this->addMappings('node', array(
0 => array(
'source' => 'guid',
'target' => 'guid',
'unique' => TRUE,
),
1 => array(
'source' => 'title',
'target' => 'title',
),
2 => array(
'source' => 'tag1',
'target' => 'field_tags',
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME,
'autocreate' => TRUE,
),
3 => array(
'source' => 'tag2',
'target' => 'field_tags2',
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME,
'autocreate' => TRUE,
),
));
$this->importFile(
'node',
$this->absolutePath() . '/tests/feeds/taxonomy_multiple_tag_fields.csv');
$this->assertText('Created 2 nodes');
$term_names = array(
'Alpha',
'Beta',
'Kiwi',
);
foreach ($term_names as $term_name) {
$loaded_term = taxonomy_get_term_by_name($term_name);
$terms[$term_name] = reset($loaded_term);
}
$expected_node_values = array(
1 => array(
'field_tags' => $terms['Alpha']->tid,
'field_tags2' => $terms['Beta']->tid,
),
2 => array(
'field_tags' => $terms['Kiwi']->tid,
'field_tags2' => $terms['Kiwi']->tid,
),
);
foreach ($expected_node_values as $nid => $node_values) {
$this->drupalGet("node/{$nid}/edit");
foreach ($node_values as $field_name => $field_value) {
$this->assertFieldByName("{$field_name}[und][]", $field_value);
}
}
}
/**
* Tests if terms can be mapped when term access modules are enabled.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment