0

Hi I have set URL alias of for node add from path like below :

  1. node/add/my_content_type = addlink

  2. node/add/my_content_type = suggestion

When user is logged-in we make access of node add form via this URL:

www.example.com/addlink

Now for not logged-in user we want users to use node add from via below URL:

www.example.com/suggestion

I am trying with rules on event "Initialize drupal" then checking path and roles and trying to redirect but not able to achieve that.

Please suggest is that possible to make it happen in some way!

Thanks!

  • Can I ask why it matters to have two URLs that go to the same thing? Is it the same content type? – Kevin Sep 22 '16 at 20:12
  • Yes it is same content type "node/add/link" but for logged in users /link and for not logged in it should be /suggestions as this is requirement because not logged in users can submit only unpublished nodes so those are suggestions and logged in users can add nodes and publish them! – jas Sep 22 '16 at 20:15
  • Why not create two menu items with hook_menu and simply set the user access permission? Then you can add that link to any menu, and users will either see it not based on their access permissions (similar to Login/Logout/My Account). Would be easier to me than rewriting and aliasing URLs. – Kevin Sep 22 '16 at 20:39
  • @Kevin thanks for suggestion, I am going to try that !! – jas Sep 22 '16 at 20:42
  • By default any menu link or link created with the l() function will check access. So while you will have two separate items, users would never know it. – Kevin Sep 22 '16 at 20:48
3

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.

  • Thank you for answer as I can see it start changing path according to user is logged in or not. Thanks for explanations and code. But It created problem ie: node/add/ from page is not loading it says "redirected you too many times." please sugggest ! – jas Sep 22 '16 at 19:24
  • Check the value of current_path(), after redirection it should match the $expected_path. Otherwise you end up with a infinite redirection loop. – Pierre Buyle Sep 22 '16 at 19:26
  • current path is "node/add/link" but expected path says "addlink" or "suggestion" as we set Path alias for them. – jas Sep 22 '16 at 19:28
  • Please suggest as per our paths current_path and expected path are not equal due to which it should enter in loop but drupal_goto($expected_path) is not working but its entering infinite loop instead. sorry to bother you for asking more details. – jas Sep 22 '16 at 19:34
  • we are using 2 languages but those are based on domain so no language prefix is used! but we are using entity translation. – jas Sep 22 '16 at 19:56
0

Finally I am able to solve this by using theme_menu_link:

function theme_menu_link__main_menu(array $variables) {



  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#original_link']['mlid'] == "610") {

       if ($element['#original_link']['mlid'] == "610") {

          $path = user_is_anonymous() ? 'suggestion' : 'addlink';
          $element['#original_link']['link_path'] = $path;
          $element['#href'] = $path;
     }
  }

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '' . $output . $sub_menu . "\n";

}
  • 1
    Just note that this only works when the link is in the main menu. If you use the link in other menus, or in content (like body text) it won't do what you want. Hence the hook_menu suggestion. You can also remove global $user here and just use user_is_anonymous() api.drupal.org/api/drupal/modules%21user%21user.module/function/… – Kevin Sep 23 '16 at 22:42
  • 1
    Also, if someone ever mistakenly deletes the menu link and re-adds it, this code will no longer work. – Kevin Sep 23 '16 at 22:43
  • Thanks those are valid points and I have removed global user and will try with suggestion!! – jas Sep 24 '16 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.