You are here

function taxonomy_machine_name_clean_name in Taxonomy Machine Name 8

Same name and namespace in other branches
  1. 7 taxonomy_machine_name.module \taxonomy_machine_name_clean_name()

Clean name to generate machine name.

Parameters

string $name: Name to clean.

bool $force: Force new machine name.

Return value

string Cleaned name.

3 calls to taxonomy_machine_name_clean_name()
taxonomy_machine_name_form_taxonomy_term_form_alter in ./taxonomy_machine_name.module
Implements hook_form_FORM_ID_alter().
taxonomy_machine_name_taxonomy_term_presave in ./taxonomy_machine_name.module
Implements hook_ENTITY_TYPE_presave().
taxonomy_machine_name_update_term in ./taxonomy_machine_name.module
Update term with machine name.

File

./taxonomy_machine_name.module, line 208
This is the Taxonomy Machine Name module.

Code

function taxonomy_machine_name_clean_name($name, $force = FALSE) {
  if (!preg_match('/^[a-z0-9\\_]+$/', $name) || $force) {
    $unknown_character = '_';

    // Transliterate and sanitize the destination filename.
    $langcode = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
    $machine_name = \Drupal::transliteration()
      ->transliterate($name, $langcode, $unknown_character);
    $machine_name = trim(mb_strtolower($machine_name));
    $machine_name = trim(preg_replace('/[^a-z0-9\\_]+/', $unknown_character, $machine_name), $unknown_character);
  }
  else {

    // Nothing to do.
    $machine_name = $name;
  }
  \Drupal::moduleHandler()
    ->alter('taxonomy_machine_name_clean_name', $machine_name, $name, $force);
  return $machine_name;
}