Skip to content

Instantly share code, notes, and snippets.

@mguillermin
Created March 21, 2012 08:13
Show Gist options
  • Save mguillermin/2145559 to your computer and use it in GitHub Desktop.
Save mguillermin/2145559 to your computer and use it in GitHub Desktop.
Drupal - Utiliser Workbench pour filtrer les menus dispos lors de la création de contenu
<?php
function my_module_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'page_node_form' && !user_access('administer content')) {
// Alter node edit form
$menu_items = $form['menu']['link']['parent']['#options'];
$menu_items_to_delete = array();
$workbench_access_menus = workbench_access_get_user_tree();
foreach ($menu_items as $menu_key => $menu_value) {
$ok = false;
foreach ($workbench_access_menus as $id => $workbench_access_menu) {
if ($menu_key == $workbench_access_menu['access_type_id'].':'.$workbench_access_menu['access_id']) {
$ok = true;
}
}
if (!$ok) {
$menu_items_to_delete[] = $menu_key;
}
}
foreach ($menu_items_to_delete as $menu_key_to_delete) {
unset($form['menu']['link']['parent']['#options'][$menu_key_to_delete]);
}
}
if ($form_id == 'menu_overview_form' && !user_access('administer content')) {
// Alter structure/menu/manage form
$menu_items = array();
foreach ($form as $key => $value) {
if (strstr($key, 'mlid') !== false) {
$menu_items[] = $key;
}
}
$menu_items_to_delete = array();
$workbench_access_menus = workbench_access_get_user_tree();
foreach ($menu_items as $menu_key) {
$ok = false;
foreach ($workbench_access_menus as $id => $workbench_access_menu) {
if ($menu_key == 'mlid:'.$workbench_access_menu['access_id']) {
$ok = true;
}
}
if (!$ok) {
$menu_items_to_delete[] = $menu_key;
}
}
foreach ($menu_items_to_delete as $menu_key_to_delete) {
unset($form[$menu_key_to_delete]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment