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
Comment #1
venkat-rk CreditAttribution: venkat-rk commentedPlease 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.
Comment #2
peterx CreditAttribution: peterx commentedI 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.
Comment #3
venkat-rk CreditAttribution: venkat-rk commentedThanks for the response. I will try taxonomy menu with pathauto and report back if it works.
Comment #4
dkruglyak CreditAttribution: dkruglyak commentedI 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.
Comment #5
peterx CreditAttribution: peterx commentedI 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.
Comment #6
Cantalamessa CreditAttribution: Cantalamessa commentedGood job, it works fine!
Comment #7
ron_mahon CreditAttribution: ron_mahon commentedCan 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
Comment #8
steveparks CreditAttribution: steveparks commentedWorks 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
Comment #9
peterx CreditAttribution: peterx commentedSee 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.
Comment #10
sachbearbeiter CreditAttribution: sachbearbeiter commentedhello
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
Comment #11
peterx CreditAttribution: peterx commentedSee http://drupal.org/node/41476#comment-181146 for the best description of what I currently use.
petermoulding.com/web_architect
Comment #12
brmassa CreditAttribution: brmassa commented