You are here

function taxonomy_machine_name_uniquify in Taxonomy Machine Name 7

Same name and namespace in other branches
  1. 8 taxonomy_machine_name.module \taxonomy_machine_name_uniquify()

Check and alter machine name to generate a unique value.

Parameters

string $machine_name: Machine name to uniquify.

object $term: Taxonomy term of reference.

1 call to taxonomy_machine_name_uniquify()
taxonomy_machine_name_taxonomy_term_presave in ./taxonomy_machine_name.module
Implements hook_taxonomy_term_presave().

File

./taxonomy_machine_name.module, line 201

Code

function taxonomy_machine_name_uniquify(&$machine_name, $term) {
  if (!taxonomy_machine_name_exists($machine_name, $term)) {
    return;
  }

  // If the machine name already exists, generate a new, variant.
  $maxlength = _taxonomy_machine_name_get_schema_machine_name_maxlength();
  $separator = '_';
  $original_machine_name = $machine_name;
  $i = 0;
  do {

    // Append an incrementing numeric suffix until we find a unique value.
    $unique_suffix = $separator . $i;
    $machine_name = truncate_utf8($original_machine_name, $maxlength - drupal_strlen($unique_suffix, TRUE)) . $unique_suffix;
    $i++;
  } while (taxonomy_machine_name_exists($machine_name, $term));
}