User login

Return user to current page: How to link to a form from a Drupal menu

Press login / press account links for anonymous and logged in users, respectively. Subtly added to a bottom menu. Via code in order to allow for the redirect back to the page you were on after login or editing.

There is surely a way, or should be a way, that does not involve hook_page_alter(), but hey, this works.

<?php
/**
 * Implements hook_page_alter().
 */
function feature_final_page_alter(&$page) {
  if ($menu =& $page['footer']['menu_menu-utility']) {
    if ($menu['378']['#attributes']['class'][0] == 'last') {
      unset($menu['378']['#attributes']['class'][0]);
    }
    global $user;
    if (!$user->uid) {
      // If anonymous, set link and link title accordingly.
      // Note:  Path will be handed to l() and thus url().
      $path = 'user';
      $title = t('Press login');
      $tooltip = t('Sign in or register for media-only access to high-resolution images and more.');
    }
    else {
      // The user is logged in, direct them to their account page.
      $path = 'user/' . $user->uid . '/edit';
      $title = t('Press account');
      $tooltip = t('Manage your contact information.');
    }
    $menu['feature_final_press'] = array(
      '#theme' => 'menu_link__menu_utility',
      '#attributes' => array(
        'class' => array(
          0 => 'last',
          1 => 'leaf',
        ),
      ),
      '#title' => $title,
      '#href' => $path,
      '#localized_options' => array(
        'query' => drupal_get_destination(),
        'attributes' => array(
          'title' => $tooltip,
        ),
      ),
      '#below' => array(),
    );
  }
}
?>

Searched words: 
drupal include current destination in link add redirect to menu link

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.