Drupal 9 loop term objects to retrieve term data (id, name, uuid)

To loop through term objects and retrieve term data in Drupal 9, you can use the following code snippet:

$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('your_vocabulary_name');

foreach ($terms as $term) {
  $term_id = $term->tid->value;
  $term_name = $term->name->value;
  $term_uuid = $term->uuid->value;

  // Do something with the term data (id, name, uuid).
  // For example, you can print them or store them in an array.
}

In this code, you first load the taxonomy terms using the loadTree() method of the taxonomy_term entity type manager. Replace 'your_vocabulary_name' with the machine name of your vocabulary.

Then, you can iterate through the $terms array using a foreach loop. Inside the loop, you can access the term properties such as tid (term ID), name, and uuid. Assign these values to variables for further processing or use them directly within the loop.

You can perform any desired operations with the term data inside the loop, such as printing the values or storing them in an array for later use.

Remember to adjust the code to fit your specific requirements, including the vocabulary name and any additional processing you need to perform with the term data.