Pregunta:
Una pregunta para novatos, chicos.
¿Alguna idea de cómo puedo crear un menú mediante programación? Dice que tengo 3 menús diferentes que quiero crear. El primer menú se colocará en la parte superior izquierda del encabezado. El segundo menú se colocará debajo del primer menú. El tercer menú será la navegación principal.
¿Podrían estos los menús estar en el mismo grupo? ¿Sería un problema de estilismo?
Gracias
Respuesta:
Si está intentando hacer esto en un script de actualización, esto debería funcionar:
$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);
}
}
}
Los comentarios son bienvenidos si mi enfoque es incorrecto. Gracias.