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
Categories:
Programming
- Drupal
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!
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.
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!
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.
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! :)
My solution here only adds node editing feature for already available functionality.
https://github.com/rogercodina/node_menusettings_visibility_checkbox
Students do not have to worry about their Online Essay Writing Service anymore because our writers are here to provide any Research Writing Services.
Getting reliable and competent Custom Writing Services writing companies is no longer an easy task as most of the current providers are unreliable and provide poor Custom College Papers that do not let students get high scores.