0

The problem: We have a main site with 70 subsites. On these subsites are users who have access to the menu settings. When they add a menu item they need to check the box near their own subsite. If they forget this the menu item will be shown on all the other domains. Sometimes resulting in multiple menu items with the same title.

The proposed solution: Alter the menu_edit_item form so that the $form['domain_menu_access']['manage']['domain_menu_access_show']['#default_value'] is set to the domains the current user has access to.

My question: How do I set the default value for these checkboxes? Here is my code:

// Alter the edit menu item form


if($form_id == 'menu_edit_item'){
    $domains = array();
    if($user->uid > 1){
      $form['domain_menu_access']['manage']['#collapsed'] = false;
      foreach($user->domain_user as $domain){

    $form['domain_menu_access']['manage']['domain_menu_access_show']['#default_value'] = array((string)'d'.$domain);
  }
}

I have tried several variations with the $domain. (array($domain), array($domain => $domain), etc)

0

I found a workaround. I just rebuild the options array with the domains the user has admin access to. Here is my code:

// Alter the edit menu item form


if($form_id == 'menu_edit_item'){
    $form['domain_menu_access']['manage']['#collapsed'] = false;
    $domains = array();
    if($user->uid > 1){

  // We don't need to hide any menu items on other domains so we don't show this option.
  $form['domain_menu_access']['manage']['domain_menu_access_hide']['#access'] = false;

  // Loop the user object for all the domains current user is connected to
  foreach($user->domain_user as $domain){
    // Get the domain data
    $domains[] = domain_lookup($domain);

    // First unset all the options of the form element so we can rebuild it with our own values
    unset($form['domain_menu_access']['manage']['domain_menu_access_show']['#options']);
    //Rebuild the #options array
    $form['domain_menu_access']['manage']['domain_menu_access_show']['#options']['d'.$domain] = $domains[0]['sitename'];

    // Set the default value(s)
    $form['domain_menu_access']['manage']['domain_menu_access_show']['#default_value'] = array('d'.$domain);
  }
}

}

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.