{"record":{"id":"ea72dfd201fb72b2","repo":"phacility/phabricator","slug":"no-such-key-s-to-add-menu-item-after","errorCode":null,"errorMessage":"No such key '%s' to add menu item after!","messagePattern":"No such key '(.+?)' to add menu item after!","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/view/phui/PHUIListView.php","lineNumber":68,"sourceCode":"      ->setHref($href);\n\n    $this->addMenuItem($item);\n\n    return $item;\n  }\n\n  public function addMenuItem(PHUIListItemView $item) {\n    return $this->addMenuItemAfter(null, $item);\n  }\n\n  public function addMenuItemAfter($key, PHUIListItemView $item) {\n    if ($key === null) {\n      $this->items[] = $item;\n      return $this;\n    }\n\n    if (!$this->getItem($key)) {\n      throw new Exception(pht(\"No such key '%s' to add menu item after!\",\n        $key));\n    }\n\n    $result = array();\n    foreach ($this->items as $other) {\n      $result[] = $other;\n      if ($other->getKey() == $key) {\n        $result[] = $item;\n      }\n    }\n\n    $this->items = $result;\n    return $this;\n  }\n\n  public function addMenuItemBefore($key, PHUIListItemView $item) {\n    if ($key === null) {\n      array_unshift($this->items, $item);","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/phacility/phabricator/blob/5720a38cfe95b00ca4be5016dd0d2f3195f4fa04/src/view/phui/PHUIListView.php#L50-L86","documentation":"PHUIListView::addMenuItemAfter($key, $item) inserts $item directly after the existing item whose key equals $key (keys are set with PHUIListItemView::setKey()). Before inserting, the list resolves the anchor via getItem($key), which scans its items, and throws when no item carries that key — a typo, a name-vs-key mixup, or an anchor that is added later all fail.","triggerScenarios":"addMenuItemAfter('home', $item) when no item was created with setKey('home'); referencing an item by display name instead of its key; adding the anchor item after the insert call.","commonSituations":"Dynamically built menus where anchor items are conditionally omitted; copy-paste from a menu with different keys; refactors that rename keys but not every reference.","solutions":["Verify the anchor exists with $list->getItem($key) and fix any typo or missing setKey() call.","When the anchor is optional, branch: if getItem($key) is truthy use addMenuItemAfter(), otherwise addMenuItem() to append.","Centralize menu keys as class constants so set and lookup sites cannot drift."],"exampleFix":"// before\n$list->addMenuItemAfter('revisions', $item); // no item keyed 'revisions'\n\n// after\n$anchor = id(new PHUIListItemView())\n  ->setKey('revisions')\n  ->setName(pht('Revisions'))\n  ->setHref('/differential/');\n$list->addMenuItem($anchor);\n$list->addMenuItemAfter('revisions', $item);","handlingStrategy":"validation","validationCode":"if ($list->getItem($key)) {\n  $list->addMenuItemAfter($key, $item);\n} else {\n  $list->addMenuItem($item); // no anchor: append at the end\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["getItem($key) is public — always probe before positional inserts.","Centralize menu keys as class constants.","Anchors must be added before items that reference them."],"tags":["php","phabricator","menu","keys","list-view"],"backgroundTag":"unknown-menu-key","analyzedSha":"5720a38cfe95b00ca4be5016dd0d2f3195f4fa04","analyzedAt":"2026-08-21T05:07:25.672Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}