I have a page "/node/add" that I would like to programmatically limit the links that are displayed based on the users role. Is using either hook_form_alter or hook_form_ID_alter the way to do this? I just figured out that "/node/add" is not a form so I can't use hook_form_alter or hook_form_ID_alter but in doing a little research it appears that I may be able to user hook_page_alter() instead to filter out some of the links.
1 Answer
If you set node creation permission (Node Module on www.yourhost.com/admin/user/permissions
) to specific roles, the existing form should take care of this automagically by itself, eg, if the user accessing www.yourhost.com/node/add
can't create a specific node type, it won't be presented on that page.
Based upon commentary, node/add
is built via:
function node_add_page() {
$item = menu_get_item();
$content = system_admin_menu_block($item);
return theme('node_add_list', $content);
}
/**
* Display the list of available node types for node creation.
*
* @ingroup themeable
*/
function theme_node_add_list($content) {
$output = '';
if ($content) {
$output = '<dl class="node-type-list">';
foreach ($content as $item) {
$output .= '<dt>'. l($item['title'], $item['href'], $item['localized_options']) .'</dt>';
$output .= '<dd>'. filter_xss_admin($item['description']) .'</dd>';
}
$output .= '</dl>';
}
return $output;
}
so you most certainly could create a new YOURTHEME_node_add_list()
function that filters the links based upon a user's role realizing that if the user entered the link directly it would nonetheless still work.
You could also create a node-add-list.tpl.php
in your theme's directory with basically this in it as a start:
<?php
$output='';
if ($content) {
$output = '<dl class="node-type-list lrfm-ed">';
foreach ($content as $item) {
$output .= '<dt>'. l($item['title'], $item['href'], $item['localized_options']) .'</dt>';
$output .= '<dd>'. filter_xss_admin($item['description']) .'</dd>';
}
$output .= '</dl>';
}
print $output;
and then modify it however you wanted.
Just flush caches after either to make drupal's theme system see and pick up the changes.
-
I am aware about limiting access to content types by role but the problem is that I can't get the workbench module to work correctly unless I give the access "Node: Bypass content access control" to users that use workbench/workflow which causes all the content types to be listed within /node/add page. Commented May 20, 2013 at 15:42
-
This YOURTHEME_node_add_list() function do I put this within a custom module like to do for hooks? Commented May 20, 2013 at 16:31
-
it would go inside your theme, most usually template.php Commented May 20, 2013 at 16:32
-
you could also create a
node-add-list.tpl.php
file with the logic in it and save that in your theme's directory if you didn't want to modify any files there directly. (inside of which you would basically just print $output instead of returning it) Commented May 20, 2013 at 16:40 -