menu_force.module in Menu Force 7
Same filename and directory in other branches
This module enables you to make Menu Settings required on specific content types
It forces a node from one or more content types to be included in the menu system before the content will be saved successfully. This can be useful in a number of situations, e.g. when using [menupath-raw] in the pathauto settings, which expects a node to live in the menu system. This module makes sure it does.
Based on the PHP snippet of @Keyz (http://drupal.org/node/466620)
File
menu_force.moduleView source
<?php
/**
* @file
* This module enables you to make Menu Settings required on specific content types
*
* It forces a node from one or more content types to be included in the menu system
* before the content will be saved successfully. This can be useful in a number of
* situations, e.g. when using [menupath-raw] in the pathauto settings, which expects
* a node to live in the menu system. This module makes sure it does.
*
* Based on the PHP snippet of @Keyz (http://drupal.org/node/466620)
*/
/**
* Implements hook_form_alter().
*/
function menu_force_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'node_type_form') {
$form['menu']['menu_force'] = array(
'#type' => 'checkbox',
'#title' => t('Make the Menu Settings mandatory for this content type'),
'#description' => t('Enabling this will force the user to add the node to the menu system.'),
'#default_value' => variable_get('menu_force_' . $form['#node_type']->type, FALSE),
);
$form['menu']['menu_force_parent'] = array(
'#type' => 'checkbox',
'#title' => t('Lock the "Default parent item" as well'),
'#description' => t('Enabling this will lock the parent item choice, forcing created content to a chosen parent in the menu.'),
'#default_value' => variable_get('menu_force_parent_' . $form['#node_type']->type, FALSE),
'#states' => array(
'invisible' => array(
'input[name="menu_force"]' => array(
'checked' => FALSE,
),
),
),
);
$form['#validate'][] = 'menu_force_node_type_form_validate';
$form['#submit'][] = 'menu_force_node_type_form_submit';
}
// If the menu settings have been forced, alter the form add/edit form
$node_types = node_type_get_types();
foreach ($node_types as $type) {
if ($form_id == $type->type . '_node_form') {
if (variable_get('menu_force_' . $type->type, FALSE)) {
// Make the menu entry required by hiding the
// checkbox and setting the link title required
$form['menu']['enabled']['#value'] = TRUE;
$form['menu']['enabled']['#type'] = 'value';
$form['menu']['link']['link_title']['#required'] = TRUE;
$form['#attached']['js'][] = drupal_get_path('module', 'menu_force') . '/menu_force.js';
}
if (variable_get('menu_force_parent_' . $type->type, FALSE)) {
//disable the drop down option as required
$form['menu']['link']['parent']['#disabled'] = TRUE;
}
}
}
}
/**
* Validate the menu_force settings when a content type form has been submitted
*/
function menu_force_node_type_form_validate($form, &$form_state) {
if ($form_state['values']['menu_force_parent']) {
$menu_parent = $form_state['values']['menu_parent'];
list($menu, $menu_item) = explode(':', $form_state['values']['menu_parent']);
// Print an error if a menu is selected instead of a menu_item
if ($menu && $menu_item == 0) {
form_set_error('menu_parent', t('If you want to force a Default parent menu item, please select which one.'));
}
}
}
/**
* Saves the menu_force settings when a content type form has been submitted
*/
function menu_force_node_type_form_submit(&$form, $form_state) {
$force_menu = $form_state['values']['menu_force'];
$force_menu_parent = $form_state['values']['menu_force_parent'];
if ($force_menu) {
variable_set('menu_force_' . $form_state['values']['type'], TRUE);
}
else {
variable_del('menu_force_' . $form_state['values']['type']);
}
if ($force_menu_parent) {
variable_set('menu_force_parent_' . $form_state['values']['type'], TRUE);
}
else {
variable_del('menu_force_parent_' . $form_state['values']['type']);
}
}
/**
* Delete the menu_force setting when a content type is deleted
*/
function menu_force_type_delete_confirm(&$form, $form_state) {
variable_del('menu_force_' . $form_state['values']['type']);
variable_del('menu_force_parent_' . $form_state['values']['type']);
}
Functions
Name![]() |
Description |
---|---|
menu_force_form_alter | Implements hook_form_alter(). |
menu_force_node_type_form_submit | Saves the menu_force settings when a content type form has been submitted |
menu_force_node_type_form_validate | Validate the menu_force settings when a content type form has been submitted |
menu_force_type_delete_confirm | Delete the menu_force setting when a content type is deleted |