Drupal 8 - Menu phân loại với phân cấp


7

Tôi đã tìm kiếm trong một thời gian và không thể tìm thấy bất cứ điều gì liên quan đến Drupal 8. Có các Mô-đun như Menu Phân loại nhưng không may cho Drupal 8 hoạt động tương ứng.

Đã có giải pháp nào để xây dựng một menu trong số các thuật ngữ phân loại cho Drupal 8 mà không có sự phát triển riêng lẻ (php, v.v.) chưa?

Hơn nữa, tôi đã nhận ra khi tôi tạo một thuật ngữ phân loại và tạo một tài liệu tham khảo từ một nội dung tôi không có được cái nhìn tổng quan về nội dung được liên kết với thuật ngữ này.

Hy vọng quan điểm của tôi là rõ ràng.


Vẫn chưa có gì, không - D8 vẫn đang trong giai đoạn thử nghiệm, đừng quên
Clive

Menu phân loại hiện có bản phát hành D8.
acrosman

@ Làm cho mọi người ở taxonomy_menu có thể sử dụng một số trợ giúp tốt! :)
Alex

Câu trả lời:


5

Tôi cần điều tương tự. Thật không may là không có gì cho nó vì vậy tôi đã phải tạo ra một cái gì đó cho thời gian. Nó không đẹp nhưng nó hoạt động nên tôi không quan tâm.

/**
 * Returns renderable array of taxonomy terms from Categories vocabulary in
 * hierarchical structure ready to be rendered as html list.
 *
 * @param int $parent
 *   The ID of the parent taxonomy term.
 *
 * @param int $max_depth
 *   The max depth up to which to look up children.
 *
 * @param string $route_name
 *   The name of the route to be used for link generation.
 *   Taxonomy term(ID) will be provided as route parameter.
 *
 * @return array
 */
function mymodule_categories_tree($parent = 0, $max_depth = NULL, $route_name = 'mymodule.category.view') {
  // Load terms
  $tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree('categories', $parent, $max_depth);

  // Make sure there are terms to work with.
  if (empty($tree)) {
    return [];
  }

  // Sort tree by depth so we can easily find out the deepest level
  uasort($tree, function($a, $b) {
    // Change objects to array
    return \Drupal\Component\Utility\SortArray::sortByKeyInt((array) $a, (array) $b, 'depth');
  });

  // Get the value of the deepest term
  $deepest = end($tree);
  $deepest = $deepest->depth;

  // Create a structured array
  $list = [
    $parent => [
      'items' => [],
      'depth' => -1
    ]
  ];
  foreach ($tree AS $term) {
    $list[$term->tid] = (array) $term;
  }

  // See if we're on a node page and if so open the menu
  // on the proper position.
  $node = \Drupal::request()->attributes->get('node');
  if ($node) {
    $categories = $node->get('categories')->getValue();
    // Go through each category to find out the least deep one.
    // That one will be the one we'll open.
    $open_category = $parent;
    foreach ($categories AS $target) {
      $tid = $target['target_id'];
      if ($list[$tid]['depth'] > $list[$open_category]['depth']) {
        $open_category = $tid;
      }
    }
  } else {
    // See if we're on a term page and set the corresponding item
    // as active so we don't have to rely on JS.
    $term = \Drupal::request()->attributes->get('taxonomy_term');
    if ($term) {
      $open_term = $term->id();
    }
  }

  for ($i = $deepest; $i >= 0; $i--) {
    foreach ($list AS $term) {
      if ($term['depth'] == $i) {
        $item = [
          '#type' => 'link',
          '#weight' => $term['weight'],
          '#title' => $term['name'],
          '#url' => Url::fromRoute($route_name, ['taxonomy_term' => $term['tid']]),
          '#options' => [
            'set_active_class' => TRUE
          ]
        ];
        // If we're on a node page and this category was chosen
        // as active, set the link's class.
        if (isset($open_category) && $open_category == $term['tid']) {
          $item['#attributes']['class'][] = 'active';
        }
        // If we're on term page, set the link's class to 'active'
        // and if this item is a parent, open it.
        if (isset($open_term) && $open_term == $term['tid']) {
          $item['#attributes']['class'][] = 'active';
          if (!empty($term['items'])) {
            $item['#wrapper_attributes']['class'][] = 'open';
          }
        }
        // If this item has children
        if (!empty($term['items'])) {
          $item['items'] = $term['items'];
          $item['#wrapper_attributes']['class'][] = 'parent';
          $item['#prefix'] = '<span></span>';
          // If any of the child items has 'active' class,
          // or is also a parent and has 'open' class
          // add the 'open' class to this wrapper too.
          foreach ($item['items'] AS $child) {
            if (
              isset($child['#attributes']['class']) && in_array('active', $child['#attributes']['class'])
              || isset($child['#wrapper_attributes']['class']) && in_array('open', $child['#wrapper_attributes']['class'])
            ) {
              $item['#wrapper_attributes']['class'][] = 'open';
              break;
            }
          }
        }
        foreach ($term['parents'] AS $pid) {
          $list[$pid]['items'][$term['tid']] = $item;
        }
        unset($list[$term['tid']]);
      }
    }
  }

  return [
    '#theme' => 'item_list',
    '#items' => $list[$parent]['items'],
    '#attributes' => [
      'class' => ['categories-tree']
    ],
    '#attached' => [
      'library' => [
        'mymodule/categories_tree'
      ]
    ]
  ];
}

Vì, tôi cần thêm các mô-đun không có sẵn trong drupal 8, tôi đã quyết định sử dụng drupal 7.
sam

1

Tôi đã tạo một phiên bản RẤT thô sơ và đầy đủ các lỗi của taxonomy_menu để thiết lập một trình đơn cha mẹ hiện có cho các điều khoản được thêm vào. Ngoài ra, bạn có thể xác định độ sâu tối đa.

Tôi hy vọng một ngày nào đó sẽ có người xem lại điều này và biến nó thành điều kỳ diệu.


Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.