You are here

function taxonomy_machine_name_taxonomy_term_presave in Taxonomy Machine Name 8

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

Implements hook_ENTITY_TYPE_presave().

File

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

Code

function taxonomy_machine_name_taxonomy_term_presave(EntityInterface $term) {

  // Set default value based on current name term.

  /** @var \Drupal\taxonomy\Entity\Term $term */
  if ($term
    ->get('machine_name')
    ->isEmpty()) {
    $machine_name = $term
      ->get('name')
      ->first()
      ->getValue()['value'];
  }
  else {

    // Clean by security.
    $machine_name = $term
      ->get('machine_name')
      ->first()
      ->getValue()['value'];
  }
  $machine_name = taxonomy_machine_name_clean_name($machine_name);

  // If the alias already exists, generate a new,
  // hopefully unique, variant.
  taxonomy_machine_name_uniquify($machine_name, $term);
  $term
    ->set('machine_name', $machine_name);
}