I've been trying to create a Taxonomy Menu with Custom Path but I have not found where to set it up.

Is this option available for Drupal 8? How can I achieve it?

Comments

juankvillegas created an issue. See original summary.

gaurav.kapoor’s picture

Hi juankvillegas
There isn't any such option in Taxonomy Menu as of now. You will have to use other site building techniques to achieve that.
Thanks.

robertoperuzzo’s picture

Is there any intention to implement that?

bojanz’s picture

I worked around this by implementing hook_menu_links_discovered_alter().

Here's an example that replaces the default path with a url pointing to a catalog view + facet.

function commerce_demo_menu_links_discovered_alter(&$links) {
  $alias_cleaner = \Drupal::service('pathauto.alias_cleaner');
  $facet_storage = \Drupal::entityTypeManager()->getStorage('facet');
  $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  $view_url = Url::fromRoute('view.product_catalog.page_1', [], ['absolute' => TRUE]);
  $view_url = $view_url->toString();

  foreach ($links as &$link) {
    $menu_name = isset($link['menu_name']) ? $link['menu_name'] : '';
    if ($link['provider'] == 'taxonomy_menu' && $menu_name == 'catalog') {
      // Generate the path to the view + facets.
      // Assumes that the facet is named the same as the vocabulary.
      $term = $term_storage->load($link['metadata']['taxonomy_term_id']);
      /** @var \Drupal\facets\FacetInterface $facet */
      $facet = $facet_storage->load($term->bundle());
      $label = $alias_cleaner->cleanString($term->label());

      $link['url'] = $view_url . '/' . $facet->getUrlAlias() . '/' . $label . '-' . $term->id();
      $link['route_name'] = '';
      $link['route_parameters'] = [];
      $link['load arguments'] = [];
    }
  }
}
bobemoe’s picture

Just spent a while trying to work out how to do this based on "Uses the default taxonomy term path or custom paths" being listed as a feature.

Would be great to see this added. Thanks :) Or at least maybe remove it from the description?

bobemoe’s picture

Thanks @bojanz I did similar:

function taxonomy_menu_fix_menu_links_discovered_alter(&$links) {
  $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  foreach ($links as &$link) {
    $menu_name = isset($link['menu_name']) ? $link['menu_name'] : '';
    if ($link['provider'] == 'taxonomy_menu' && $menu_name == 'main') {
      $term = $term_storage->load($link['metadata']['taxonomy_term_id']);
      $label = strtolower($term->label());

      $link['route_name'] = 'view.category2.page_1';
      $link['route_parameters'] = ['arg_0'=>$label];
      $link['load arguments'] = [];
    }
  }
}

DamienMcKenna’s picture

Category: Support request » Feature request
Issue tags: +Needs tests

That could be a useful feature, but I think we would want test coverage to make sure it worked as expected.