Embedding a View from a menu hook in Drupal

Today I was looking for some code to embed a view from a menu hook in Drupal 7. I could find some examples of embedding views, and of creating menu hooks, but nothing that brought the two together. The following example shows how to embed a view from a menu hook implemented by your module.

To start with you will need to know the name of your view, and the display id that you wish to embed. In my exported view "resources" I searched for the title I gave the display and found:

/* Display: Reviewed Page */
$handler = $view->new_display('page', 'Reviewed Page', 'page_2');

This showed me that the display id for the view is "page_2".

Then I needed to create a menu hook in my module to embed the view, using the views_embed_view() function.

* Implements hook_menu().
function mymodule_menu() {

// define the menu hook as an array. The key is the path.
$items['resources'] = array(
'title' => 'Resources',

// Content for this menu hook will come from views_embed_view().
'page callback' => 'views_embed_view',

// Define an array to pass the view name and display id as arguments to views_embed_view().
'page arguments' => array('resources', 'page_2'),

// Set the menu item type: MENU_CALLBACK does not add an item to a visible menu.
'type' => MENU_CALLBACK,

Set the permission required to access this menu item / path.
'access arguments' => array('edit any resource content'),

// Provide the path to the views module.
'file path' => drupal_get_path('module', 'views'),

// Provide the name of the file in which views_embed_view() is defined.
'file' => 'views.module',

return $items;

The view can now be accessed from /resources by users with the specified permission.