Commit f645ab46 authored by Dries's avatar Dries

- Patch #844410 by Dave Reid: taxonomy parents need to be saved before term hooks are called.

parent 98ef4e3e
......@@ -493,25 +493,24 @@ function taxonomy_term_save($term) {
module_invoke_all('taxonomy_term_presave', $term);
if (empty($term->tid)) {
$op = 'insert';
$status = drupal_write_record('taxonomy_term_data', $term);
field_attach_insert('taxonomy_term', $term);
module_invoke_all('taxonomy_term_insert', $term);
module_invoke_all('entity_insert', $term, 'taxonomy_term');
if (!isset($term->parent)) {
$term->parent = array(0);
}
}
else {
$op = 'update';
$status = drupal_write_record('taxonomy_term_data', $term, 'tid');
field_attach_update('taxonomy_term', $term);
module_invoke_all('taxonomy_term_update', $term);
module_invoke_all('entity_update', $term, 'taxonomy_term');
if (isset($term->parent)) {
db_delete('taxonomy_term_hierarchy')
->condition('tid', $term->tid)
->execute();
}
}
if (isset($term->parent)) {
if (!is_array($term->parent)) {
$term->parent = array($term->parent);
......@@ -536,8 +535,14 @@ function taxonomy_term_save($term) {
}
$query->execute();
}
// Reset the taxonomy term static variables.
taxonomy_terms_static_reset();
// Invoke the taxonomy hooks.
module_invoke_all("taxonomy_term_$op", $term);
module_invoke_all("entity_$op", $term, 'taxonomy_term');
return $status;
}
......@@ -675,6 +680,7 @@ function taxonomy_terms_static_reset() {
drupal_static_reset('taxonomy_term_count_nodes');
drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_parents');
drupal_static_reset('taxonomy_get_parents_all');
drupal_static_reset('taxonomy_get_children');
entity_get_controller('taxonomy_term')->resetCache();
}
......
......@@ -1005,7 +1005,22 @@ class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase {
$edit[$this->instance['field_name'] . '[' . $this->langcode . '][]'] = $term2->tid;
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
// Generate and test sanitized tokens.
// Generate and test sanitized tokens for term1.
$tests = array();
$tests['[term:tid]'] = $term1->tid;
$tests['[term:name]'] = check_plain($term1->name);
$tests['[term:description]'] = check_markup($term1->description, $term1->format);
$tests['[term:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE));
$tests['[term:node-count]'] = 0;
$tests['[term:parent:name]'] = '[term:parent:name]';
$tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name);
foreach ($tests as $input => $expected) {
$output = token_replace($input, array('term' => $term1), array('language' => $language));
$this->assertFalse(strcmp($output, $expected), t('Sanitized taxonomy term token %token replaced.', array('%token' => $input)));
}
// Generate and test sanitized tokens for term2.
$tests = array();
$tests['[term:tid]'] = $term2->tid;
$tests['[term:name]'] = check_plain($term2->name);
......
......@@ -128,9 +128,10 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options =
break;
case 'parent':
$parents = taxonomy_get_parents($term->tid);
$parent = array_pop($parents);
$replacements[$original] = check_plain($parent->name);
if ($parents = taxonomy_get_parents($term->tid)) {
$parent = array_pop($parents);
$replacements[$original] = check_plain($parent->name);
}
break;
}
}
......@@ -140,8 +141,7 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options =
$replacements += token_generate('vocabulary', $vocabulary_tokens, array('vocabulary' => $vocabulary), $options);
}
if ($vocabulary_tokens = token_find_with_prefix($tokens, 'parent')) {
$parents = taxonomy_get_parents($term->tid);
if (($vocabulary_tokens = token_find_with_prefix($tokens, 'parent')) && $parents = taxonomy_get_parents($term->tid)) {
$parent = array_pop($parents);
$replacements += token_generate('term', $vocabulary_tokens, array('term' => $parent), $options);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment