Ok, I have looked over the menu api and I can't rig a way to do the following:

1. Get an array containing all the important data from the active menu
2. iterate through that array and create teasers for the nodes represented in that menu

Please please please don't recommend taxonomy for this. Just tell me how to get the DATA for the menu, like the Node IDs represented by the links. I don't want it formatted. I just want the data.

Comments

brooklynwebguy’s picture

Bumping.

somebody knows the answer to this.

How do I get a menu array for one of my own menus in the same way I can do it with the primary or secondary links?

newdru’s picture

but i'll put a question back at you..

why would you want to do this?

The active menu already has subitems that are visible when you display the menu right? that allows a user to click on them at will and dispaly the page view of the link.

Why would you want to dispaly teasers of all the links then? It would seem like you want a section home page nstead.

The reason why someone recommended taxonomy is because your nodes should probably not be made into menu links. Instead they should be categorized and then taxonomy or views will automatically create the teaser list for you.

I guess what i'm saying is that you probably can do what you want in some way but most nodes that are displayed as teasers are really "stories" or "article" TYPE content.. not static pages. Generally you don't want stories to be menu items/links because they quicly go out of date and lose validity over time. Pages on the other hand are more static in nature. They're fixed and you do want them to exist in menus.

That may not make a lot of sense right now but i've been trying to learn the content structuring for a couple of months and it eventually starts to sink in and make sense. Overall, i think the menus/structuring of content is the hardest part to master in drupal and any CMS for that matter. It's an art and requires a lot of brain processing to grasp it and make it work for you. Mostly visualizing how can i get x done.

Note, I don't believe this is a limitation of drupal. most cms's struggle with the handling of data and menus in a way that makes it easy to setup and use. But after you walk through it and ask yourself how would actually do it, you find that there's no really easy answer.

Which means if you want to do what you want to do, you're probably going to have to code it.

chew on that for awhile and then ask yourself why I do NOT want to use taxonomy. maybe you'll see some light and realize, i don't need to make my nodes menu items..

hope that doesn't sound too esoteric but it's the best i can offer you.
good luck

brooklynwebguy’s picture

The active menu already has subitems that are visible when you display the menu right? that allows a user to click on them at will and display the page view of the link.

Why would you want to dispaly teasers of all the links then? It would seem like you want a section home page nstead.

I would display teasers because teasers give a stronger idea of what's on a page than a link title does. Lots of sites use teasers for content that is relatively static. You are right, I want something akin to a section home page. Is there some easy way of getting this in Drupal?

The reason why I don't like using taxonomy for things like this is because it is very hard if not impossible to get things based on taxonomy that integrate both the taxonomy categories and the page links. You end up with stupid things like a menu link that points to a page with precisely one teaser pointing at precisely one page. It is also hard to control link and teaser order when using taxonomy rather than explicit menu assignments.

The overuse of taxonomy and the resultant placelessness of everything in many drupal sites is the reason why in my view most Drupal sites are, from a usability standpoint, amateurish failures.

For me, taxonomy is useful for things that change a lot like news stories and blog posts and for linking that changing stuff to the static stuff. It's not good for making menus for fairly deep hierarchies of static, descriptive content. Menus are perfect for that and some function call that gives me all that link data would be the icing on the cake.

.

newdru’s picture

I can see where you're coming from, especially with regard to ordering to be exactly like it is in the menu tree.

fwiw, i agree the majority of amateurish drupal sites do have poorly designed navigation / content structures. but after exploring this myself for awhile that comes mostly from a lack of insight into the nature of the beast, knowledge of usability and how complex this stuff actually is. Most people want to use it out of the box to do everything their unique situation requires but the sw can't predict everything or every data structure because the data is so variable.

if you think you have a permanent / bulletproof way to make this work, try and invent one and contribute it back to the community. it's not so easy.

which means as i said before you're going to have code a php snippet or module to do what you want on the top level section page. this definitely can be done. i don't know off hand but i'll give you a jumpstart on condition you post your solution back here :-).

go to

api.drupal.org

search in drupal 5 for "menu".

You'll get a whole list of drupal functions / hooks from the popup related to menu ops.

One of those, something like menu_tree or theme_menu_tree, will probably allow you to get the menu structure you want. You probably have to give it a menu id so it knows which menu to go to. Then it's up to you to loop through the menu items, yank the node id or path that underlies each menu item and then create teasers for each node id (nid). go back to api.drupal.org and search on node.

