1

I'm trying to get my module to install a vocabulary on enable/install but something about adding terms to it isn't working. any idea why this function isn't working?

function _vocabulary_add_items() {
  $vocabulary = taxonomy_vocabulary_machine_name_load('usertype');
  if($vocabulary === TRUE) {
    $terms = array(
        (object) array(
            'vid' => $vocabulary->vid,
            'name' => t('Economy'),
            'description' => t('Basic user account'),
            'format' => '', // default
            'weight' => 0, // default
            'vocabulary_machine_name' => $vocabulary->machine_name, // optional
        ),
        (object) array(
            'vid' => $vocabulary->vid,
            'name' => t('Deluxe'),
            'description' => t('Deluxe user account'),
            'format' => '', // default
            'weight' => 0, // default
            'vocabulary_machine_name' => $vocabulary->machine_name, // optional            
        ),
        (object) array(
            'vid' => $vocabulary->vid,
            'name' => t('Ultimate'),
            'description' => t('Ultimate user account'),
            'format' => '', // default
            'weight' => 0, // default
            'vocabulary_machine_name' => $vocabulary->machine_name, // optional            
        )
    );
    foreach ($terms as $term) {
        taxonomy_term_save($term);
    }
  }
}

1 Answer 1

2

taxonomy_vocabulary_machine_name_load() returns an object or FALSE. You're checking for exactly TRUE by using ===.

Any of the following should work (sorted by my preference):

if ($vocabulary) {
if (is_object($vocabulary)) {
if ($vocabulary !== FALSE) {
if ($vocabulary == TRUE) { // only two equal signs here
5
  • I've tried each of those options but it seems not to create the terms still.
    – Chad Payne
    Commented Dec 22, 2011 at 18:09
  • Could it be the way i have the terms nested in the array?
    – Chad Payne
    Commented Dec 22, 2011 at 18:29
  • I just tried your code using if ($vocabulary) { and it worked great. hook_install() is only called on first installation. So if you're just disable/enable to test w/o uninstalling first, it won't work. Commented Dec 22, 2011 at 19:17
  • I've been using the devel reinstall option. I believe this uninstalls and then installs.
    – Chad Payne
    Commented Dec 22, 2011 at 20:02
  • Great thanks rocketeerbkw seems to work now. I think it was my conditional statement in a previous function that was preventing it from looping over the results.
    – Chad Payne
    Commented Dec 22, 2011 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.