I tried to find a method to verify if the current taxonomy item page has a parent or children, now I get two but I don't know which one is better for the efficiency.
approach I: use taxonomy item controller
$tid = \Drupal::routeMatch()->getRawParameters('taxonomy_term'); $term = taxonomy_term_load($tid); $items = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadChildren($tid,$term->getVocabularyId());
approach II: database query
$db = \Drupal::database(); $query = $db->select('taxonomy_term__parent','ttp')->condition('ttp.parent_target_id',$tid)->fields('ttp',['entity_id']); $result = $query->execute()->fetchAll();
Both method can satisfy my initial demand, but which one is better in efficiency? And what about the general case, when we need to check some data, using the data object method or database query, which is better?