You'll find node_load and node_view hooks to help you with the last two ops.

Basically i've given you everything you need (i think), the pseudo code and functions, to solve this. You just need to code up the details in php.

good luck and post the solution when you get it!

brooklynwebguy’s picture

As I said in my first post I looked at the menu api and could not find what I needed.

In the absence of something in menu that gives me what I want, i'm working on extracting the data from the string returned by theme_menu_tree using the SimpleXML library in php. This seems inefficient (removing formatting that I don't want added in the first place) but it shouldn't be too expensive. I will contribute it back as a snippet if I think it will benefit others.

In the meantime if someone has a better idea, please advise.

newdru’s picture

i told you and you're rigth menu_tree isn't exactly it...

it looks like you might have to dig into those functions (look at them in the api) and see what calls they make. you could use a direct sql select from the menu table to pull the items you want.... it basically is going to involve some work...

it can still be done but sorry if i lead you astray that a function provided the menu elements unthemed.

i'm really surprised that doesn't exist... maybe somebody else will post that we've both overlooked something..

brooklynwebguy’s picture

There is quite a lot of information provided in theme_menu_tree (like which submenu is active, for instance) that I really don't want the headache of re-coding. The SimpleXML solution with theme_menu_tree seems to be working out ok. By the way, for folks who know xpath, I strongly recommend SimpleXML. Beats hell out of scraping.

newdru’s picture

post your example with simpleXML. I'd be curious to see how you're using it to strip out the themed tags from the data you want. thanks...

brooklynwebguy’s picture

This example just shows how you get the hrefs from the active submenu, but you could pull out any attribute, such as the li class data or the link titles. I like SimpleXML because I am an XSL/Xpath developer in my day job. You can no doubt accomplish the same thing with regular expressions, but xpath is more to my taste for something like this.

  
  $menu= theme_menu_tree(45); /* Get active menu. This example uses a hardcoded menu id. */
  $xml = simplexml_load_string($menu); /* Load menu for SimpleXML parsing */
  $result = $xml->xpath("/ul/li/ul/li/a") ; /* Use xpath to get the links from the submenu */
  $paths = array();
     
   foreach ($result as $key=>$value) { /* Iterate through link collection to parse out href attribute data */
       $paths [] = $value["href"] ; /*  SimpleXML  creates associative arrays for attributes; hence $value["href'] */

   }

newdru’s picture

what does this line do exactly:

$result = $xml-><code>xpath("/ul/li/ul/li/a") ; /* Use xpath to get the links from the submenu */

I imagine if finds elements that have the exact nested relationship as described correct?

man, i just assumed that simpleXML only worked with *pure* xml files. But html really is xml, so in that regard, i guess you can access any piece of the tag strucutre.

fwiw, i think what you're saying is that simpleXML is based on XPath - yes? i didn't know that. so in essence it is very similary to jquery shorthand notation (actually maybe jquery uses xpath???).

Which means, using simpleXML via php is similar to making php function like pseudo js - yes?

Would you have any inkling which one is more efficient (not in programmer effort but actual runtime processing) the regexp or your simpleXML / xpath method?

I take it you're a js guru?

thanks

brooklynwebguy’s picture

I imagine if finds elements that have the exact nested relationship as described correct?

That's exactly right. You can actually be very specific about what elements to grab. For instance /ul/li[@class="expanded"], will fetch all the li's under ul that are of the class "expanded." //li will grab all the li's at any level. XSLT and Xpath are very big, robust tools. They are not to everyone's taste. I have been working with them for 7 or 8 years now, so I'm quite comfortable with them.

Since Drupal's html (at least in this case) is xhtml, it is exposed to xml parsing. Badly formed html would not be.

I haven't used pseudo js or jquery so I can't answer your other questions knowledgeably.

I can only guess at your efficiency question. My gut says regular expressions are probably more efficient, since I would imagine ordinary string parsing is what SimpleXML is built on. Still, I think SimpleXML is alleged to be fast and PHP 5's XSL processing is also supposed to be efficient. Since in a case like this the xml chunk is relatively small, I don't think it makes much difference. Also, since I am working on a low traffic site at the moment, processing considerations are less compelling than coding effort and enjoyment.

I am not a js guru. All the Xpath and XSL work I have done is server side. Also with Javascript it is better to use the DOM since I am fairly sure Safari does not support XSL and Xpath though I think there are libraries for getting around that.