You are here

function fb_connect_translated_menu_link_alter in Drupal for Facebook 7.4

Implements hook_translated_menu_link_alter().

Customize the link on each page, to return the user to this page after connecting. Experimental use of '</a>...<a>' because drupal will surround our title with <a>...</a> and for our markup we don't want that.

File

./fb_connect.module, line 51

Code

function fb_connect_translated_menu_link_alter(&$item) {
  if ($item['link_path'] == 'fb/connect') {
    if (arg(0) == 'admin' && arg(2) == 'menu') {

      // Do not alter on admin menu pages so that admins can modify menu links.
    }
    else {

      // This was an attempt to custom theme menu item.  It doesn't work when menu_navigation_links() is called, so using the </a>...</a> hack below instead.

      //$item['menu_name'] = 'fb_connect';  // $element['#theme'] = 'menu_link__' . strtr($data['link']['menu_name'], '-', '_');

      /* This was an attempt to let javascript show/hide the proper link based on whether the user is currently connected.  The link generated this was does not look very good alongside other drupal menu links.

            $item['title'] = '</a>' . theme('fb_markup', array(
                                              'options' => array('wrapper' => 'span'),
                                              'not_connected' => theme('fb_login_button', array()),
                                              'connected' => '<span class="fb_replace"><!---!name---></span>',
                                            )) . '<a>';
            */
      $img_file = drupal_get_path('module', 'fb') . '/images/f_logo.png';
      $img_src = url($img_file, array(
        'fb_url_alter' => FALSE,
      ));

      // Replace the menu item link with a link customized for the current page.
      if ($token = fb_user_token()) {
        try {
          $me = fb_graph('me', $token);

          // user is already connected.
          $item['href'] = $me['link'];
          $item['title'] = t('<img class="fb_logo" src="!img_src" />&nbsp;%name', array(
            '%name' => $me['name'],
            '!img_src' => $img_src,
          ));
        } catch (Exception $e) {

          // Give user another chance to connect.
          $item['href'] = fb_client_auth_url();
          $item['title'] = t('<img class="fb_logo" src="!img_src" />&nbsp;!title', array(
            '!title' => $item['title'],
            '!img_src' => $img_src,
          ));
        }
      }
      else {
        $item['href'] = fb_client_auth_url();
        if (empty($item['href'])) {

          // Facebook Connect is not configured.
          if (user_access(FB_PERM_ADMINISTER)) {
            $item['title'] = t('<em>Facebook Connect</em>');
            $item['href'] = FB_CONNECT_PATH_ADMIN;
          }
          else {
            $item['hidden'] = TRUE;
          }
        }
        else {
          $item['title'] = t('<img class="fb_logo" src="!img_src" />&nbsp;!title', array(
            '!title' => $item['title'],
            '!img_src' => $img_src,
          ));
        }
      }
    }
    $item['localized_options']['attributes']['class'][] = 'fb';
  }
}