Skip to content
Snippets Groups Projects
user avatar
Project-Update-bot authored
b5c5235a
History
user avatar b5c5235a

REST menu items

This module provides a REST endpoint to retrieve menu items based on the menu name.

For example /api/menu_items/main provides you with the full menu tree of the main menu.

By adding query parameter like ?max_depth=1 or ?min_depth=2 you control how to output the menu in the web service.

Requirements

This module depends on the Drupal core module RESTful Web Services, which will be installed automatically. Optionally, you can install the REST UI module for enabling REST resources.

Installation

Install as you would normally install a contributed Drupal module. For further information, see Installing Drupal Modules.

Configuration

Once the module is installed you must enable the REST resource:

  • Through the UI by installing the contrib module REST UI. After this module is enabled, navigate to /admin/config/services/rest (Configuration > Web Services > REST resources) to enable the REST menu items resource.
  • Through configuration changes or programmatically. Read RESTful Web Services API overview to see how this can be done.

After the resource has been enabled, you must set access permissions via /admin/people/permissions (People > Permissions)

Navigate to admin/config/services/rest_menu_items (Configuration > Web Services > REST menu items through the administration panel) and configure the available values you want to output in the JSON (or XML) response. Also check which menus should be available as REST resource (for example you may not want to expose the administration menu).

Customizing

Change the output

Two hooks are being provided to customize the output:

  • hook_rest_menu_items_resource_manipulators_alter
  • hook_rest_menu_items_output_alter

See rest_menu_items.api.php for more information.

Change the endpoint URL

If you ever want to change the endpoint URL you can do this with hook_rest_resource_alter:

/**
 * Implements hook_rest_resource_alter().
 */
function MYMODULE_rest_resource_alter(&$definitions) {
  if (!empty($definitions['rest_menu_item'])) {
    $definitions['rest_menu_item']['uri_paths']['canonical'] = '/api/v2/my-fancy-menu-items/{menu_name}';
  }
}

Troubleshooting

  • If you get a 406 - Not Acceptable error you need to add the "?_format=json|hal_json|xml" attribute to the URL. See information about this 406 response
  • Submit bug reports and feature suggestions, or track changes in the issue queue.

More information

Maintainers

Sponsors

This project is sponsored by Finalist