I installed taxonomy_menu 4.6.0 and decided there was too much work manually creating URL aliases so I wrote a quick change that is probably not considered Halal but here it is anyway for consideration as an enhancement.

I am happy to work on it to make it "correct". The url_alias update could be made optional through an administration setting.
The code does not contain a way to delete obsolete entries. It could start with a global delete of all taxonomy_menu entries and then rebuild everything. I though that if I left old entries in the table then they would handle people who bookmark old URLs. Perhaps the global delete could be an option through a setting.

First I added function taxonomy_menu_update_url_alias to update {url_alias}. Then I modified taxonomy_menu_menu to pass menu changes to taxonomy_menu_update_url_alias.

function taxonomy_menu_menu($may_cache)
	{
	$items = array();
	$rebuild = false;
	if ($may_cache)
		{
		$access = user_access('access content');
		foreach (taxonomy_get_vocabularies() as $vocabulary)
			{
			if (variable_get('taxonomy_menu_show_'. $vocabulary->vid, 1))
				{
				$path = 'taxonomy_menu/'. $vocabulary->vid;
				$title = t($vocabulary->name);
				$items[] = array('path' => $path, 'title' => $title,
					'callback' => 'taxonomy_menu_page', 'access' => $access,
					'weight' => $vocabulary->weight);
				if(taxonomy_menu_update_url_alias($path, $title))
					{
					$rebuild = true;
					}
				$tree = taxonomy_get_tree($vocabulary->vid);
				$old_depth = -1;
				$old_path = $path;
				$old_title = $title;
				foreach ($tree as $term)
					{
					if ($term->depth <= $old_depth)
						{
						$slashes_to_remove = $old_depth - $term->depth + 1;
						for ($i = 0; $i < $slashes_to_remove; $i++)
							{
							$old_path = substr($old_path, 0, strrpos($old_path, '/'));
							$old_title = substr($old_title, 0, strrpos($old_title, '/'));
							}
						}
					$path = $old_path .'/'. $term->tid;
					$old_depth = $term->depth;
					$old_path = $path;
					$title = t($term->name);
					$items[] = array('path' => $path, 'title' => $title,
						'weight' => $term->weight);
					$title = $old_title .'/'. $title;
					$old_title = $title;
					if(taxonomy_menu_update_url_alias($path, $title))
						{
						$rebuild = true;
						}
					}
				}
			}
		}
	if($rebuild === true)
		{
		drupal_rebuild_path_map();
		}
	return $items;
	}

function taxonomy_menu_update_url_alias($path, $alias)
	{
	$alias = str_replace(' ', '_', strtolower($alias));
	$result = db_query("select pid, dst from {url_alias} where src = '%s'", $path);
	if($result === false)
		{
		return false;
		}
	$url_alias = db_fetch_object($result);
	if($url_alias === false)
		{
		$result = db_query("insert into {url_alias} set dst = '%s', src = '%s'",
			$alias, $path);
		}
	else
		{
		if($alias == $url_alias->dst)
			{
			return false;
			}
		$result = db_query("update {url_alias} set dst = '%s' where pid = %d",
			$alias, $url_alias->pid);
		}
	if($result === false)
		{
		return false;
		}
	return true;
	}

Comments

venkat-rk’s picture

Please correct me if I am wrong, but does taxonomy_menu fail to automatically create alias even when pathauto is used?

I was going to use taxo_menu for my site based on the nice book page on your personal site, but if it doesn't handle automatic url alias, it would be a let down.

Sorry if this sounds like a support request. It isn't meant to be.

peterx’s picture

I did not use Pathauto. I looked at some examples of Pathauto and I decided to not download it. I did not write down why I decided to not use path auto. Pathauto may fit your requirement.

venkat-rk’s picture

Thanks for the response. I will try taxonomy menu with pathauto and report back if it works.

dkruglyak’s picture

Category:feature» bug
Priority:Minor» Normal

I do not think pathauto supports taxonomy_menu right now.

