The ensure the right path is used when displaying links, you need to implement hook_url_outbound_alter()
in a custom module.
/**
* Implement hook_url_outbound_alter().
*/
function MODULE_url_outbound_alter(&$path, &$options, $original_path) {
if ($path == 'node/add/my_content_type') {
$path = user_is_logged_in() ? 'addlink' : 'suggestion';
}
}
Ensuring the right path is always used can be achieved with a hook_menu_alter(). There, you can alter the page callback for node/add/my_content_type
. And in your callback, check whether the user is accessing the page using the expected path.
/**
* Implements hook_menu_alter().
*/
function MODULE_hook_menu_alter(&$items) {
$type_url_str = str_replace('_', '-', 'my_content_type');
$items[['node/add/' . $type_url_str]['page callback'] = 'MODULE_my_content_type_add';
}
/**
* Page callback for the add new My Content node page.
*
* @see MODULE_hook_menu_alter
*/
function MODULE_my_content_type_add($type) {
$expected_path = user_is_logged_in() ? 'addlink' : 'suggestion';
if (current_path() != $expected_path) {
drupal_goto($expected_path);
}
else {
return node_add($type);
}
}
If you site use multiple language with prefix language selector, then current_path()
will also contains the language prefix whch need to be accounted for in your code.