| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id: automenu.module,v 1.1 2008/06/09 08:04:05 danielfs Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_nodeapi(). |
* Implementation of hook_nodeapi(). |
| 6 |
* Fill in menu section values if user left them empty AND there's a parent |
* Fill in menu section values if user left them empty AND there's a parent |
| 7 |
* menu item selected for this node type. |
* menu item selected for this node type and the node is published. |
| 8 |
*/ |
*/ |
| 9 |
function automenu_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { |
function automenu_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { |
| 10 |
switch ($op) { |
switch ($op) { |
| 11 |
case 'submit': |
case 'insert': |
| 12 |
$parent_menu = variable_get('parentmenu_'. $node->type, 0); |
case 'update': |
| 13 |
if ($parent_menu != 0) { |
$parent_menu = explode(":", variable_get('parentmenu' . $node->language . '_' . $node->type, '0')); |
| 14 |
if (($node->menu['title'] == '') || ($node->menu['delete'])) { |
if ($parent_menu[0] != '0') { |
| 15 |
$node->menu['title'] = $node->title; |
if (($node->menu['link_title'] == '' || $node->menu['delete']) && $node->status == 1) { |
| 16 |
$node->menu['pid'] = $parent_menu; |
$new_menu = array( |
| 17 |
unset($node->menu['delete']); |
'link_path' => 'node/' . $node->nid, |
| 18 |
|
'link_title' => $node->title, |
| 19 |
|
'plid' => $parent_menu[1], |
| 20 |
|
//'customized' => true, // ? |
| 21 |
|
); |
| 22 |
|
if (!menu_link_save($new_menu)) { |
| 23 |
|
drupal_set_message(t('There was an error saving the auto-menu link.'), 'error'); |
| 24 |
|
} |
| 25 |
|
else { |
| 26 |
|
drupal_set_message(t('The page was automatically added to: !menu.', array('!menu' => $parent_menu[0]))); |
| 27 |
|
} |
| 28 |
} |
} |
| 29 |
} |
} |
| 30 |
break; |
break; |
| 35 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 36 |
* Add the "Default Parent Menu" section to content type edit node. |
* Add the "Default Parent Menu" section to content type edit node. |
| 37 |
*/ |
*/ |
| 38 |
function automenu_form_alter($form_id, &$form) { |
function automenu_form_alter(&$form, $form_state, $form_id) { |
| 39 |
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) { |
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) { |
| 40 |
|
// Get a list of enabled languages. |
| 41 |
|
$languages = language_list('enabled'); |
| 42 |
|
$languages = $languages[1]; |
| 43 |
|
|
| 44 |
|
// Shouldn't this be language specific?! |
| 45 |
|
$options = array_merge(array("0" => "None"), menu_parent_options(menu_get_menus(), 0)); |
| 46 |
|
|
| 47 |
|
// add a selection for "no language" selection... maybe we can find a |
| 48 |
|
// better way (I think this is what happens in language neutral situations?) |
| 49 |
|
|
| 50 |
$form['workflow']['parentmenu'] = array( |
$form['workflow']['parentmenu'] = array( |
| 51 |
'#type' => 'select', |
'#type' => 'select', |
| 52 |
'#title' => t('Default Parent Menu'), |
'#title' => t('Default parent menu'), |
| 53 |
'#default_value' => variable_get('parentmenu_'. $form['#node_type']->type, 0), |
'#default_value' => variable_get('parentmenu_' . $form['#node_type']->type, 0), |
| 54 |
'#options' => _automenu_build_menu_opts() |
'#options' => $options, // TODO filter on menu items that are language neutral? |
| 55 |
|
'#description' => t('Select the default menu for nodes without a language (i.e. language neutral.)'), |
| 56 |
); |
); |
|
} |
|
|
} |
|
| 57 |
|
|
| 58 |
/** |
foreach ($languages as $language) { |
| 59 |
* Build the menu list to show in the content type edit form. |
$form['workflow']['parentmenu'. $language->language] = array( |
| 60 |
*/ |
'#type' => 'select', |
| 61 |
function _automenu_build_menu_opts($pid=0, $indent='') { |
'#title' => t('Parent menu for !lang nodes', array('!lang' => $language->name)), |
| 62 |
$t = menu_get_menu(); |
'#default_value' => variable_get('parentmenu' . $language->language . '_' . $form['#node_type']->type, 0), |
| 63 |
$result = array($pid => $indent . $t['visible'][$pid]['title']); |
'#options' => $options, // TODO filter on menu items of current language |
| 64 |
if (isset($t['visible'][$pid]['children'])) { |
'#description' => t('Select the default menu for nodes written in !lang.', array('!lang' => $language->name)), |
| 65 |
foreach ($t['visible'][$pid]['children'] as $child) { |
); |
|
$result += _automenu_build_menu_opts($child, '--'. $indent); |
|
| 66 |
} |
} |
| 67 |
} |
} |
|
return $result; |
|
|
} |
|
| 68 |
|
} |