Drupal theme preprocessing function - primary links

I recently wrote a theme function to add a class to my main links, which works great. Then I wrote some css classes to create these links with custom background images. Excellent. Now there is a problem, the link text for the main links is displayed. This is usually not a problem, as I would just wrap it in a regular β€œhide” class. For instance:

<span class="hide"><a href="#">Link Text</a></span> 

So my question is: how can I scroll through primary links and wrap text w / a <span> , as my example? Here is my theme function that I used to add my classes.

 function zkc_preprocess_page(&$vars, $hook) { // Make a shortcut for the primary links variables $primary_links = $vars['primary_links']; // Loop thru the menu, adding a new class for CSS selectors $i = 1; foreach ($primary_links as $link => $attributes){ // Append the new class to existing classes for each menu item $class = $attributes['attributes']['class'] . " item-$i"; // Add revised classes back to the primary links temp variable $primary_links[$link]['$attributes']['class'] = $class; $i++; } // end the foreach loop // reset the variable to contain the new markup $vars['primary_links'] = $primary_links; } 
+4
source share
4 answers

Is there a jQuery option?

Try something like this:

 $(document).ready(function(){ $('#primary li a') .wrapInner('<span class="hide">' + '</span>'); }); 

EDIT:

Or, if you want to go to Drupal, put this guy in your foreach loop:

$link['title'] = '<span class="hide">' . check_plain($link['title']) . '</span>';

+2
source

If you only want to hide the link text, why don't you just use something like text-indent: -9999px; ?

+2
source

The correct methods for changing the display of menu links can be done on the theming layer. You were on the right track using preprocessing, but there is a bit more.

Refer to this for more information:

http://drupal.org/node/352924#comment-1189890

http://api.drupal.org/api/function/theme_links/6

+1
source

Typo?

$primary_links[$link]['$attributes']['class'] = $class;

Must read;

$primary_links[$link]['attributes']['class'] = $class;

0
source

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


All Articles