You are here

function og_vocab_machine_name_validate in OG Vocabulary 7

Element validate; Auto-set a new machine name if vocabulary already exists.

1 string reference to 'og_vocab_machine_name_validate'
og_vocab_form_taxonomy_form_vocabulary_alter in ./og_vocab.module
Implements hook_form_FORM-ID_alter().

File

./og_vocab.module, line 1257
Give each group its own system controlled vocabularies.

Code

function og_vocab_machine_name_validate($element, &$form_state) {
  if (empty($form_state['values']['machine_name'])) {
    return;
  }
  $machine_name = $form_state['values']['machine_name'];

  // Set the static variable to "skip" to prevent altering while we verify
  // the machine name doesn't exist yet.
  $cache =& drupal_static('og_vocab_query_taxonomy_vocabulary_load_multiple_alter', FALSE);
  $cache = 'skip';
  if (!taxonomy_vocabulary_machine_name_load($machine_name)) {
    return;
  }

  // Start iterating over machine names until we hit one that doesn't
  // exist yet.
  $i = 1;
  while (taxonomy_vocabulary_machine_name_load($machine_name)) {
    $machine_name = substr($machine_name, 0, 32 - strlen($i)) . $i;
    ++$i;
  }
  form_set_value($element, $machine_name, $form_state);
}