Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
940 B
1
Indexable
Never
use Drupal\taxonomy\Entity\Term;

/**
 * Update taxonomy terms containing "UNOG DCM" in their titles.
 */
function mymodule_update_taxonomy_terms() {
  // Use EntityQuery to fetch all taxonomy terms.
  $query = \Drupal::entityQuery('taxonomy_term');
  $term_ids = $query->execute();

  if (!empty($term_ids)) {
    // Load and update each term.
    foreach ($term_ids as $term_id) {
      $term = Term::load($term_id);
      if ($term) {
        // Get the current title.
        $current_title = $term->label();
        
        // Check if the title contains "UNOG DCM" (case insensitive).
        if (stripos($current_title, 'UNOG DCM') !== false) {
          // Remove "UNOG DCM" from the title.
          $new_title = str_ireplace('UNOG DCM', '', $current_title);
          
          // Update the title.
          $term->setName($new_title);

          // Save the updated term.
          $term->save();
        }
      }
    }
  }
}