I have been doing some work on a drupal site recently and working heavily with Taxonomy.
I had a need to identify Taxonomy terms based on the person that created them.
The problem with taxonomy terms is that unlike traditional content types, author information isn’t automatically appended when a term is created. Terms are just terms. However in Drupal 7 and beyond they are also “fieldable” entities. I will get to why that is so important in a bit…
There is a module I came across called “Private Taxonomy” which seemed to fit my needs perfectly. Until I found out it was a bit buggy and administratively heavy.
First, private taxonomy adds a whole new permission set. I already have a lot of those as my site is fairly complex and this was adding an unnecessary layer of complexity, hence more work administratively. Second, private taxonomy was buggy. I already had a bunch of regular taxonomy sets and only needed one or two with “private” functionality, or more specifically, I only had a couple of vocabularies for which I needed to tie created terms to authors. Unfortunately the private taxonomy module caused widespread issue across all my vocabularies… I experienced issues where users could no longer see terms from any vocabulary that wasn’t private and assigned to them. Not good…
After some digging I came across a much cleaner solution…
Towards the bottom of that discussion one of the contributors mentions using rules and a new field on his taxonomy term. It took a bit of guesswork on my part, so to ease things for anyone else here is exactly how to proceed…
1. First go to Admin –> Structure –> Taxonomy
2. Then hit “edit vocabulary” for the vocabulary you need to tie author info to.
3. Click the “manage fields” tab in the vocabulary edit window.
4. Add a new field and call it something like “term author” and note the “Machine Name” (ex. field_term_author).
5. Make the “field type” just a simple “text” field.
6. Make sure “plain text” is selected for Text Processing and leave everything else as default and then save it.
— At this point any taxonomy term created in this vocabulary now has a field called “term author.” Our next task is to auto-populate that field any time a term is changed or a new term created in this vocabulary. For this we use “Rules” —
1. Go to Admin –> Configuration –> Workflow –> Rules
2. Click “add new rule”
3. Name your rule something obvious (ex. “autofill Term Author |vocabulary name|”), leave the “tags” blank unless you need it for some reason.
4. Still on the initial rule creation page, from the “React on Event” dropdown select “Taxonomy| After Saving a New Term”
5. Still on the initial rule creation page a new drop down option should have popped up called “Restrict by Vocabulary” –> Select your vocabulary from the list. Click “save”
6. This will take you to a new page with “Events”, “Conditions” and “Actions” – First, if you also want to fill this field in any time an existing taxonomy term is “updated” or “saved” we need to add an additional event. Click “Add event”
7. Under “react on event” dropdown, select “Taxonomy| After updating an existing term” and once again a new dropdown will appear to restrict which vocabulary this applies to. Select yours and then click “add”
8. Next we need to add a condition, click “Add condition” – this brings up a new dialog, from the dropdown select “entity has field” which brings up another dialog.
9. In the data-selector choose “term (created term)” without a “:…” at the end of it. Under “field” select from the “value” drop down the machine-name of the field you created earlier for terms in this vocabulary (ex. field_term_author). Click “save” – Adding this into the conditions section allows us to now take action on this field…
10. Now we have our events we want to react on and the field we want to mess with… time to actually tell drupal what we want to happen. Click “Add Action”
11. Under the “action to add” dropdown, choose “Data| Set a Data Value” which brings up a new dialog.
12. Under “data selector” drop-down, select “Term:… (created term)” –> “term:field-term-author (Term Author)” … note that your’s will look a little different if you named your author field something else.. Click “continue”
13. Finally you need to tell it what value you actually want to set, and in this case we will be using “tokens”. I suggest you click the “Switch to Data Select” button.
14. Using the “Data Selector” dropdown, select “site:…” –> “site:current-user….” –> “site:current-user:uid”. Click “save”
I use the “UID” value which differs from what is explained in the forum discussion above. My reasoning is simply that it is easier to filter in views on the UID and the UID is ALWAYS a UNIQUE value, whereas the actual username on its own is not.
Finally, back on the main rule edit page, click “save changes” and you should be all done. Now go test…
Anytime you create a new term, or update an existing term, your UID (a number) should autofill the “term author” field which you can use to filter on views and elsewhere!
I love this solution. Even though it seems a bit more difficult to setup, it is infinitely easier to manage ongoing and it keeps you from using yet one more module. Less complexity = less bugs and work = happier admin.