Drupal - 设置“user/%/edit/uprofile”到“user/%/edit”上的默认选项卡

发布于 2024-09-13 02:00:30 字数 159 浏览 6 评论 0原文

我正在使用内容配置文件模块。当用户位于查看个人资料页面并按下编辑时,他们希望显示个人资料编辑页面,而不是像现在这样的帐户设置。内容配置文件编辑页面的路径是“user/%/edit/uprofile”。有谁知道如何将“user/%/edit/uprofile”设置为“user/%/edit”的默认选项卡?

I'm using the content profile module. When a user is on their view profile page and press edit, they expect the profile edit page to show, not account settings as it is now. The path to content profile edit page is 'user/%/edit/uprofile'. Does anyone know how to set the 'user/%/edit/uprofile' to default tab for 'user/%/edit'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉生梦死 2024-09-20 02:00:30

编辑 2(针对内容配置文件)

此示例适用于“内容配置文件”选项卡。只需将 $type 更改为配置文件内容类型的短名称即可。这也展示了如何清理代码以供实际使用(我之前的示例非常非常冗长):

function mymodule_menu_alter(&$items) {
  // Specify the content profile type you'd like to work with
  $type = 'profile';

  // Make sure the user has a Content Profile to edit
  if (!empty($items['user/%user_category/edit/' . $type])) {
    // Pull out the menu items we want to modify.
    $account  = &$items['user/%user_category/edit/account'];
    $edit     = &$items['user/%user_category/edit'];
    $profile  = &$items['user/%user_category/edit/' . $type];

    // Specify the Edit Account page as just a regular tab. 
    // You do not need to change this block: this will always be the same as long 
    // as you don't want Edit account to be the default tab.
    $account = array(
      'type' => MENU_LOCAL_TASK,
      'page callback' => $edit['page callback'],
      'page arguments' => $edit['page arguments'],
      'access callback' => $edit['access callback'],
      'access arguments' => $edit['access arguments'],
      'module' => $edit['module'],
      'file' => $edit['file'],
    ) + $account;

    // Change the default action to take when hitting user/<UID>/edit to
    // the content profile
    $edit = array(
      'page callback' => $profile['page callback'],
      'page arguments' => $profile['page arguments'],
      'access callback' => $profile['access callback'],
      'access arguments' => $profile['access arguments'],
      'file' => $profile['file'],
      'file path' => $profile['file path'],
    ) + $edit;

    // Specify the profile page as the default tab and remove settings
    // already set above
    $profile['type'] = MENU_DEFAULT_LOCAL_TASK;
    unset($profile['page callback'], 
      $profile['page arguments'], 
      $profile['access callback'], 
      $profile['access arguments'], 
      $profile['file'], 
      $profile['file path']);
  }
}

编辑 1(对于 Core 的配置文件模块)

