1

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();
} 
4
  • If an editor makes a typo in their article title, are you gonna hook_update_N that too?
    – No Sssweat
    Commented Oct 21, 2020 at 8:06
  • well I include the Taxonomy Term in the install file, and I couldn't change it after reinstall the module, so I guess hook_update_N is the answer. Can you help me @NoSssweat ? Commented Oct 21, 2020 at 8:08
  • Ah some much needed context. Show us the code in your install file where the typo is.
    – No Sssweat
    Commented Oct 21, 2020 at 8:12
  • function honda_model_install() {$term = \Drupal\taxonomy\Entity\Term::create([ 'vid' => 'honda-color', 'name' => 'Crystal Black Pearl', ]); $term->save();} I already changed it here but it already written 'Crsytal Black Pearl' in the database Commented Oct 21, 2020 at 8:16

1 Answer 1

4

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.