0

I am writing a module that gets data from a couple sources, mostly XML, and then creates a nested array of values, such as "Title" and "URL"/link.

I'd like to create a new menu and then loop through my nested array and for each entry create a new menu item or child menu item, passing the title and URL to Drupal's menu system. I know that I can create menus with menu_save and items with hook_menu but how to create a new menu that has all external URLs?

Here is a part of the data:

Array
(
    [8CB0A2C5DC3B19401323F1A3F22D5769] => Array
        (
            [id] => 8CB0A2C5DC3B19401323F1A3F22D5769
            [parentId] => CA4CBBBB070F043ACF7FB35FE3FD1081
            [level] => 1
            [link] => http://externallink.com/fjdsklafjdkl
            [title] => Foo
            [orderWeight] => 1
            [children] => Array
                (
                    [259D3A1F1BCB8D601B13742603415970] => Array
                        (
                            [id] => 259D3A1F1BCB8D601B13742603415970
                            [parentId] => 8CB0A2C5DC3B19401323F1A3F22D5769
                            [level] => 2
                            [link] => http://externallink.com/hi
                            [title] => FooFooFoo
                            [orderWeight] => -3
                            [children] => Array
                                (
                                    [23C7938348994922AB879B87A85567DF] => Array
                                        (
                                            [id] => 23C7938348994922AB879B87A85567DF
                                            [parentId] => 259D3A1F1BCB8D601B13742603415970
                                            [level] => 3
                                            [link] => http://externallink.com?jfsdk
                                            [title] => FooFooFooFooFooFoo
                                            [orderWeight] => -1
                                        )

                                    [993EC027EC1342E39F48A30E754621AA] => Array
                                        (
                                            [id] => 993EC027EC1342E39F48A30E754621AA
                                            [parentId] => 259D3A1F1BCB8D601B13742603415970
                                            [level] => 3
                                            [link] => http://externallink.com
                                            [title] => FooFooFooFooFoo
                                            [orderWeight] => 1
                                        )
                                  )
                            )
                      )
                )
          [B7D67ECD8DD382E2A9F6E8F8149011E4] => Array
              (
                  [id] => B7D67ECD8DD382E2A9F6E8F8149011E4
                  [parentId] => CA4CBBBB070F043ACF7FB35FE3FD1081
                  [level] => 1
                  [link] => http://externallink.com
                  [title] => FooFooFoo
                  [orderWeight] => 3
                  [children] => Array
                      (
                          [B18FA5C86281B72BB9914EC541216F21] => Array
                              (
                                  [id] => B18FA5C86281B72BB9914EC541216F21
                                  [parentId] => B7D67ECD8DD382E2A9F6E8F8149011E4
                                  [level] => 2
                                  [link] => http://externallink.com
                                  [title] => Foo Foo Foo
                                  [orderWeight] => 8
                                  [children] => Array
                                      (
                                          [C20086F4FDF627499800D60391C384E3] => Array
                                              (
                                                  [id] => C20086F4FDF627499800D60391C384E3
                                                  [parentId] => B18FA5C86281B72BB9914EC541216F21
                                                  [level] => 3
                                                  [link] => http://externallink.com60391C384E3
                                                  [title] => Foo Foo
                                                  [orderWeight] => 9
                                              )

                                          [E5F00486FC054E78B68A996B30DAC89C] => Array
                                              (
                                                  [id] => E5F00486FC054E78B68A996B30DAC89C
                                                  [parentId] => B18FA5C86281B72BB9914EC541216F21
                                                  [level] => 3
                                                  [link] => http://externallink.commmm
                                                  [title] => Foo
                                                  [orderWeight] => 26
                                              )
                                        )
                                  )
                            )
                      )
)
2

You can create menu programmatically, using menu_save function and after that add links to this menu using menu_link_save function.

  • Thanks! I didn't know about menu_link_save() and external links. Worked a treat! :) – jonathanbell May 29 '15 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.