📜 ⬆️ ⬇️

Dances with a tambourine, part 2 - tabbed menu in Drupal

Actually, the continuation of this Habratopik , as they say, “at the request of the workers”. And before I begin, I bring a bunch of sincere thanks to the habrousers who were not lazy to send bug reports and thus helped to “lick” the result. Let there be no limit to perfection, and I still have to fix a couple of rough edges - but in any case, it is better than it was originally. And now I will tell you how I pulled the whole kitchen on the menu system in Drupal.

To begin with - we will formulate the task. We need to:
  1. the menu item becomes active when you select the appropriate page (well, this is a no brainer, and the corresponding class on the link is automatically attached by Drupal)
  2. each menu item has a unique ID for hanging styles on it
  3. the menu item remained active when choosing any node from this page or when choosing a print version (the link to which has the form "print-page name")
  4. By default, on other pages that are not related to the catalog, the menu "Apartments" should be active.

Well, to make it all look, respectively, like this:
image

Create a new menu (“Management” - “Site Design” - “Menu”). We push the necessary links into it. Go to “Management” - “Site Design” - “Blocks” and substitute a block with a new menu in one of the visible areas (do not forget to check if this area is displayed in the template, and if the working divs of the area correspond to the ID, that spelled out in the style sheet). We get a "naked" menu, which we will now customize.

So, since task 1 is solved out of the box by Drupal and is already registered in our style sheet from the previous topic, let's move on to point 2. How can I attach unique attributes to each menu item, for example, ID? It's very simple - we put the Menu Attributes module, turn it on on the module management page, and now, going into editing the menu item, we will see a separate section for adding attributes. In our case, we assign an ID to each link: for the menu “Apartments” - “apartments”, for “Houses and cottages” - “houses”, for “Land plots” - “ground”, and for “Industrial real estate” - “ business ". And after this procedure, we will see that now the menu at least on the pages of directories (generated with Views, if anything) looks like it should.
')
Let's go further. Now we need to keep the menu item active in the case of selecting any node from the appropriate section of the directory, or the version for printing the page. I must say that I have every type of property - it is a separate type of node, with the appropriate parameters, therefore. making the binding, I will check exactly the type of the node, although in general it can be anything (taxonomy, CCK field ... but how many). We climb into the folder of our template and open the file template.php, adding the following lines to it:

 function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) { $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf')); if (!empty($extra_class)) { $class .= ' '. $extra_class; } preg_match("/node\/\d+/", request_uri(), $nid); // ,    ,      .    preg_match - -  if (!empty($nid[0])) { $nid[0] = ereg_replace("node/", "", $nid[0]); $tmp = node_load($nid[0]); } if (ereg('apartments',$link) and !(preg_match("/houses|ground|business/", request_uri()) or preg_match("/house|place|for_business/", $tmp->type) )) {$in_active_trail = true;} if ( ereg('/houses',$link) and (ereg("/houses", request_uri()) or $tmp->type=='house') ) {$in_active_trail = true;} if ( ereg('/ground',$link) and (ereg("/ground", request_uri()) or $tmp->type=='place') ) {$in_active_trail = true;} if ( ereg('/business',$link) and (ereg("/business", request_uri()) or $tmp->type=='for_business') ) {$in_active_trail = true;} if ($in_active_trail) { $class .= ' active-trail '; } return '<li class="'. $class .'">'. $link . $menu ."</li>\n"; } 

Wow ... scared? And do not be afraid. Now I will explain to you what is happening here. By itself, the function overrides the standard Drupal menu output. Since the global variable $ node here (FIG knows why, there was no time to figure it out) is not visible, then the number of the active node, if there is one, we get from the current URL with a regular expression. And if there is one, load the parameters of the node.

Then we check several conditions - just whether the address of the current page is contained in the menu link (this is necessary so that print versions containing the page name are processed correctly), and if not, whether the type of the current node corresponds to the corresponding page for the real estate type. Everything! :)

I know that this is certainly not the most beautiful, fast, scalable, etc. way to. But, first, this is my first more or less serious project on drupal, and, secondly, I was promised that the structure of the menu and materials will continue to remain unchanged. Although, in principle, if you correctly approach the naming of types of materials and pages, so that they all correspond to a specific pattern, then with the same regulars this task can be extrapolated to more serious conditions ...

Source: https://habr.com/ru/post/52601/


All Articles