Score:0

Get taxonomy and check if published?

zw flag

I am trying to display taxonomy programitcally. The problem is, all taxonomy is displayed whther published all not. How can I filter only published taxonomy.

I use this code to get taxonomy.

$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
  'vocabulary_name',
  0,
  1, 
  TRUE               
);
 
$results = [];
 
foreach ($tree as $term) {
  $results[] = $term->getName();
}

Can I add a filter like status->1, or status = pubished?

Score:1
in flag

TermStorage::loadTree() is just a fancy way to load term entities. The first mode is a "quick mode" that skims data from taxonomy_term_data. The second mode is just a regular loadMultiple() that loads full entities using the IDs from the quick mode. Since you're interested in status data, you will be loading full entities instead of the quick mode.

Since you already have hold of the term storage service, you can just use loadByProperties(). Filter against the vid, status, and parent properties.

$terms = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadByProperties([
      'vid' => 'vocabulary_name',
      'parent' => 0,
      'status' => 1,
    ])
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.