Solution 1
Add a custom access callback to your paths, that will then determine whether or not the user should have access to a path.
Important: the code below will deny access to those paths. If you just wanted to show a slightly different menu and not block the access to those pages, see solution #2.
For example:
function YOUR_MODULE_menu_alter(&$items) {
$items['your_path']['access callback'] = 'YOUR_MODULE_menu_access_callback';
}
function YOUR_MODULE_menu_access_callback(){
global $user;
if ($user->uid) {
// Load user's Profile2 profile.
$profile = profile2_by_uid_load($user->uid, 'NAME_OF_YOUR_PROFILE');
// Allow access only if the user selected the checkbox.
if (isset($profile->field_YOUR_FIELD[LANGUAGE_NONE][0]['value']) && $profile->field_YOUR_FIELD[LANGUAGE_NONE][0]['value']) {
return TRUE;
}
}
// This assumes that anonymous users should not have the access to a
// particular menu.
return FALSE;
}
This code would go in a custom module.
Solution 2
Add a simple theme_menu_link()
override in your front-end theme, and skip rendering of those links.
Important: this will not prevent the access to those pages, it will just skip rendering of those list items. Also, you must be rendering the links using a menu, e.g. place a menu block somewhere in your theme, and not render the menu directly (e.g. theme('links__system_main_menu', ...)
or theme('links__system_secondary_menu), ...)
).
For example:
function YOUR_THEME_menu_link($variables) {
global $user;
$element = $variables ['element'];
$sub_menu = '';
// Exclude certain paths.
if ($user->uid) {
// Load user's Profile2 profile.
$profile = profile2_by_uid_load($user->uid, 'NAME_OF_YOUR_PROFILE');
// Allow access only if the user selected the checkbox.
if (!isset($profile->field_YOUR_FIELD[LANGUAGE_NONE][0]['value']) || !$profile->field_YOUR_FIELD[LANGUAGE_NONE][0]['value']) {
// Define the list of paths that should be excluded.
$paths_to_exclude = array(
'sample',
);
if (in_array($element['#href'], $paths_to_exclude)) {
// Stop processing here, meaning this list item will not be included in
// the menu.
return;
}
}
}
if ($element ['#below']) {
$sub_menu = drupal_render($element ['#below']);
}
$output = l($element ['#title'], $element ['#href'], $element ['#localized_options']);
return '<li' . drupal_attributes($element ['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
This code would go in the template.php
file of your front-end theme.