Question

I have taxonomy terms of color and I made a typo in one of the color name. It should be 'Crystal Black Pearl' instead of 'Crsytal Black Pearl'. How do I change it using hook_update_N ? Thank you in advance.

function honda_model_install() {
  $term = \Drupal\taxonomy\Entity\Term::create([
    'vid' => 'honda-color', 
    'name' => 'Crsytal Black Pearl',
  ]); 
  $term->save();
} 
Was it helpful?

Solution

honda_model.install

/**
* Fix typo of term name.
*/
function honda_model_update_8101() {
  $terms = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadByProperties([
        'name' => 'Crsytal Black Pearl',
        'vid' => 'honda-color'
      ]); // load using typo
  if (!empty($terms)) {
    $term = reset($terms);
    $term->setName('Crystal Black Pearl'); // make correction
    $term->save();
  }
}

Make sure you include the block comment, as that's what gets displayed to the user when they go to /update.php

Flush all caches so hook gets pickedup.

PS: once update.php runs it gets recorded in the DB that it ran and it will not run this again. So if you later on need todo another update, you need to add a new hook and increase the number, EX: honda_model_update_8102.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top