我没有意识到您想要更改 Edit 下的默认选项卡。这与我在下面描述的一般原则相同,但有一些细微的修改。此示例将使个人选项卡(位于user//edit/Personal)默认而不是帐户选项卡:

function mymodule_menu_alter(&$items) {
  // Specify the Edit Account page as just a regular tab. 
  // You do not need to change this block: this will always be the same as long 
  // as you don't want Edit account to be the default tab.
  $items['user/%user_category/edit/account']['type'] = MENU_LOCAL_TASK;
  $items['user/%user_category/edit/account']['page callback'] = $items['user/%user_category/edit']['page callback'];
  $items['user/%user_category/edit/account']['page arguments'] = $items['user/%user_category/edit']['page arguments'];
  $items['user/%user_category/edit/account']['access callback'] = $items['user/%user_category/edit']['access callback'];
  $items['user/%user_category/edit/account']['access arguments'] = $items['user/%user_category/edit']['access arguments'];
  $items['user/%user_category/edit/account']['module'] = $items['user/%user_category/edit']['module'];
  $items['user/%user_category/edit/account']['file'] = $items['user/%user_category/edit']['file'];

  // Change default action to take when hitting user/<UID>/edit to
  // the settings of the page you want to use.
  // -- Custom settings start here --
  $items['user/%user_category/edit']['page callback'] = $items['user/%user_category/edit/Personal']['page callback'];
  $items['user/%user_category/edit']['page arguments'] = $items['user/%user_category/edit/Personal']['page arguments'];
  $items['user/%user_category/edit']['access callback'] = $items['user/%user_category/edit/Personal']['access callback'];
  $items['user/%user_category/edit']['access arguments'] = $items['user/%user_category/edit/Personal']['access arguments'];
  $items['user/%user_category/edit']['module'] = $items['user/%user_category/edit/Personal']['module'];
  $items['user/%user_category/edit']['file'] = $items['user/%user_category/edit/Personal']['file'];

  // When loading a profile tab, user_edit needs two parameters. The second parameter is the name of the profile
  // (i.e. Personal from user/<UID>/edit/Personal).
  $items['user/%user_category/edit']['page arguments'] = array(1, 'Personal');

  // Specify the Personal page as the default tab and remove settings
  // already set above */
  $items['user/%user_category/edit/Personal']['type'] = MENU_DEFAULT_LOCAL_TASK;
  unset($items['user/%user_category/edit/Personal']['page callback']);
  unset($items['user/%user_category/edit/Personal']['page arguments']);
  unset($items['user/%user_category/edit/Personal']['access callback']);
  unset($items['user/%user_category/edit/Personal']['access arguments']);
  unset($items['user/%user_category/edit/Personal']['module']);
  unset($items['user/%user_category/edit/Personal']['file']);
}

概述和概念

您可以使用< a href="http://api.drupal.org/api/function/hook_menu_alter/6" rel="nofollow noreferrer">hook_menu_alter 并更改特定选项卡的类型。

更改默认选项卡是一个有点痛苦的过程。基本上,默认选项卡继承页面的所有属性,而无需选择任何选项卡。这允许用户转到user/UID并获取视图页面,而不必直接转到user/UID/view

要更清楚地了解这一点,请查看 user_menu() 钩子实现。请注意 $items['user/%user/view'] 非常空,而 $items['user/%user_uid_optional'] 包含您想要的所有设置'我希望在 $items['user/%user/view'] 下看到。

因此,您将首先将视图选项卡设置为常规选项卡:为此,您必须复制附加到用户/UID 的所有设置菜单项并将它们放入 user/UID/view 菜单项中。

完成此操作后,您将用您想要成为默认选项卡的选项卡的设置替换用户/UID 的设置。

最后,您将取消默认选项卡的所有菜单项,因为它将继承 user/UID 的设置。

查看此代码,该代码使“编辑”选项卡成为默认选项:

function mymodule_menu_alter(&$items) {
  // Specify the View page as just a regular tab. 
  // You do not need to change this block: this will always be the same as long 
  // as you don't want View to be the default tab.
  $items['user/%user/view']['type'] = MENU_LOCAL_TASK;
  $items['user/%user/view']['page callback'] = $items['user/%user_uid_optional']['page callback'];
  $items['user/%user/view']['page arguments'] = $items['user/%user_uid_optional']['page arguments'];
  $items['user/%user/view']['access callback'] = $items['user/%user_uid_optional']['access callback'];
  $items['user/%user/view']['access arguments'] = $items['user/%user_uid_optional']['access arguments'];
  $items['user/%user/view']['file'] = $items['user/%user_uid_optional']['file'];

  // Normal tabs don't have a weight
  unset($items['user/%user/view']['weight']);

  // Change default action to take when hitting user/<UID> to
  // the settings of the page you want to use.
  // -- Custom settings start here --
  $items['user/%user_uid_optional']['page callback'] = $items['user/%user_category/edit']['page callback'];
  $items['user/%user_uid_optional']['page arguments'] = $items['user/%user_category/edit']['page arguments'];
  $items['user/%user_uid_optional']['access callback'] = $items['user/%user_category/edit']['access callback'];
  $items['user/%user_uid_optional']['access arguments'] = $items['user/%user_category/edit']['access arguments'];
  $items['user/%user_uid_optional']['file'] = $items['user/%user_category/edit']['file'];

  // Specify the Edit page as the default tab and remove settings
  // already set above
  $items['user/%user_category/edit']['type'] = MENU_DEFAULT_LOCAL_TASK;
  $items['user/%user_category/edit']['weight'] = -10;
  unset($items['user/%user_category/edit']['page callback']);
  unset($items['user/%user_category/edit']['page arguments']);
  unset($items['user/%user_category/edit']['access callback']);
  unset($items['user/%user_category/edit']['access arguments']);
  unset($items['user/%user_category/edit']['file']);
}

将函数的第二部分替换为菜单项的设置,您应该处于良好状态。当然,请记住在进行任何菜单更改后清除缓存以使它们生效。

Edit 2 (for Content Profile)

This example is for a Content Profile tab. Just change $type to the short name of the content type for the profile. This also shows how to clean up the code for real-world use (my previous examples were really, really verbose):

function mymodule_menu_alter(&$items) {
  // Specify the content profile type you'd like to work with
  $type = 'profile';

  // Make sure the user has a Content Profile to edit
  if (!empty($items['user/%user_category/edit/' . $type])) {
    // Pull out the menu items we want to modify.
    $account  = &$items['user/%user_category/edit/account'];
    $edit     = &$items['user/%user_category/edit'];
    $profile  = &$items['user/%user_category/edit/' . $type];

    // Specify the Edit Account page as just a regular tab. 
    // You do not need to change this block: this will always be the same as long 
    // as you don't want Edit account to be the default tab.
    $account = array(
      'type' => MENU_LOCAL_TASK,
      'page callback' => $edit['page callback'],
      'page arguments' => $edit['page arguments'],
      'access callback' => $edit['access callback'],
      'access arguments' => $edit['access arguments'],
      'module' => $edit['module'],
      'file' => $edit['file'],
    ) + $account;

    // Change the default action to take when hitting user/<UID>/edit to
    // the content profile
    $edit = array(
      'page callback' => $profile['page callback'],
      'page arguments' => $profile['page arguments'],
      'access callback' => $profile['access callback'],
      'access arguments' => $profile['access arguments'],
      'file' => $profile['file'],
      'file path' => $profile['file path'],
    ) + $edit;

    // Specify the profile page as the default tab and remove settings
    // already set above
    $profile['type'] = MENU_DEFAULT_LOCAL_TASK;
    unset($profile['page callback'], 
      $profile['page arguments'], 
      $profile['access callback'], 
      $profile['access arguments'], 
      $profile['file'], 
      $profile['file path']);
  }
}

Edit 1 (for Core's Profile module)

I didn't realize you wanted to change the default tabs under Edit. It's the same general principle as I described below, but with some minor modifications. This example will make the Personal tab (at user/<UID>/edit/Personal) default instead of the account tab:

function mymodule_menu_alter(&$items) {
  // Specify the Edit Account page as just a regular tab. 
  // You do not need to change this block: this will always be the same as long 
  // as you don't want Edit account to be the default tab.
  $items['user/%user_category/edit/account']['type'] = MENU_LOCAL_TASK;
  $items['user/%user_category/edit/account']['page callback'] = $items['user/%user_category/edit']['page callback'];
  $items['user/%user_category/edit/account']['page arguments'] = $items['user/%user_category/edit']['page arguments'];
  $items['user/%user_category/edit/account']['access callback'] = $items['user/%user_category/edit']['access callback'];
  $items['user/%user_category/edit/account']['access arguments'] = $items['user/%user_category/edit']['access arguments'];
  $items['user/%user_category/edit/account']['module'] = $items['user/%user_category/edit']['module'];
  $items['user/%user_category/edit/account']['file'] = $items['user/%user_category/edit']['file'];

  // Change default action to take when hitting user/<UID>/edit to
  // the settings of the page you want to use.
  // -- Custom settings start here --
  $items['user/%user_category/edit']['page callback'] = $items['user/%user_category/edit/Personal']['page callback'];
  $items['user/%user_category/edit']['page arguments'] = $items['user/%user_category/edit/Personal']['page arguments'];
  $items['user/%user_category/edit']['access callback'] = $items['user/%user_category/edit/Personal']['access callback'];
  $items['user/%user_category/edit']['access arguments'] = $items['user/%user_category/edit/Personal']['access arguments'];
  $items['user/%user_category/edit']['module'] = $items['user/%user_category/edit/Personal']['module'];
  $items['user/%user_category/edit']['file'] = $items['user/%user_category/edit/Personal']['file'];

  // When loading a profile tab, user_edit needs two parameters. The second parameter is the name of the profile
  // (i.e. Personal from user/<UID>/edit/Personal).
  $items['user/%user_category/edit']['page arguments'] = array(1, 'Personal');

  // Specify the Personal page as the default tab and remove settings
  // already set above */
  $items['user/%user_category/edit/Personal']['type'] = MENU_DEFAULT_LOCAL_TASK;
  unset($items['user/%user_category/edit/Personal']['page callback']);
  unset($items['user/%user_category/edit/Personal']['page arguments']);
  unset($items['user/%user_category/edit/Personal']['access callback']);
  unset($items['user/%user_category/edit/Personal']['access arguments']);
  unset($items['user/%user_category/edit/Personal']['module']);
  unset($items['user/%user_category/edit/Personal']['file']);
}

Overview and Concept

You can do this with hook_menu_alter and changing the types for specific tabs.

Changing the default tab is a little bit of a harrowing process. Basically, the default tab inherits all the properties of the page without any tabs selected. This allows a user to go to user/UID and get the view page without having to go directly to user/UID/view.

To get a clearer understanding of this, check out the user_menu() hook implementation. Note how $items['user/%user/view'] is pretty empty, and $items['user/%user_uid_optional'] contains all the settings you would've expected to see under $items['user/%user/view'].

So, you're going to first set up the view tab to act as a regular tab: to do this, you're going to have to copy all the settings that are attached to the user/UID menu item and put them into the user/UID/view menu item.

Once you do that, you're going to replace the settings for user/UID with the settings for the tab you want to become the default tab.

Finally, you're going to unset all the menu items for the default tab since it will inherit the settings for user/UID.

Check out this code which makes the Edit tab default:

function mymodule_menu_alter(&$items) {
  // Specify the View page as just a regular tab. 
  // You do not need to change this block: this will always be the same as long 
  // as you don't want View to be the default tab.
  $items['user/%user/view']['type'] = MENU_LOCAL_TASK;
  $items['user/%user/view']['page callback'] = $items['user/%user_uid_optional']['page callback'];
  $items['user/%user/view']['page arguments'] = $items['user/%user_uid_optional']['page arguments'];
  $items['user/%user/view']['access callback'] = $items['user/%user_uid_optional']['access callback'];
  $items['user/%user/view']['access arguments'] = $items['user/%user_uid_optional']['access arguments'];
  $items['user/%user/view']['file'] = $items['user/%user_uid_optional']['file'];

  // Normal tabs don't have a weight
  unset($items['user/%user/view']['weight']);

  // Change default action to take when hitting user/<UID> to
  // the settings of the page you want to use.
  // -- Custom settings start here --
  $items['user/%user_uid_optional']['page callback'] = $items['user/%user_category/edit']['page callback'];
  $items['user/%user_uid_optional']['page arguments'] = $items['user/%user_category/edit']['page arguments'];
  $items['user/%user_uid_optional']['access callback'] = $items['user/%user_category/edit']['access callback'];
  $items['user/%user_uid_optional']['access arguments'] = $items['user/%user_category/edit']['access arguments'];
  $items['user/%user_uid_optional']['file'] = $items['user/%user_category/edit']['file'];

  // Specify the Edit page as the default tab and remove settings
  // already set above
  $items['user/%user_category/edit']['type'] = MENU_DEFAULT_LOCAL_TASK;
  $items['user/%user_category/edit']['weight'] = -10;
  unset($items['user/%user_category/edit']['page callback']);
  unset($items['user/%user_category/edit']['page arguments']);
  unset($items['user/%user_category/edit']['access callback']);
  unset($items['user/%user_category/edit']['access arguments']);
  unset($items['user/%user_category/edit']['file']);
}

Replace the second part of the function with the settings for your menu item and you should be in good shape. Of course, remember to clear the cache after making any menu changes for them to take effect.

夏有森光若流苏 2024-09-20 02:00:30

drupal.org 上有一个模块。
适用于配置文件字段。
请参阅配置文件工具

There is a module on drupal.org.
Works with profile fields.
See Profile Tools.

迷路的信 2024-09-20 02:00:30

以下是在安装了内容配置文件和默认内容类型('profile')的情况下对我有用的代码:

function mymodule_menu_alter(&$items) {
  // Save the data from user/%/edit into user/%/edit/account.
  $items['user/%user_category/edit/account'] = $items['user/%user_category/edit'];
  $items['user/%user_category/edit/account']['tab_parent'] = 'user/%/edit';
  $items['user/%user_category/edit/account']['title'] = 'Account';

  // Change the default user/%/edit to user/%/edit/profile.
  $useredit = &$items['user/%user_category/edit'];
  $userprofile = &$items['user/%user_category/edit/profile'];
  $useredit['page callback'] = $userprofile['page callback'];
  $useredit['page arguments'] = $userprofile['page arguments'];
  $useredit['access callback'] = $userprofile['access callback'];
  $useredit['access arguments'] = $userprofile['access arguments'];
  $useredit['file'] = $userprofile['file'];
  $useredit['file path'] = $userprofile['file path'];

  // Make the user/%/edit/profile sub-tab the default and left-most sub-tab.
  $userprofile['type'] = MENU_DEFAULT_LOCAL_TASK;
  $userprofile['weight'] = -10;
}

更改 $userprofile = $items['user/%user_category/edit/profile'] 以匹配您需要的内容配置文件路径(在您的情况下,这将是 $userprofile = $items['user/%user_category/edit/uprofile']

另外,请确保您的模块在 content_profile 模块之后运行(您的模块必须具有比 -1 更高的权重,即 content_profile.module 的权重)。模块的默认权重为 0,所以您应该没问题。

Here's the code that worked for me with content profile installed and the default content type ('profile'):

function mymodule_menu_alter(&$items) {
  // Save the data from user/%/edit into user/%/edit/account.
  $items['user/%user_category/edit/account'] = $items['user/%user_category/edit'];
  $items['user/%user_category/edit/account']['tab_parent'] = 'user/%/edit';
  $items['user/%user_category/edit/account']['title'] = 'Account';

  // Change the default user/%/edit to user/%/edit/profile.
  $useredit = &$items['user/%user_category/edit'];
  $userprofile = &$items['user/%user_category/edit/profile'];
  $useredit['page callback'] = $userprofile['page callback'];
  $useredit['page arguments'] = $userprofile['page arguments'];
  $useredit['access callback'] = $userprofile['access callback'];
  $useredit['access arguments'] = $userprofile['access arguments'];
  $useredit['file'] = $userprofile['file'];
  $useredit['file path'] = $userprofile['file path'];

  // Make the user/%/edit/profile sub-tab the default and left-most sub-tab.
  $userprofile['type'] = MENU_DEFAULT_LOCAL_TASK;
  $userprofile['weight'] = -10;
}

Change $userprofile = $items['user/%user_category/edit/profile'] to match the content profile path you need (in your case this would be $userprofile = $items['user/%user_category/edit/uprofile']

Also make sure your module runs after the content_profile module (your module must have a 'higher' weight than -1, which is content_profile.module's weight). Modules have a default weight of 0 so you should be fine there.

我正在寻找解决方案,但对如此复杂的情况不满意。我就是这样做的。

function MYTHEME_menu_link(array $variables) {
    global $user;

    if($variables["element"]["#theme"] == "menu_link__user_menu" && $variables["element"]["#original_link"]["router_path"] == "user") {
        $variables["element"]["#href"] = "user/".$user->uid."/edit";
    }

    return theme_menu_link($variables);
}

I was searching for a solution but wasn't satisfied with such complexity. I did it like this.

function MYTHEME_menu_link(array $variables) {
    global $user;

    if($variables["element"]["#theme"] == "menu_link__user_menu" && $variables["element"]["#original_link"]["router_path"] == "user") {
        $variables["element"]["#href"] = "user/".$user->uid."/edit";
    }

    return theme_menu_link($variables);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文