Drupal7: VBO clone taxonomy fields FEB 2016

David G - DrupalOn a live website for a departmental unit at my work, we’ve undergone some changes to the website wherein the major options a Course can be associated need to be changed. Typically the Majors on the site were a single Taxonomy Tree in Drupal and it was available as a field between Courses and Alunni and Faculty. But, once I altered the values of the Majors to the current offerings this quarter — we collectively noticed that Alumni wanted to be referenced by any valid major ever used in the system, not simply the newest majors.

So I wanted to make this available to Alumni by copying all the current values of Majors to a new field for Alumni and a special Vocabulary, which will then be expanded to include all possible majors ever. To do this I used Views Bulk Operations and Rules as detailed below.

The Problem

As I discussed above, historically speaking the department had about 5 majors: Math, Writing, Computer Science, Liberal Arts, etc.

As of late 2015 the majors offered were officially changing to include:

  • Writing would be changed to “Literature and Writing”
  • Computer Science would be changed to “Computing”

The issue is that while all courses and pages should be updated to reflect this new wording. The Alumni from past years want to see on the web pages their majors, as listed when they acquired their degree.

So this isn’t a problem that the website was released with — but rather an administrative problem as the department and majors offered evolve on our campus.

The Solution

To enact this solution I created a new field for Alumni  that is simply called Alumni Major, and mimics the legacy Major field on the content type except that it will reference a special vocabulary we create:

Add a new Alumni Major field to our content type mimicking the existing legacy field.

Add a new Alumni Major field to our content type mimicking the existing legacy field.

Then I created by-hand a new Taxonomy for our Alumnus majors which I then simply copy/pasted the current majors into.

Create a new Alumni Major taxonomy.

Create a new Alumni Major taxonomy.

Add all majors possible (ever) into this taxonomy.

Add all majors possible (ever) into this taxonomy.

Then in order to bulk update all alumni in the system (on the order of hundreds) I wrote some PHP code to use with Views Bulk Operations to update a Node to set the alumni major field to the current major field. This code is desired because there are simply too many alumni to update by hand:

<?php

// Get the old term name.
$legacy_tid = $entity->field_coursedb_discipline[$entity->language][0]['tid'];
$legacy_major = taxonomy_term_load($legacy_tid);
$legacy_major_name = $legacy_major->name;

// Get the new Alumni Major TID from the legacy term name.
$vocabs = taxonomy_vocabulary_get_names();
$alumni_vocab = $vocabs['alumni_majors'];
$result = taxonomy_get_term_by_name($legacy_major_name, $alumni_vocab->machine_name);
$new_alumni_tid = array_shift($result)->tid;

if (isset($new_alumni_tid)) {
// Save the new term to the appropriate new field.
  $entity->field_alumni_major[$entity->language][0]['tid'] = $new_alumni_tid;
  $entity->revision = 1;
  $entity->log = t('Update existing alumni members to add a new field called Alumni Major from the existing coursedb_major field. Alumni can have any major offered in CCS past and present. So the new Alumni Major vocabulary contains those options. -dgurba 01-25-2016');
  node_save($entity);
}
else {
  dpm('new alumni tid not found. legacy was ' . $legacy_major_name . ' for Node title: ' . $entity->title);
}

The Views Bulk Operation form appears as follows:

Admin access to the VBO powered View to bulk update alumni.

Admin access to the VBO powered View to bulk update alumni.

Admin page for the VBO based admin view. By placing the View under /admin it provides some protection from Anonymous user access to the form.

Admin page for the VBO based admin view. By placing the View under /admin it provides some protection from Anonymous user access to the form.

This all makes a relatively big change to the site, rather painless. Of course, the existing View(s) for alumni that were looking at the legacy major field were simply updated to filter or present the major field from the newer Alumni Major field.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *