Showing posts with label term multiple load in drupal 8. Show all posts
Showing posts with label term multiple load in drupal 8. Show all posts

Friday 20 July 2018

Taxonomy terms load in drupal 8.

In drupal 7, we have taxonomy_term_load($tid), taxonomy_term_load_multiple($tids) functions for load the taxonomy term or multiple terms but now in drupal 8 syntax is changed and now Single/Multiple term load by Term::load($tid)
or
Add the name space at the top of the page.
\Drupal\taxonomy\Entity\Term::load($tid);

If we are using Symfony library and calling name space 
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy;

or direct use in function calling time like  \Drupal\taxonomy\Entity\Term::load($tid);

Example:
$term_detail_array = \Drupal\taxonomy\Entity\Term::load($tid);
or
$term_detail_array = Term::load($tid); //if we are already using taxonomy library..

Then get the fields detail by $term_detail_array like,

Term id:  $term_detail_array ->tid->value;
Term name:  $term_detail_array ->name->value;
Term manually added field:  $term_detail_array ->get('field_machine_name')->value;  // manually added field value..
Term image field:  $term_detail_array ->field_machine_name->entity->getFileUri();

 If we have field with entity reference then field value will be in form of $term_detail_array ->get('field_machine_name')->target_id;
or
If we have field without entity reference then field value will be in form of $term_detail_array ->get('field_machine_name')->value;

Now we will discuss on Term::loadMultiple.
And the syntax is $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids); // here $tids is the array of multiple tid..
$terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids); 
foreach ($terms as $term) {  //then the fields value will be..
$term_id = $term->tid->value;
$term_name = $term->name->value;
$term_file field = $term->field_machine_name->uri;
}
and so on..

Note: If have any suggestions or issue regarding 'Taxonomy terms load in drupal 8' then you can ask by comments.

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...