digitalist.global // March 27 2017

Updating taxonomy term name in Drupal 8

Here you can read how to use hook update for updating taxonomy terms in Drupal 8.

The hook update is added in the .install file like mymodule_update_800x

function mymodule_update_8001(&$sandbox) {
  $query = \Drupal::entityQuery('taxonomy_term');
  $query->condition('vid', 'county');
  $result = $query->execute();

  foreach($result as $term_id) {
    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($term_id);
    $term_name = $term->getName();
    // Transforming taxonomy term value. Doing my stuff.
    $value = $term_name . " län";
    $term->setName($value);
    $term->save(); // Important!!!!
  }
}

More from digitalist.global