Or: how can I prevent unpublished nodes' menu items to be removed in the menu select when the user does not have the bypass node access checks permission, but may actually edit various unpublished nodes?
Im facing the problem that in the node form, the menu item options are missing some menu items because the related node is unpublished although the user may actually see and edit those nodes.
In /core/lib/Drupal/Core/Menu/MenuParentFormSelector.php
the function getParentSelectOptions
builds the options list which is then used in the node edit forms.
The function is:
/**
* {@inheritdoc}
*/
public function getParentSelectOptions($id = '', array $menus = NULL, CacheableMetadata &$cacheability = NULL) {
if (!isset($menus)) {
$menus = $this->getMenuOptions();
}
$options = array();
$depth_limit = $this->getParentDepthLimit($id);
foreach ($menus as $menu_name => $menu_title) {
$options[$menu_name . ':'] = '<' . $menu_title . '>';
$parameters = new MenuTreeParameters();
$parameters->setMaxDepth($depth_limit);
$tree = $this->menuLinkTree->load($menu_name, $parameters);
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkNodeAccess'),
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $this->menuLinkTree->transform($tree, $manipulators);
$this->parentSelectOptionsTreeWalk($tree, $menu_name, '--', $options, $id, $depth_limit, $cacheability);
}
return $options;
}
The critical part in that function is the manipulator
'menu.default_tree_manipulators:checkNodeAccess'
.
The manipulator (/core/lib/Drupal/Core/Menu/DefaultMenuLinkTreeManipulators.php
) does the following thing (public function checkNodeAccess
):
if ($this->account->hasPermission('bypass node access')) {
$query->accessCheck(FALSE);
}
else {
$access_result->addCacheContexts(['user.node_grants:view']);
$query->condition('status', NODE_PUBLISHED); // <<< wtf?
}
TL;DR
So it assumes "either a user may bypass all node access checks or hide all unpublished nodes from the menu select" - which is weird...
How can I overwrite this?