Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save orakili/3703789 to your computer and use it in GitHub Desktop.
Save orakili/3703789 to your computer and use it in GitHub Desktop.
Drupal 7.15 - Taxonomy patch combining #1343822 (autocreate) and #962664 (unpublished nodes) and #995156 (permission vocabulary machine_name)
diff --git a/modules/taxonomy/taxonomy.admin.inc b/modules/taxonomy/taxonomy.admin.inc
index 828fde0..6f72f4d 100644
--- a/modules/taxonomy/taxonomy.admin.inc
+++ b/modules/taxonomy/taxonomy.admin.inc
@@ -768,7 +768,7 @@ function taxonomy_form_term($form, &$form_state, $edit = array(), $vocabulary =
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
- '#access' => user_access("delete terms in $vocabulary->vid") || user_access('administer taxonomy'),
+ '#access' => user_access("delete terms in $vocabulary->machine_name") || user_access('administer taxonomy'),
'#weight' => 10,
);
}
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index d501282..db67033 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -72,12 +72,12 @@ function taxonomy_permission() {
);
foreach (taxonomy_get_vocabularies() as $vocabulary) {
$permissions += array(
- 'edit terms in ' . $vocabulary->vid => array(
+ 'edit terms in ' . $vocabulary->machine_name => array(
'title' => t('Edit terms in %vocabulary', array('%vocabulary' => $vocabulary->name)),
),
);
$permissions += array(
- 'delete terms in ' . $vocabulary->vid => array(
+ 'delete terms in ' . $vocabulary->machine_name => array(
'title' => t('Delete terms from %vocabulary', array('%vocabulary' => $vocabulary->name)),
),
);
@@ -371,7 +371,14 @@ function taxonomy_admin_paths() {
* Return edit access for a given term.
*/
function taxonomy_term_edit_access($term) {
- return user_access("edit terms in $term->vid") || user_access('administer taxonomy');
+ if (user_access('administer taxonomy')) {
+ return TRUE;
+ }
+ if (!isset($term->vocabulary_machine_name)) {
+ $vocabulary = taxonomy_vocabulary_load($term->vid);
+ $term->vocabulary_machine_name = $vocabulary->machine_name;
+ }
+ return user_access("edit terms in $term->vocabulary_machine_name");
}
/**
@@ -1780,6 +1787,15 @@ function taxonomy_rdf_mapping() {
function taxonomy_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $item) {
if ($item['tid'] == 'autocreate') {
+ // Avoid duplicating tags within the same vocabulary.
+ $tid = db_query_range("SELECT tid FROM {taxonomy_term_data} WHERE name = :name AND vid = :vid", 0, 1, array(
+ ':name' => trim($item['name']),
+ ':vid' => $item['vid'],
+ ))->fetchField();
+ if (!empty($tid)) {
+ $items[$delta]['tid'] = $tid;
+ continue;
+ }
$term = (object) $item;
unset($term->tid);
taxonomy_term_save($term);
@@ -1822,7 +1838,7 @@ function taxonomy_build_node_index($node) {
}
}
// We only maintain the taxonomy index for published nodes.
- if ($status) {
+ if ($status || variable_get('taxonomy_index_unpublished', FALSE)) {
// Collect a unique list of all the term IDs from all node fields.
$tid_all = array();
foreach (field_info_instances('node', $node->type) as $instance) {
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index 32ae84d..b3fb667 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -1298,6 +1298,124 @@ class TaxonomyTermIndexTestCase extends TaxonomyWebTestCase {
}
/**
+ * Tests term autocreation with multiple autocomplete widgets.
+ */
+class TaxonomyTermAutocreateTestCase extends TaxonomyWebTestCase {
+
+ public static function getInfo() {
+ return array(
+ 'name' => 'Taxonomy term autocreation',
+ 'description' => 'Tests term autocreation with multiple autocomplete widgets.',
+ 'group' => 'Taxonomy',
+ );
+ }
+
+ function setUp() {
+ parent::setUp('taxonomy');
+ // Create an administrative user.
+ $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
+ $this->drupalLogin($this->admin_user);
+
+ // Create a vocabulary and add two autocomplete fields on article nodes.
+ $this->vocabulary = $this->createVocabulary();
+
+ $this->field_name_1 = drupal_strtolower($this->randomName());
+ $this->field_1 = array(
+ 'field_name' => $this->field_name_1,
+ 'type' => 'taxonomy_term_reference',
+ 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+ 'settings' => array(
+ 'allowed_values' => array(
+ array(
+ 'vocabulary' => $this->vocabulary->machine_name,
+ 'parent' => 0,
+ ),
+ ),
+ ),
+ );
+ field_create_field($this->field_1);
+ $this->instance_1 = array(
+ 'field_name' => $this->field_name_1,
+ 'bundle' => 'article',
+ 'entity_type' => 'node',
+ 'widget' => array(
+ 'type' => 'taxonomy_autocomplete',
+ ),
+ 'display' => array(
+ 'default' => array(
+ 'type' => 'taxonomy_term_reference_link',
+ ),
+ ),
+ );
+ field_create_instance($this->instance_1);
+
+ $this->field_name_2 = drupal_strtolower($this->randomName());
+ $this->field_2 = array(
+ 'field_name' => $this->field_name_2,
+ 'type' => 'taxonomy_term_reference',
+ 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+ 'settings' => array(
+ 'allowed_values' => array(
+ array(
+ 'vocabulary' => $this->vocabulary->machine_name,
+ 'parent' => 0,
+ ),
+ ),
+ ),
+ );
+ field_create_field($this->field_2);
+ $this->instance_2 = array(
+ 'field_name' => $this->field_name_2,
+ 'bundle' => 'article',
+ 'entity_type' => 'node',
+ 'widget' => array(
+ 'type' => 'taxonomy_autocomplete',
+ ),
+ 'display' => array(
+ 'default' => array(
+ 'type' => 'taxonomy_term_reference_link',
+ ),
+ ),
+ );
+ field_create_instance($this->instance_2);
+ }
+
+ /**
+ * Tests adding the same term to multiple autocomplete fields.
+ */
+ function testTaxonomyTagging() {
+ $term1_name = $this->randomName();
+
+ // Post an article.
+ $edit = array();
+ $langcode = LANGUAGE_NONE;
+ $edit["title"] = $this->randomName();
+ $edit["body[$langcode][0][value]"] = $this->randomName();
+ $edit["{$this->field_name_1}[$langcode]"] = $term1_name;
+ $this->drupalPost('node/add/article', $edit, t('Save'));
+
+ // Check that the term is saved and can be retrieved.
+ $terms = taxonomy_get_term_by_name($term1_name);
+ $this->assertEqual(1, count($terms), t('The term was saved.'));
+
+ // Add a new term to two different fields.
+ $node = $this->drupalGetNodeByTitle($edit["title"]);
+ $term2_name = $this->randomName();
+ $edit["{$this->field_name_1}[$langcode]"] = $term2_name;
+ $edit["{$this->field_name_2}[$langcode]"] = $term2_name;
+ $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
+
+ // Check that the term is displayed multiple times.
+ $this->assertText($term2_name, t('The term was saved and appears on the node page.'));
+ $this->assertNoUniqueText($term2_name, t('The term displays multiple times.'));
+
+ // Check that the term is saved only once.
+ $terms = taxonomy_get_term_by_name($term2_name);
+ $this->assertEqual(1, count($terms), t('The term was saved only once.'));
+ }
+}
+
+/**
* Test the taxonomy_term_load_multiple() function.
*/
class TaxonomyLoadMultipleTestCase extends TaxonomyWebTestCase {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment