You are here

function _uc_catalog_navigation in Ubercart 8.4

Same name and namespace in other branches
  1. 5 uc_catalog/uc_catalog.module \_uc_catalog_navigation()
  2. 6.2 uc_catalog/uc_catalog.module \_uc_catalog_navigation()
  3. 7.3 uc_catalog/uc_catalog.module \_uc_catalog_navigation()

Emulates Drupal's menu system, but based around the catalog taxonomy.

Parameters

\Drupal\uc_catalog\TreeNode $branch: A TreeNode object. Determines if the URL points to itself, or possibly one of its children, if present. If the URL points to itself or one of its products, it displays its name and expands to show its children, otherwise displays a link and a count of the products in it. If the URL points to one of its children, it still displays a link and product count, but must still be expanded. Otherwise, it is collapsed and a link.

Return value

array An array whose first element is true if the TreeNode is in hierarchy of the URL path. The second element is the HTML of the list item of itself and its children.

1 call to _uc_catalog_navigation()
theme_uc_catalog_block in uc_catalog/uc_catalog.theme.inc
Themes the catalog block.

File

uc_catalog/uc_catalog.module, line 187
Ubercart Catalog module.

Code

function _uc_catalog_navigation(TreeNode $branch) {
  static $types;
  if (empty($types)) {
    $types = uc_product_types();
  }
  $query = \Drupal::entityQuery('node')
    ->condition('type', $types, 'IN')
    ->condition('status', TRUE)
    ->condition('taxonomy_catalog.target_id', $branch
    ->getTid())
    ->count();
  $num = $query
    ->execute();
  $branch_path = uc_catalog_path($branch);

  // Determine if the URL points to this term.
  $here = Url::fromRoute('<current>')
    ->toString() == $branch_path;

  // Determine whether to expand menu item.
  $inpath = $here;
  $request = \Drupal::request();
  if ($request->attributes
    ->has('node')) {
    $node = $request->attributes
      ->get('node');
    if (isset($node->taxonomy_catalog)) {
      $inpath = FALSE;
      $parents = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term')
        ->loadAllParents($node->taxonomy_catalog->value);
      foreach ($parents as $parent) {
        if ($parent
          ->id() == $branch
          ->getTid()) {
          $inpath = TRUE;
        }
      }
    }
  }
  $lis = [];
  $expand = \Drupal::config('uc_catalog.settings')
    ->get('expand_categories');
  if ($expand || count($branch
    ->getChildren())) {
    foreach ($branch
      ->getChildren() as $twig) {

      // Expand if children are in the menu path. Capture their output.
      list($child_in_path, $lis[], $child_num) = _uc_catalog_navigation($twig);
      $num += $child_num;
      if ($child_in_path) {
        $inpath = $child_in_path;
      }
    }
  }

  // No nodes in category or descendants. Not in path and display nothing.
  if (!$num) {
    return [
      FALSE,
      '',
      0,
    ];
  }

  // Checks to see if node counts are desired in navigation.
  $num_text = '';
  if (\Drupal::config('uc_catalog.settings')
    ->get('block_nodecount')) {
    $num_text = ' (' . $num . ')';
  }
  $link = Link::fromTextAndUrl($branch
    ->getName() . $num_text, Url::fromUri('base:/' . $branch_path))
    ->toString();
  $output = theme_uc_catalog_item([
    'here' => $here,
    'link' => $link,
    'lis' => $lis,
    'expand' => $expand,
    'inpath' => $inpath,
    'count_children' => count($branch
      ->getChildren()),
  ]);

  // Tell parent category your status, and pass on output.
  return [
    $inpath,
    $output,
    $num,
  ];
}