You are here

private function MenuTree::displayMenuItem in Taxonomy Facets 8

When building menu tree we check if we want to display a menu item depending on various user preferences.

Parameters

$term:

$children: s

$tid_selected:

Return value

bool true if to display, false if not to display

File

src/MenuTree.php, line 127

Class

MenuTree

Namespace

Drupal\taxonomy_facets

Code

private function displayMenuItem($term) {
  $display_item = TRUE;
  $filter_applied = FALSE;
  $children = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadTree($this->vid, $term->tid);

  // Get user preferences.
  $do_not_display_if_empty = variable_get('taxonomy_facets_display_link_if_empty', FALSE);
  $do_not_display_if_intersection_empty = variable_get('taxonomy_facets_display_link_if_intersection_empty', FALSE);

  // If user preference is to NOT display link if empty, i.e no nodes underneath.
  if ($do_not_display_if_empty) {

    // check if it has nodes underneath
    $has_nodes = taxonomy_facets_get_subnodes($this->vid, $term->tid);
    if (!$has_nodes) {

      // if no nodes do not display
      $display_item = FALSE;
    }
  }

  // User preference is to NOT display link if selection of filters have
  // no nodes underneath.
  if ($do_not_display_if_intersection_empty) {

    // Check if this item is already used as filter applied, if yes we display
    // item anyhow.
    if ($this->tidSelected == $term->tid) {
      $filter_applied = TRUE;
    }

    // Do this check only if item is last leaf
    // and if no filter applied.
    $applied_filters = $this->filtersObject
      ->getAppliedFilters();
    if ($applied_filters && empty($children) && !$filter_applied) {

      // Remove filter from this vocabulary, if any.
      $new_terms_arr = array();
      foreach ($applied_filters as $t) {
        if ($this->vid != $t->vid) {
          $new_terms_arr[] = $t;
        }
      }

      // Add current item to filters.
      $curr_term = new \stdClass();
      $curr_term->tid = $term->tid;
      $new_terms_arr[] = $curr_term;
      $nodes = taxonomy_facets_get_nodes_based_on_intersect_of_terms($new_terms_arr);
      if (empty($nodes)) {
        $display_item = FALSE;
      }
    }
  }
  return $display_item;
}