A way of hook Drupal pathauto
This is a way, not very nice though, to hook pathauto. It is called when someone view or save the settings of URL alias at admin/build/path/pathauto. The module_invoke_all() is called in the function pathauto_admin_settings(), so this function has to be named "..._pathauto". This is not a ready to use script, even if I have used it on a production site. We had to remove it because we removed Node symlink that had a conflict with translation modules.
<?php
/**
* A nodetype and Node symlinks bulk url_alias
* Make a nodetype symlinks menu items match their url_alias
* Called when user visit and save pathauto:s admin page.
* Couldn't find any better hook or method.
*
**/
function mymodule_pathauto(){
$dummie = array();
$message = "All My node type symlinks was in order";
//If any of My node type symlinks exists
if($symlink_menu_items = db_fetch_array(db_query('SELECT * FROM {menu_links} WHERE link_path LIKE "node/%/mid/%"'))){
//Find missing url_aliases
if($missing_results = db_query('SELECT * FROM {menu_links} LEFT JOIN {url_alias} ON link_path = src WHERE link_path LIKE "node/%/mid/%" AND src IS NULL')){
while($missing_items = db_fetch_array($missing_results)){
$item['menu_name'] = $missing_items['menu_name'];
$item['mlid'] = $missing_items['mlid'];
$link_path_array = explode("/",$missing_items['link_path']);
watchdog("mymodule_path", print_r($missing_items, TRUE)." | ".$item['menu_name']." | ".$item['mlid']);
if(is_numeric($link_path_array[1])){
$node = node_load($link_path_array[1]);
mymodule_symlink_pathalias($item, $node);
$flag = "mymodule node symlinks has now been updated";
}else{
//Node symlinks menu item is an acceptable exception
if($missing_items['link_path'] != "node/%/mid/%" OR $missing_items['link_title'] != "Content Duplicate"){
drupal_set_message("mymodule node symlinks, could not make a correct bulk operation on mlid: ".$missing_items['mlid'], "warning", FALSE);
watchdog("mymodule_symlinks","mymodule node symlinks, could not make a correct bulk operation on mlid: @mlid", array("@mlid", $item['mlid']), WATCHDOG_WARNING);
}
}
}
}
// mymodule_delete_symlik_alias($node->nid);
drupal_set_message($message);
return $dummie;
}else{
drupal_set_message("Maybe there is something wrong with mymodule node symlinks or there path alias?<br>".print_r($symlink_menu_items, TRUE));
return $dummie;
}
}
/**
* Creates an alias to symlink items
*
*
*
**/
function mymodule_symlink_pathalias($item, $node){
watchdog("mymodule_symlink", $node->type. " ".$node->nid. " ".print_r($item, TRUE));
//Use language pattern
if (variable_get('language_content_type_'. $node->type, 0)) {
$pattern = trim(variable_get('pathauto_node_'. $node->type .'_'. $node->language .'_pattern', FALSE));
drupal_set_message("language");
}
//Use type pattern
if (empty($pattern)) {
$pattern = trim(variable_get('pathauto_node_'. $node->type .'_pattern', FALSE));
watchdog("mymodule_pattern", print_r($pattern, TRUE));
//Use default pattern
if (empty($pattern)) {
$pattern = trim(variable_get('pathauto_node_pattern', FALSE));
}
}
// Only do work if there's a pattern
if ($pattern) {
// Only create an alias if the checkbox was not provided or if the checkbox was provided and is checked
if (!isset($node->pathauto_perform_alias) || $node->pathauto_perform_alias) {
$placeholders = pathauto_get_placeholders('node', $node);
//watchdog("bio_placeholder1",print_r($placeholders, true));
$mp_raw_pos;
foreach($placeholders['tokens'] as $key => $value){
if($value == "[menupath-raw]"){
$mp_raw_pos = $key;
}
}
$menu_link = menu_link_load($item['mlid']);
$trail_raw = _menu_titles($menu_link, $node->nid);
//watchdog("testtrail_raw", print_r($trail_raw, true));
$trail_raw_str = implode('/', $trail_raw);
$placeholders['values'][$mp_raw_pos] = pathauto_cleanstring($trail_raw_str, FALSE);
if (variable_get('pathauto_case', 1)) {
foreach($placeholders['values'] as $key => $value){
$placeholders['values'][$key] = drupal_strtolower($value);
}
}
$src = "node/$node->nid/mid/".$item['mlid'];
$pattern = trim(variable_get('pathauto_node_clinical_'. $node->language .'_pattern', ''));
if (empty($pattern)) {
$pattern = trim(variable_get('pathauto_node_'. $node->type .'_pattern', ''));
}
if (empty($pattern)) {
$pattern = trim(variable_get('pathauto_node_pattern', ''));
}
//watchdog("pattern", print_r($pattern, true));
$base_alias = variable_get('duplicate_content_menu_alt_path_'.$item['menu_name'], "");
if($base_alias != "") {
$alias = $base_alias."/".$placeholders['values'][$mp_raw_pos];
//drupal_set_message($item['menu_name']." - ".$src. " - ".$placeholders['values'][$mp_raw_pos]. " - " .$alias);
watchdog("bio_syml_plac", "Menu base alias: ".$item['menu_name']." - src:".$src. " - ph:".$placeholders['values'][$mp_raw_pos]. " - alias:" .$alias);
}else{
$alias = str_replace($placeholders['tokens'], $placeholders['values'], $pattern);
watchdog("bio_syml_plac", "No menu base alias: ".$item['menu_name']."<pre>".print_r($placeholders, true)."</pre>");
}
path_set_alias($src, $alias);
//watchdog("bio_syml_plac",$item['mlid']."<pre>".print_r($placeholders, true)."</pre>");
//drupal_set_message($src. " ".$alias, "status", FALSE);
}
}else{
watchdog("mymodule_symlink", "No Pathauto pattern is set, in function mymodule_symlink_pathalias()");
}
}
/**
* Load all duplicate menulinks from database for given node nid. Returns array.
*
* @param int $nid
* @return array
function mymodule_delete_symlik_alias($nid) {
$result = db_query("DELETE FROM {url_alias} WHERE src LIKE 'node/%d/mid/%%'", $nid);
}
?>
Knowledge keywords: