Index: modules/field_ui/field_ui.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field_ui/field_ui.module,v
retrieving revision 1.26
diff -u -p -r1.26 field_ui.module
--- modules/field_ui/field_ui.module	22 Feb 2010 16:29:34 -0000	1.26
+++ modules/field_ui/field_ui.module	18 Mar 2010 11:50:34 -0000
@@ -71,9 +71,23 @@ function field_ui_menu() {
     if ($info['fieldable']) {
       foreach ($info['bundles'] as $bundle_name => $bundle_info) {
         if (isset($bundle_info['admin'])) {
-          // Extract informations from the bundle description.
+          // Extract information from the bundle.
           $path = $bundle_info['admin']['path'];
-          $bundle_arg = isset($bundle_info['admin']['bundle argument']) ? $bundle_info['admin']['bundle argument'] : $bundle_name;
+          // Different bundles can appear on the same path (e.g. %node_type and
+          // %comment_node_type). To allow field_ui_menu_load() to extract the
+          // actual bundle object from the translated menu router path
+          // arguments, we need to identify the argument position of the bundle
+          // name string ('bundle argument') and pass that position to the menu
+          // loader. The position needs to be casted into a string; otherwise it
+          // would be replaced with the bundle name string.
+          if (isset($bundle_info['admin']['bundle argument'])) {
+            $bundle_arg = $bundle_info['admin']['bundle argument'];
+            $bundle_pos = (string) $bundle_arg;
+          }
+          else {
+            $bundle_arg = $bundle_name;
+            $bundle_pos = '0';
+          }
           $access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array('access callback', 'access arguments')));
           $field_position = count(explode('/', $path)) + 1;
 
@@ -86,33 +100,45 @@ function field_ui_menu() {
             'file' => 'field_ui.admin.inc',
           ) + $access;
           $items["$path/fields/%field_ui_menu"] = array(
+            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
+            'title callback' => 'field_ui_menu_title',
+            'title arguments' => array($field_position, $entity_type, $bundle_arg),
             'page callback' => 'drupal_get_form',
             'page arguments' => array('field_ui_field_edit_form', $entity_type, $bundle_arg, $field_position),
             'type' => MENU_LOCAL_TASK,
             'file' => 'field_ui.admin.inc',
           ) + $access;
           $items["$path/fields/%field_ui_menu/edit"] = array(
+            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
+            'title' => 'Edit',
             'page callback' => 'drupal_get_form',
             'page arguments' => array('field_ui_field_edit_form', $entity_type, $bundle_arg, $field_position),
             'type' => MENU_DEFAULT_LOCAL_TASK,
             'file' => 'field_ui.admin.inc',
           ) + $access;
           $items["$path/fields/%field_ui_menu/field-settings"] = array(
+            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
+            'title' => 'Field settings',
             'page callback' => 'drupal_get_form',
             'page arguments' => array('field_ui_field_settings_form', $entity_type, $bundle_arg, $field_position),
             'type' => MENU_LOCAL_TASK,
             'file' => 'field_ui.admin.inc',
           ) + $access;
           $items["$path/fields/%field_ui_menu/widget-type"] = array(
+            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
+            'title' => 'Widget type',
             'page callback' => 'drupal_get_form',
             'page arguments' => array('field_ui_widget_type_form', $entity_type, $bundle_arg, $field_position),
             'type' => MENU_LOCAL_TASK,
             'file' => 'field_ui.admin.inc',
           ) + $access;
           $items["$path/fields/%field_ui_menu/delete"] = array(
+            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
+            'title' => 'Delete',
             'page callback' => 'drupal_get_form',
             'page arguments' => array('field_ui_field_delete_form', $entity_type, $bundle_arg, $field_position),
             'type' => MENU_LOCAL_TASK,
+            'weight' => 10,
             'file' => 'field_ui.admin.inc',
           ) + $access;
 
@@ -144,15 +170,56 @@ function field_ui_menu() {
 
 /**
  * Menu loader; Load a field based on its name.
- */
-function field_ui_menu_load($field_name) {
+ *
+ * @param $field_name
+ *   The name of the field, as contained in the path.
+ * @param $entity_type
+ *   The name of the entity.
+ * @param $bundle_name
+ *   The name of the bundle, as contained in the path.
+ * @param $bundle_pos
+ *   The position of $bundle_name in $map.
+ * @param $map
+ *   The translated menu router path argument map.
+ */
+function field_ui_menu_load($field_name, $entity_type, $bundle_name, $bundle_pos, $map) {
+  // Extract the actual bundle name from the translated argument map.
+  // The menu router path to manage fields of an entity can be shared among
+  // multiple bundles. For example:
+  // - admin/structure/types/manage/%node_type/fields/%field_ui_menu
+  // - admin/structure/types/manage/%comment_node_type/fields/%field_ui_menu
+  // The menu system will automatically load the correct bundle depening on the
+  // actual path arguments, but this menu loader function only receives the node
+  // type string as $bundle_name, which is not the bundle name for comments.
+  // We therefore leverage the dynamically translated $map provided by the menu
+  // system to retrieve the actual bundle object and bundle name for the current
+  // path.
+  if ($bundle_pos > 0) {
+    $bundle_object = $map[$bundle_pos];
+    $bundle_name = field_extract_bundle($entity_type, $bundle_object);
+  }
+  // Check whether the field exists at all.
   if ($field = field_info_field($field_name)) {
-    return $field;
+    // Only return the field if a field instance exists for the given entity
+    // type and bundle.
+    if ($instance = field_info_instance($entity_type, $field_name, $bundle_name)) {
+      return $field;
+    }
   }
   return FALSE;
 }
 
 /**
+ * Menu title callback.
+ */
+function field_ui_menu_title($field, $entity_type, $bundle_object) {
+  $bundle_name = field_extract_bundle($entity_type, $bundle_object);
+//  dsm(func_get_args());
+  $instance = field_info_instance($entity_type, $field['field_name'], $bundle_name);
+  return $instance['label'];
+}
+
+/**
  * Implements hook_theme().
  */
 function field_ui_theme() {
