You are here

function sarnia_menu_alter in Sarnia 7

Implements hook_menu_alter().

This totally sucks, but if we want Field UI forms to live under the "Sarnia" tab of Search API server pages, this is what we have to do.

Field UI hard-codes entity and bundle names into the menu array, or else it requires that they be present in the path. Also, the Sarnia tab path plus the Field UI field paths are longer than Drupal's limit of 9 menu arguments.

Moving Sarnia bundle field management to somewhere else in the menu system would mean we could remove this entire hook_menu_alter() implementation, but for now it is more important to keep the UI in one place.

File

./sarnia.module, line 87

Code

function sarnia_menu_alter(&$items) {
  if (!module_exists('field_ui')) {
    return;
  }

  // Remove paths added by Field UI.
  foreach ($items as $path => $item) {
    if (preg_match('#^admin/config/search/search_api/server/[^/]+/sarnia/(field|display)#', $path)) {
      unset($items[$path]);
    }
  }

  // Add customized Field UI paths.
  $base = array(
    'page callback' => 'drupal_get_form',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'field_ui.admin.inc',
    'file path' => drupal_get_path('module', 'field_ui'),
  );
  $items['admin/config/search/search_api/server/%sarnia_entity_server_name/sarnia/fields'] = array(
    'title' => 'Manage fields',
    'page arguments' => array(
      'field_ui_field_overview_form',
      5,
      5,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 9,
  ) + $base;

  // The field edit path has sub-paths for editing (default), updating field
  // settings, changing the widget type, and deleting the field, but the Drupal
  // menu system only goes 9 levels deep, and Sarnia bundles happen to be
  // located too deep.
  // - sarnia_field_ui_menu_access_callback() makes sure that the sub-path is
  //   allowed.
  // - sarnia_field_ui_menu_page_callback() summons the correct field editing
  //   form.
  $items['admin/config/search/search_api/server/%sarnia_entity_server_name/sarnia/fields/%sarnia_field_ui_menu'] = array(
    'load arguments' => array(
      5,
      '%map',
    ),
    'title callback' => 'field_ui_menu_title',
    'title arguments' => array(
      8,
    ),
    'access callback' => 'sarnia_field_ui_menu_access_callback',
    'access arguments' => array(
      'administer site configuration',
      9,
    ),
    'page callback' => 'sarnia_field_ui_menu_page_callback',
    'page arguments' => array(
      8,
    ),
  ) + $base;
  $items['admin/config/search/search_api/server/%sarnia_entity_server_name/sarnia/display'] = array(
    'title' => 'Manage display',
    'page arguments' => array(
      'field_ui_display_overview_form',
      5,
      5,
      'default',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  ) + $base;
  $items['admin/config/search/search_api/server/%sarnia_entity_server_name/sarnia/display/default'] = array(
    'title' => 'Default',
    'page arguments' => array(
      'field_ui_display_overview_form',
      5,
      5,
      8,
    ),
    'access callback' => '_field_ui_view_mode_menu_access',
    'access arguments' => array(
      5,
      5,
      8,
      'user_access',
      'administer site configuration',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
  ) + $base;
  foreach (sarnia_entity_types() as $type) {
    $entity_info = entity_get_info($type['machine_name']);
    $search_api_server = $type['search_api_server'];
    if (is_array($entity_info['view modes'])) {
      foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
        $items["admin/config/search/search_api/server/{$search_api_server}/sarnia/display/{$view_mode}"] = array(
          'title' => $view_mode_info['label'],
          'page arguments' => array(
            'field_ui_display_overview_form',
            $type['machine_name'],
            $type['machine_name'],
            $view_mode,
          ),
          'access callback' => '_field_ui_view_mode_menu_access',
          'access arguments' => array(
            $type['machine_name'],
            $type['machine_name'],
            $view_mode,
            'user_access',
            'administer site configuration',
          ),
          'type' => MENU_LOCAL_TASK,
        ) + $base;
      }
    }
  }
}