Created
July 25, 2012 00:14
-
-
Save rob-mcgrail/3173567 to your computer and use it in GitHub Desktop.
Hardcoding taxonomy terms sensibly in D7 module code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @file | |
| * Install, update and uninstall functions for the site feature. | |
| */ | |
| /** | |
| * Implements hook_enable(). | |
| * | |
| * Adds Site status and platform taxonomies and some initial values. | |
| */ | |
| function site_enable() { | |
| // Create document type taxonomy if it doesn't exist. | |
| $existing_taxonomies = taxonomy_vocabulary_get_names(); | |
| if (!array_key_exists('platform', $existing_taxonomies)) { | |
| taxonomy_vocabulary_save((object) array( | |
| 'name' => 'Platform', | |
| 'machine_name' => 'platform', | |
| 'description' => 'Site\'s Content Management System or development framework.', | |
| )); | |
| } | |
| if (!array_key_exists('status', $existing_taxonomies)) { | |
| taxonomy_vocabulary_save((object) array( | |
| 'name' => 'Status', | |
| 'machine_name' => 'status', | |
| 'description' => 'Current status of website.', | |
| )); | |
| } | |
| // Create some taxonomy items if they don't exist. | |
| $vocabs = taxonomy_vocabulary_get_names(); | |
| $vid = $vocabs['platform']->vid; | |
| $terms = array( | |
| 'Drupal', | |
| 'eZPublish', | |
| 'Elgg', | |
| ); | |
| foreach ($terms as $term) { | |
| $existing_term = taxonomy_get_term_by_name($term, 'platform'); | |
| if (empty($existing_term)) { | |
| taxonomy_term_save((object) array('name' => $term, 'vid' => $vid)); | |
| } | |
| } | |
| $vid = $vocabs['status']->vid; | |
| $terms = array( | |
| 'Live', | |
| 'In Development', | |
| 'Archived', | |
| ); | |
| foreach ($terms as $term) { | |
| $existing_term = taxonomy_get_term_by_name($term, 'status'); | |
| if (empty($existing_term)) { | |
| taxonomy_term_save((object) array('name' => $term, 'vid' => $vid)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment