在Drupal 7中以编程方式创建菜单


15

一个新手问大家。

有什么想法如何以编程方式创建菜单?说我要创建3个不同的菜单。第一个菜单将位于标题的左上方。第二个菜单将位于第一个菜单的下方。第三个菜单将是主导航。

这些菜单可以在同一组中吗?造型会不会有问题?

谢谢

Answers:


25

如果您尝试在更新脚本中执行此操作,则该方法应该起作用:

$menus = array(
  array(
    'menu_name' => 'menu_test_one',
    'title' => 'My Menu One',
    'description' => 'Lorem Ipsum',
  ),
  array(
    'menu_name' => 'menu_test_two',
    'title' => 'My Menu Two',
    'description' => 'Lorem Ipsum',
  ),
  array(
    'menu_name' => 'menu_test_three',
    'title' => 'My Menu Three',
    'description' => 'Lorem Ipsum',
  ),
);

$links = array(
  array(
    array(
      'link_title' => 'Link1',
      'link_path' => 'http://yourdomain.com/link1',
      'menu_name' => 'menu_test_one',
      'weight' => 0,
      'expanded' => 0,
    ),
    array(
      'link_title' => 'Link2',
      'link_path' => 'http://yourdomain.com/link2',
      'menu_name' => 'menu_test_one',
      'weight' => 1,
      'expanded' => 0,
    ),
  ),
  array(
    array(
      'link_title' => 'Link3',
      'link_path' => 'http://yourdomain.com/link3',
      'menu_name' => 'menu_test_two',
      'weight' => 0,
      'expanded' => 0,
    ),
    array(
      'link_title' => 'Link4',
      'link_path' => 'http://yourdomain.com/link4',
      'menu_name' => 'menu_test_two',
      'weight' => 1,
      'expanded' => 0,
    ),
  ),
  array(
    array(
      'link_title' => 'Link5',
      'link_path' => 'http://yourdomain.com/link5',
      'menu_name' => 'menu_test_three',
      'weight' => 0,
      'expanded' => 0,
    ),
    array(
      'link_title' => 'Link6',
      'link_path' => 'http://yourdomain.com/link6',
      'menu_name' => 'menu_test_three',
      'weight' => 1,
      'expanded' => 0,
    ),
  ),
);

// Save menu group into menu_custom table
foreach ($menus as $menu) {
  // Look the table first if the data does exist
  $exists = db_query("SELECT title FROM {menu_custom} WHERE menu_name=:menu_name", array(':menu_name' => $menu['menu_name']))->fetchField();
  // Save the record if the data does not exist
  if (!$exists) {
    menu_save($menu);
  }
}

$item = ''; 
foreach ($links as $layer1) {
  foreach ($layer1 as $link) {
    // Build an array of menu link 
    $item = array(
      'link_path' => $link['link_path'],
      'link_title' => $link['link_title'],
      'menu_name' => $link['menu_name'],
      'weight' => $link['weight'],
      'expanded' => $link['expanded'],
    );
    // Look the table first if the data does exist
    $exists = db_query("SELECT mlid from {menu_links} WHERE link_title=:link_title AND link_path=:link_path", array(':link_title' =>  $link['link_title'], ':link_path' => $link['link_path']))->fetchField();
    // Save the record if the data does not exist
    if (!$exists) {  
      menu_link_save($item);
    }
  }
}

如果我的方法有误,欢迎发表评论。谢谢。


这正是我想要的。让我尝试您的代码,稍后再提供反馈。感谢您的分享
user8012'9

5
代替运行$ exists数据库查询,一种更未来的方法(但可能会稍微慢一些)是使用if(!menu_load($ menu ['menu_name'])){menu_save($ menu); }
巴拉·克拉克

太好了!
泰勒·德登

我的建议是使用menu_get_menus()而不是查询。然后,您可以将菜单保存到var中,然后简单地执行if (!array_key_exists($menu, $menus)) {-还将单个参数FALSE添加到menu_get_menus()仅返回自定义菜单。
基督教徒

1
同样,当您检查菜单链接是否存在时://如果数据确实存在,请首先查看表$ exists = ...您还需要检查menu_name,因为同一链接组合可以位于不同的菜单中。
hugronaphor 2015年

4

这是一种从数组轻松填充菜单的方法:

<?php
function populate_menu($links, $menu_name, $plid = 0) {
  foreach ($links as $link) {
    $ls = array(
      'menu_name' => $menu_name,
      'link_title' => $link['link_title'],
      'link_path' => $link['link_path'],
      'plid' => $plid,
    );
    $newpid = menu_link_save($ls);
    if (!empty($link['childs'])) {
      populate_menu($link['childs'], $menu_name, $newpid);
    }
  }
}


$items = array(
  array(
    'link_title' => 'Menu1',
    'link_path' => '<front>',
    'childs' => array(
      array(
        'link_title' => 'Sub Item 1',
        'link_path' => '<front>',
        'childs' => array(
          array(
            'link_title' => 'Link item 1',
            'link_path' => '<front>',
          ),
          array(
            'link_title' => 'Link item 2',
            'link_path' => '<front>',
          ),
        ),
      ),
      array(
        'link_title' => 'Sub Item 2',
        'link_path' => '<front>',
        'childs' => array(
          array(
            'link_title' => 'Link item',
            'link_path' => '<front>',
          ),
        ),
      ),
    ),
  ),
);
populate_menu($items, 'main-menu');

2

hook_menu()这是您在自定义模块中需要实现的全部内容。有关创建自定义模块的信息,请参阅此文档

//Define the menus in the function which goes in your MYMODULE.module file  
function MYMODULE_menu() {
  //the menu which will point to http://yoursite/first-menu
  $items['first-menu'] = array(
    'title' => 'First menu',  // will appear as the name of the link
    // Page callback, etc. need to be added here.
  );

  //the menu which will point to http://yoursite/second-menu
  $items['second-menu'] = array(
    'title' => 'Second menu',  // will appear as the name of the link
    // Page callback, etc. need to be added here.
  );

  //the menu which will point to http://yoursite/third-menu
  $items['third-menu'] = array(
    'title' => 'third menu',  // will appear as the name of the link
    // Page callback, etc. need to be added here.
  );

  return $items;
}

通过将以下代码添加到page.tpl.php主题文件中,可以在任何区域中打印菜单。

//add this line in <div id="header"></div> to print it in header.
<?php
$menu1 = menu_navigation_links('first-menu');
print theme('links__first_menu', array('links' => $menu1));

//print second menu just below first menu
$menu2 = menu_navigation_links('second-menu');
print theme('links__second_menu', array('links' => $menu1));
?>

您不需要打印,third-menu因为默认情况下它将显示在导航菜单中。


注意:这不是创建导航菜单并将其添加到页面的最佳实践。hook_menu()用于创建页面回调,而不用于创建导航菜单。请阅读THIS,它解释了不同之处。当我开始学习Drupal时,我回答了这个问题,我不再推荐这个答案了。


1
根本不是创建导航菜单并将其添加到页面的最佳实践。hook_menu()用于创建页面回调,而不用于创建导航菜单。请阅读以下内容,说明不同之处。api.drupal.org/api/drupal/includes!menu.inc/group/menu/7
Christian

1
@Christian我刚刚开始回答这个问题时就开始学习Drupal。我更喜欢学习Drupal的方式是通过实际尝试在这里回答问题。以我目前的知识水平,我当然也会否决这种答案。但实际上,我为一开始的想法感到自豪:)感谢您的解释方式和所提供的链接。
AjitS 2014年

嗨,AjitS,一切都很好-我不会经常投票回答答案。我只是在答案之前添加一个更新,表明您不再建议这种方法。谢谢回复。
基督教徒

已删除我的否决票。
基督教徒

0

您也可以尝试菜单导入模块。菜单部署非常酷并且容易。您可以在网站上手动创建菜单并使用生成的JSON。您也可以为不存在的页面创建节点。

这是导出脚本的示例:

$menu_name = 'menu-footer-secondary-menu';

// Create menu if doesn't exist.
if (!menu_load($menu_name)) {
  $menu = array(
    'menu_name' => $menu_name,
    'title' => t('Footer secondary menu'),
    'description' => '',
  );
  menu_save($menu);
}

// Import menu links from JSON.
$menu_string = 'Impressum {"url":"node\/1","options":{"attributes":[]}}
Datenschutzbestimmungen {"url":"node\/2","options":{"attributes":[]}}
Nutzungsbedingungen {"url":"node\/3","options":{"attributes":[]}}
';

$options = array(
  //'link_to_content' => TRUE, // Look for existing nodes and link to them.
  'create_content' => TRUE, // Create new content (also if link_to_content not set).
  'remove_menu_items' => TRUE, // Removes current menu items.
  'node_type' => 'page',
  'node_body' => 'Page is in development...',
  'node_author' => 1,
  'node_status' => 1,
);

menu_import_string($menu_string, $menu_name, $options);

您可以使用hook_update_N()更新脚本处理器来运行此脚本


0

要创建菜单块,请在您的.install文件中实现hook_enable()类型

function mymodule_enable() {
  $t = get_t();
  if (module_exists('menu')) {
    menu_save(array(
      'menu_name' => 'my-new-menu',
      'title' => $t('My New Menu'),
      'description' => $t('My Menu description'),
    ));
  }
 }

在同一.install文件中,实现hook_uninstall()。

function mymodule_uninstall() {
  if (module_exists('menu')) {
   if ($my_menu = menu_load('my-new-menu')) {
    menu_delete($my_menu);
  }
 }
}

接下来,在实现hook_menu()的同时,在.module文件中。

function mymodule_menu() {
  return array(
   'mypage' => array(
   'title' => 'My Page',        
   'description' => 'My Page description',    
   'page callback' => 'my_page_callback_func',
   'page arguments' => array(arg(0)),
   'access arguments' => array('access content'),
   'menu_name' => 'my-new-menu',
   'file' => 'my.new.page.inc',
   'file path' => drupal_get_path('module', 'mymodule') . '/includes',
  ),
 );     
}

.inc文件包含在mymodule文件夹中,其中包括文件夹。

有关更多信息,请参见devel模块的devel.install和devel.module文件。

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.