Still, it would not make sense to have taxonomy_menu create url_aliases, when pathauto module already exists and is much more flexible. What we need is for taxonomy_menu to recognize/use the path alias, whenever it exists (see http://drupal.org/node/28724).

Absent that, we get duplicate category pages. One if navigated through taxonomy_menu, another when opening category path. The latter does not show expanded menu.

peterx’s picture

I switched to 4.7.0 RC2 and added pathauto. To make pathauto work with taxonomy_menu I added the following module to pathauto. Perhaps it could be added to pathauto properly.

Note that I changed pathauto_taxonomy_menu and taxonomy_menu_pathauto_bulkupdate but not taxonomy_menu_pathauto. You can generate the aliases in the bulk update. I have not attempted to add the alias update into individual page updates or into the taxonomy_menu update process. Both need more research.

/*
Modified by http://petermoulding.com/web_architect
*/
/*
 * Implementation of hook_pathauto()
 */

function taxonomy_menu_pathauto($op) {
  switch ($op) {
    case 'settings':
      $settings = array();
      $settings['module'] = 'taxonomy_menu';
      $settings['groupheader'] = t('Taxonomy_menu Category path settings');
      $settings['patterndescr'] = t('Default path pattern (applies to all vocabularies with blank patterns below)');
      $settings['patterndefault'] = t('[vocab]/[catpath]');
      $settings['placeholders'] = array(
        t('[vocab]') => t("The vocabulary that the page's first category belongs to."),
        t('[cat]') => t('The name of the category.'),
        t('[catpath]') => t('As [cat], but including its supercategories.'),
        t('[tid]') => t('The id number of the category.')
      );
      // Look for extensions from other modules
      $placeholders = module_invoke_all('pathauto_taxonomy_menu', 'placeholders');
      $settings['placeholders'] = array_merge($settings['placeholders'], $placeholders);

      $settings['supportsfeeds'] = '0/feed';
      $settings['bulkname'] = t('Bulk update category paths');
      $settings['bulkdescr'] = 
        t('Generate aliases for all existing categories which do not already have aliases.');

      $vocabularies = taxonomy_get_vocabularies();
      if (sizeof($vocabularies) > 0) {
        $settings['patternitems'] = array();  
        foreach ($vocabularies as $vocab) {
          $vocabname = $vocab->name;
          $fieldlabel = t('Pattern for all '.$vocabname.' paths');
          $settings['patternitems'][$vocab->vid] = $fieldlabel;
        }
      }
      return (object) $settings;
    default:
      break;
  }
}

/**
 * Implementation of hook_taxonomy().
 */
function pathauto_taxonomy_menu($op, $type, $object = NULL) {
  switch ($type) {
    case 'term':
      switch ($op) {
        case 'insert':
        case 'update':
          /*
          ** Use the category info to automatically create an alias
          */
          $category = (object) $object;
          if ($category->name) {
            $placeholders = array();

            $vid = $category->vid;
            $vocabulary = taxonomy_get_vocabulary($vid);
            $placeholders[t('[vocab]')] = pathauto_cleanstring($vocabulary->name);

            $placeholders[t('[cat]')] = pathauto_cleanstring($category->name);
            $placeholders[t('[tid]')] = $category->tid;

            if ($category->parent) {
              $catpath = pathauto_cleanstring($category->name);
              $parents = taxonomy_get_parents_all($category->parent);
            } else {
              $catpath = '';
              $parents = taxonomy_get_parents_all($category->tid);
            }
            
            foreach ($parents as $parent) {
              $catpath = pathauto_cleanstring($parent->name).'/'.$catpath;
            }
            $placeholders[t('[catpath]')] = $catpath;
            
            // Append any additional extensions
            $extplaceholders = module_invoke_all('pathauto_taxonomy', 'values', $category);
            $placeholders = array_merge($placeholders, $extplaceholders);
            
            if (module_exist('forum')) {
              $forumvid = variable_get('forum_nav_vocabulary', '');
              if ($forumvid == $vid) {
                $src = 'forum/'.$category->tid;
                $forumalias = pathauto_create_alias('taxonomy', $op, $placeholders, 
                  $src, $forumvid);
              }
            }
            $src = 'taxonomy/term/'.$category->tid;
            $alias = pathauto_create_alias('taxonomy', $op, $placeholders, 
              $src, $vid);
          }
          break;
        case 'delete':
          /*
      	  ** If the category is deleted, remove the path aliases
      	  **
          */
          $category = (object) $object;
          path_set_alias('taxonomy/term/'.$category->tid);
          path_set_alias('forum/'.$category->tid);
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
}

// Generate aliases for all categories without aliases
function taxonomy_menu_pathauto_bulkupdate()
	{
	$query = 'SELECT tid,vid,name,src,dst FROM {term_data} '.
	"LEFT JOIN {url_alias} ON CONCAT('taxonomy_menu/', tid) = src ";
	$result = db_query($query);
	$category = db_fetch_object($result);
	
	$count = 0;
	$placeholders = array();
	while ($category)
		{
		$vid = $category->vid;
		$vocabulary = taxonomy_get_vocabulary($vid);
		$placeholders[t('[vocab]')] = pathauto_cleanstring($vocabulary->name);
		$placeholders[t('[cat]')] = pathauto_cleanstring($category->name);
		$placeholders[t('[tid]')] = $category->tid;
		
		if ($category->parent)
			{
			$catpath = $category->name;
			$src = '/' . $category->tid;
			$parents = taxonomy_get_parents_all($category->parent);
			}
		else
			{
			$catpath = '';
			$src = '';
			$parents = taxonomy_get_parents_all($category->tid);
			}
		
		foreach ($parents as $parent)
			{
			$catpath = strtolower(pathauto_cleanstring($parent->name)) . '/' . $catpath;
			$src = '/' . $parent->tid . $src;
			}
		$placeholders['[catpath]'] = $catpath;
		
		$src = 'taxonomy_menu/' . $vid . $src;
		if ($alias = pathauto_create_alias('taxonomy_menu', 'bulkupdate',
			$placeholders, $src, $vid))
			{
			$count++;
			}
		$category = db_fetch_object($result);
		}
	
	drupal_set_message(format_plural($count,
	"Bulk update of terms completed, one alias generated.",
	"Bulk update of terms completed, %count aliases generated."));
	}

Cantalamessa’s picture

Version:4.6.x-1.x-dev» 4.7.x-1.x-dev

Good job, it works fine!

ron_mahon’s picture

Component:Code» Documentation
Priority:Normal» Critical

Can you tell me how you start!
Do you create the vocabulary first?
Do you create the menu before creating the content?
Other wise what is the correct procedure?

I still get doubles in the menu or the URL aliases.
I am using 4.7.3 and have tried all the different Versions CVS and 4.7.3 modules.

Ideally for me it would be better if I could create a separate menu for the Taxonomy.

I have path auto, the addition to it to create URL Alias and taxonomy_menu.
Do I have the right modules or do I need to eliminate the Taxo_menu.

I have been using Drupal for 3 years and have been very pleased. But the most powerful feature (Taxo) is now harder than ever to use.

Appreciate any guidance
Thanks
Ron

steveparks’s picture

Works for me.
I saved the code above as pathauto_taxonomy_menu.inc in modules/pathauto/contrib.

I suggest you submit this to the pathauto maintainer to be added to the project - its very useful.

The only snag is that because pathauto is already allocating aliases to taxonomy items, this then creates them as 'name_1'...any ideas on avoiding this?

Thanks
Steve

peterx’s picture

See Allow manual specification of parent. It is the start of a change to make taxonomy menu work using the URLs from taxonomy, the URLs of the form taxonomy/term/99. I have the change working on a test system and will put it in a production system next week.

If you feel comfortable modifying includes/menu.inc then let me know and I will post the related changes for taxonomy_menu.

sachbearbeiter’s picture

hello

is it possible to do a short summary of all this stuff - i am totally desorientated what to change in which location ... taxonomy_menu is such a nice lightweight solution, but i need this wellformed links ...

thanks a lot and greetings
sb

peterx’s picture

See http://drupal.org/node/41476#comment-181146 for the best description of what I currently use.

petermoulding.com/web_architect

brmassa’s picture

Status:Active» Closed (fixed)