Beyond Spots & Dots | Digital

Re-Adding "Create Content" Link in Drupal 7 Admin Menu Module

It's been noticed on Drupal.org that the newest versions of the Admin Menu module, most notably in the Drupal 7 version, do not contain the "Create Content" submenu item. To add new content in a default Drupal 7 installation, one must take the somewhat convoluted route of first clicking the "Content" link– taking one to the Content List page– then clicking "Add Content", and then select what type of content to create. If you're counting, that's 3 clicks and page loads where there used to be one click and page load– not the most convenient.

I have written a simple module for Drupal 7 (although similar code should work for Drupal 6) which adds a new tab entitled "Create", under which the types of content the user can create are listed. In this way, the approach is not unlike the Simple Menu module. These are limited by content permissions, so that a non-privileged user cannot see a content type listed here unless he or she has been given permission to create it.

The Source Code

Take this code and add it to a file entitled create_content_links.module. Of course, do not forget to add a file entitled create_content_links.info.



/**

* @file
* Adds "Create Content" links to the Admin Menu, and removes "Tasks" and "Index".
*/

/**
* Implementation of hook_admin_menu_output_alter().
*
* Add "Create content" as a top level submenu in the admin menu.
*/
function create_content_links_admin_menu_output_alter(&$content) {
// Add a top level item for the Create content menu itself.
$content['create_content_links'] = array(
'#theme' => 'admin_menu_links',
'#weight' => -99,
'#sorted' => TRUE,
);

// Copy the create content submenu to our backend menu.
$content['create_content_links']['create-content'] = array(
'#title' => t('Create'),
'#href' => 'node/add',
'#weight' => -10,
);

foreach(node_type_get_types() as $type => $object) {
if (node_access('create', $type)) {
$node_type_url = str_replace('_', '-', $type);
$content['create_content_links']['create-content'][$node_type_url] = array(
'#title' => $object->name,
'#href' => 'node/add/'. $node_type_url,
);
} // end if node_access
} // end 'foreach'

// Remove "Tasks" and "Index" from Admin Menu output
$admin_menu_exclusions = array(
t('Tasks'),
t('Index'),
);

foreach($content['menu'] as $menu_key => $menu_tree) {
if (in_array($menu_tree['#title'], $admin_menu_exclusions))
unset($content['menu'][$menu_key]);
}
} // end hook_admin_menu_output_alter

Beyond Spots & Dots | Drupal