如何通过自定义模块重写Drupal核心Tracker模块的菜单项标题?
Got it, the problem here is that hook_form_alter() is built for modifying form structures—not menu items. That’s why your attempts with it weren’t getting you anywhere! Let’s use the right tool for the job: hook_menu_alter(), which is specifically designed to tweak existing menu definitions from other modules.
Step 1: Create a Custom Module
First, let’s make a simple custom module (we’ll call it custom_tracker_menu for this example):
- Create a folder named
custom_tracker_menuinsidesites/all/modules/custom/(make thecustomfolder if it doesn’t exist). - Add a
.infofile to define the module:
name = Custom Tracker Menu Override description = Changes the title of the "Track" local task on user profiles from the Tracker module. core = 7.x package = Custom
Step 2: Implement hook_menu_alter()
Add a .module file to the same folder with the following code:
<?php /** * Implements hook_menu_alter(). */ function custom_tracker_menu_menu_alter(&$items) { // Make sure the menu item exists before modifying it (avoids errors if Tracker is disabled) if (isset($items['user/%user/track'])) { // Replace this with your desired static title $items['user/%user/track']['title'] = 'Your Custom Title'; // Optional: For a dynamic title (e.g., include the user's name), use these lines instead: // $items['user/%user/track']['title callback'] = 'custom_tracker_menu_dynamic_title'; // $items['user/%user/track']['title arguments'] = array(1); } } /** * Custom title callback for the user track menu item (optional). */ function custom_tracker_menu_dynamic_title($account) { // Returns something like "Track John Doe" return t('Track @username', array('@username' => format_username($account))); }
Step 3: Enable the Module & Clear Cache
- Go to your Drupal site’s Modules page, find your new module, and enable it.
- Critical: Clear Drupal’s cache (you can do this via Configuration > Performance > Clear all caches). Menu changes won’t take effect until you clear the cache!
Key Notes
- We check
isset($items['user/%user/track'])to avoid PHP errors if the Tracker module is ever disabled. - If you need a dynamic title (instead of static text), use the
title callbackandtitle argumentsapproach—it lets you generate the title based on context (like the user profile being viewed). - Make sure the Tracker module is enabled, since we’re modifying a menu item it defines.
内容的提问来源于stack exchange,提问作者Markus
相关产品推荐
相关产品推荐

