如何将模块添加到管理/配置页面?


27

我正在使用Drupal 7中的模块。我添加了一个hook_menu实现:

$items['admin/config/content/mymodule'] = [
  'title'            => 'MyModule',
  'description'      => 'Configure MyModule settings.',
  'page callback'    => 'mymodule_get_form',
  'page arguments'   => ['mymodule_admin_settings'],
  'file'             => 'mymodule.admin.inc',
  'access arguments' => ['administer mymodule'],
  'menu_name'        => 'mymodule',
];

...以及到mymodule.info的配置行:

configure = admin/config/content/mymodule

MyModule 的配置链接现在显示在admin / modules页面上,但是如何在admin / config页面上列出该模块?还是仅为核心模块保留了admin / config页?

Answers:


9

查看system_admin_config_page()的代码该代码是admin / config的页面回调),我注意到它包含以下几行:

if ($admin = db_query("SELECT menu_name, mlid FROM {menu_links} WHERE link_path = 'admin/config' AND module = 'system'")->fetchAssoc()) {
  $result = db_query("
    SELECT m.*, ml.*
    FROM {menu_links} ml
    INNER JOIN {menu_router} m ON ml.router_path = m.path
    WHERE ml.link_path != 'admin/help' AND menu_name = :menu_name AND ml.plid = :mlid AND hidden = 0", $admin, array('fetch' => PDO::FETCH_ASSOC));
  foreach ($result as $item) {
    _menu_link_translate($item);
    if (!$item['access']) {
      continue;
    }
    // ...
  }
  // ...
}

第一个查询选择与路径admin / config关联的菜单的menu_name字段,默认情况下为管理;第二个查询选择菜单名称相同的所有菜单,其父菜单为admin / config。

由于您的菜单使用不同的menu_name值,因此不会从第二个查询中选择它,也不会在admin / config页中显示该菜单。


24

如果这样做,则通过使用父项的system.module回调,在访问“ admin / config / mymodule”时会看到漂亮的列表页面

/**
 * Implements hook_menu().
 */
function MYMODULE_menu() {

  $items = [];

  $items['admin/config/mymodule'] = [
    'title'            => 'My configuration section',
    'description'      => 'This is the parent item',
    'position'         => 'left',
    'weight'           => -100,
    'page callback'    => 'system_admin_menu_block_page',
    'access arguments' => ['administer site configuration'],
    'file'             => 'system.admin.inc',
    'file path'        => drupal_get_path('module', 'system'),
  ];

  // Need at least one child item before your section will appear.
  $items['admin/config/mymodule/item'] = [
    'title'            => 'First item',
    'description'      => 'This is the first child item in the section',
    'page callback'    => 'mymodule_item_callback',
    'access arguments' => ['administer site configuration'],
  ];

  return $items;
}

3
不要忘记,您需要禁用并启用该模块,菜单更改才能生效。
jevon

5
@jevon:清除(菜单)缓存足以查看更改。
巴特2013年

可以在命令行中使用drush清除菜单缓存drush cc menu-参考:drupal.stackexchange.com/a/58621/1082
therobyouknow


1

我在配置页面上也遇到了麻烦。我能够将链接添加到配置页面的唯一方法是像这样声明两个菜单项,其中父链接位于“ admin / config / module”,子链接位于“ admin / config / module / manage” 。

  $items['admin/config/whh-maps'] = array(
    'title' => 'World Hiphop configuration',
    'description' => 'Allows administrators to configure maps for WHH.',
    'position' => 'left',
    'weight' => -30,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('whh_maps_form'),
    'access arguments' => array('administer whh maps'),
    'file' => 'whh_maps.admin.inc',
  );
  $items['admin/config/whh-maps/manage'] = array(
    'title' => 'Manage countries',
    'description' => 'Allows admins to edit country information',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('whh_maps_form'),
    'access arguments' => array('administer whh maps'),
    'file' => 'whh_maps.admin.inc',
    'weight' => -10,
  ); 

1
$items['admin/config/user-interface/mymodule'] = array(
    'title' => 'My Module',
    'description' => 'description',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_admin_function'),
    'access arguments' => array('administer site configuration'),
);

$items['admin/config/user-interface/mymodule/manage'] = array(
    'title' => 'My Module',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'description' => 'description',
    'weight' => -10,
);
return $items;

这对我有用。
实际上,我挣扎了大约半个小时,直到我意识到自己没有退还$ items ....我似乎总是这样做。


0
/**
 * Implements hook_menu().
 */
function notification_menu() {

  $items = [];

  $items['admin/customize'] = [
    'title'            => 'Send Comment notifications',
    'discription'      => 'Admin will send notification to user about updates',
    'type'             => MENU_NORMAL_ITEM,
    'page callback'    => 'drupal_get_form',
    'page arguments'   => ['notification_form'],
    'access arguments' => ['access adminstration page'],
    'access callback'  => TRUE,
  ];

  return $items;
}

/**
 * Custom form.
 */
function notification_form($form, &$form_state) {

  $form['send_mail_to'] = [
    '#title'         => 'Send Mail To',
    '#discription'   => 'To whom you want to send form',
    '#size'          => 40,
    '#type'          => 'textfield',
    '#required'      => TRUE,
    '#default_value' => variable_get('send_mail_to'),
  ];

  //here the admin can wite subject for the mail.
  $form['mail_subject'] = [
    '#title'         => 'Subject',
    '#discription'   => 'the purpous of this mail',
    '#type'          => 'textfield',
    '#size'          => 40,
    '#maxlenght'     => 120,
    '#required'      => TRUE,
    '#default_value' => variable_get('mail_subject'),
  ];

  $form['mail_body'] = [
    '#title'         => 'Body',
    '#discription'   => 'the body of your mail.',
    '#type'          => 'textarea',
    '#row'           => 10,
    '#columns'       => 40,
    '#required'      => TRUE,
    '#default_value' => variable_get('mail_body'),
  ];

  $form['mail_bcc'] = [
    '#title' => 'BCC this mail to all',
    '#type'  => 'checkbox',

  ];

  return system_settings_form($form);
}

1
这并不能回答问题,因为它没有说明为什么OP无法在管理页面中看到其模块。另外,仅代码的答案没有太大帮助。(检查代码:您有错别字。)
kiamlaluno
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.