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.
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.
Hii
ReplyDeletePls help me how to render view or block in custom twig file in drupal 8 ??
Can you create a article for "render view or block in custom twig file in drupal 8" ??
Hi abhishek,
ReplyDeleteSure, I will create new article for render view or block in custom twig file in drupal 8.
Hi Abhishek,
ReplyDeletePlease check the blog for your issue, render block and view in twig template.
https://drupalwebsolutions.blogspot.com/2018/07/programmatically-render-block-in-twig.html
Hii,
ReplyDeletePlease help me with naming file for taxonomy template ?