Adding enabled checkbox into node menu settings in Drupal

This post applies to drupal 6 (D6).

I needed to add enabled checkbox into menu settings in node editing form. It makes easier to add new menu trough node form, wich must be hidden from start on. By default you can create hidden menu only trough menu manager. At least it seems so.

I came up with this simple solution that worked for me.

Altering menu settings in node form

Only 2 hooks were needed to accomplish it. First I added dummy checkbox named d6_enabled into form page_node_form trough hook_alter_form. That checkbox is used to trigger either enabled or hidden value. Look at comments. It should be self explanatory what is going on here.

function hook_form_alter( &$form, $form_state, $form_id ) {
  
  //make local copy of form_id
  $test_form_id = $form_id;
  
  //overwrite all different node type forms to "node_form"
  //so only nodes will have this checkbox
  if( substr($test_form_id, -10) == '_node_form' ){
      $test_form_id = 'node_form';
  }
  
  //Also this variant was proposed
  /*
  if( $form['#id'] == 'node-form' ){
    $test_form_id = 'node_form';
  }
  */
  
  switch ( $test_form_id ) {
    
    case 'node_form':
    
      //get mlid, if mlid captured
      //this is node editing action
      $mlid = 0;
      if( isset( $form['menu']['#item']['mlid'] ) &&
	    $form['menu']['#item']['mlid'] > 0 ){
	    $mlid = $form['menu']['#item']['mlid'];
	    $menuItem = menu_link_load( $mlid );
      }

      //add dummy hidden/enabled checkbox and
      //use previous value for checkbox status if available
      $form['menu']['d6_enabled'] = array(
	    '#type' => 'checkbox',
	    '#title' => t('enabled'),
	    '#default_value' => ($mlid == 0 ? 1 : ($menuItem[ 'hidden' ] ? 0 : 1) ),
	    '#description' => 
	      t('Menu items that are not enabled will not be listed in any menu.'),
      );
    
    break;
  
  }
}

Capturing the status and preparing it

I used nodeapi to alter menu's hidden field value in presave action. It is done before actually saving menu data, so this manipulation is also saved along other data.

function hook_nodeapi(&$node, $op, $teaser, $page) {
  
  switch ( $op ) {
    case 'presave':
      $node->menu['hidden'] = $node->menu['d6_enabled'] == 1 ? 0 : 1;
      break;
    default:
      break;
  }
  
}

Download

Download this code as working package here. This module comes with absolutely no warranty. Use it at your own risk.

Some resources

No gravatar aivable
Udit Sharma #
hi,

thanks for wonderfull code. Many thanks. I am also adding this code into my custom module, but this hides the Create content menu and also there is no checkbox in any edit/creating form, but when I create any node with menu item it is disabled. Can you tell me what I am doing wrong?

Many thanks for your code.

Cheers!
Janar #
Hi,

Thank you for your comment.
Because of that I found a little bug in this code.
It only worked for "page" node type and others were hidden, because there was no checkbox. I fixed it and updated the post.
No gravatar aivable
Udit Sharma #
Hi,

Many thanks for your code!
One suggestion from my side, just a few.

//overwrite all different node type forms to "node_form"
//so only nodes will have this checkbox
if( substr($form_id, -10) == '_node_form' ){
$form_id = 'node_form';
}

modifying the $form_id variable a bit dangerous if someone wants to use the variable later. we can switch with the $form['#id'] (value ‘node-form’) without using that hack. Can we use that too? How can we do that?

Waiting for your reply. Again many thanks for wonderful code.

Cheers!
janar #
To: Udit,

Thank you pointing this out. I added your proposed variant also in original post as a comment.

But I can't tell is it the right way or not. But seems much cleaner indeed.
No gravatar aivable
Steve #
This looks like exactly what I need... unfortunately, I am not well-versed in PHP (I'm pretty much a front-end guy). I am using the Contrast theme... can you please tell me where this code would go? Thanks so much!
Janar Jürisson #
This code is meant to be put in (custom)module (method names must be changed accordingly to reflect module name). I am pretty sure that you can't use it in template. You have to get into basics of module development for this piece of code.
No gravatar aivable
Steve #
Thanks for the reply, Janar! Do you have this available as a module already that I could use? Again, this is EXACTLY what I am looking for. Any help would be most appreciated!
Janar Jürisson #
to Steve: I added download section to the original article. Grab it from there :)
No gravatar aivable
Steve #
Thanks Janar! I installed the module & it is doing what I hoped it would... nice work & MUCH appreciated!
BiboKid #
Great work! This is what i've been looking for, for hoursss. But still i need to tweak something, but I don't know how to. I am new to drupal.

currently, your wonderful solution hides the menu on every display. What i want to do is to be able to only hide a menu on my top navigation pane, but still display them all on my left nav (even the "disabled/hidden" ones).

Can you suggest an attack plan?? Thanks! :)
Janar #
You could make your custom module to save your custom left/top state for each menu item and use that info later when viewing menus. But I am not sure that there is some hook to use this info prior viewing to hide needed ones.

My solution here only adds node editing feature for already available functionality.
BiboKid #
Thank you anyway :)
No gravatar aivable
Sho #
This is great. Any suggestions for getting this to work in Drupal 7?
No gravatar aivable
codemuncher #
for D7, i coudnt get the checkbox to persist during save so i used a field as checkbox but also hook_node_presave instead of hook_nodeapi and $node->menu['hidden'] = $node->field_choice['und'][0]['value'];
No gravatar aivable
Roger #
I have wrote a D7 version based on your code. It's an out of the box solution so interested people only have to enable the module.

https://github.com/rogercodina/node_menusettings_visibility_checkbox
No gravatar aivable
2015t new #
j\
No gravatar aivable
2015 #
UW OLSS
No gravatar aivable
5'}U 2015% #
a
No gravatar aivable
2015% #
ϦǣSnݹ
No gravatar aivable
100,i #
No gravatar aivable
100 #
No gravatar aivable
2015% #
No gravatar aivable
2015t% 3 #
I am co-founder of web/media studio GIVE me. and (android)developer at start-up named Choco. Read my about page to learn more.