0

I have created vocabulary "Hospital" and created text and image fields like:

  • Hospital Image: (machine name: field_hospital_image) // image field
  • Hospital Contact: (machine name: field_hospital_contact_no_) // text field

My code is:

$vid = 'hospitals';
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
foreach ($terms as $term) {
  $term_data[] = [
    'tid' => $term->tid,
    'tname' => $term->name,
    'image' => ...???..,    // image field
    'contact' => ...???..,  // text field
  ];
}

Please suggest namespace also.

I alsready tried :

'contact' => $term->get('field_hospital_contact_no_')->getValue(),

and

'contact' => $term->get('field_hospital_contact_no_')->value,

Errors:

Error: Call to undefined method stdClass::get()

  • 1
    Possible duplicate of How to access taxonomy term field? – GiorgosK Feb 16 at 9:42
  • Please also clarify what "Please suggest namespace also." means – GiorgosK Feb 16 at 9:42
  • namespace means like - use Drupal\taxonomy\Entity\Term; – A.Azmi Feb 16 at 10:19
  • @GiorgosK - in my case its not working – A.Azmi Feb 16 at 10:20
  • @aazmi Than please change the question to reflect what you have tried perhaps with the errors that you get. – GiorgosK Feb 16 at 10:31
2

Term object is not loaded with loadTree by default a basic php object is loaded instead.

You have to load the term object for each term using $term_obj = Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($term->tid);

Your code should look like this

$vid = 'hospital';
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
foreach ($terms as $term) {
  $term_obj = Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($term->tid);
  $url = "";
  if(isset($term_obj->get('field_hospital_image')->entity)){
    $url = file_create_url($term_obj->get('field_hospital_image')->entity->getFileUri());
  }

  $term_data[] = [
    'tid' => $term->tid,
    'tname' => $term->name,
    'contact' => $term_obj->get('field_hospital_contact_no_')->value,
    'image_url' => $url,
  ];
} 

NOTE: Alternatively you can also use loadTree($vid, 0, NULL, TRUE) to load the entities directly on the $terms array but perhaps it should be avoided on very large vocabularies. More info on the loadTree method

  • Its working for the "field_hospital_contact_no_" please provide solution for image : Hospital Image: (machine name: field_hospital_image). I need image url – A.Azmi Feb 16 at 12:31
  • I tried for image like: file_create_url($term_obj->get('field_hospital_image')->value->entity->getFileUri()) – A.Azmi Feb 16 at 12:41
  • Try without ->value – GiorgosK Feb 16 at 12:48
  • without ->value it should work, revised my answer as well to get image_url – GiorgosK Feb 16 at 13:15
  • GiorgosK - I removed ->value got this error: "Error: Call to a member function getFileUri()". – A.Azmi Feb 18 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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