Is there some way we could give an active class to links in summary attachments? I am mostly thinking of when one replicates the old Alpha Pager functionality with a glossary attachment. This works great, but I can't find a way to highlight the active link - would like for "G" to be highlighted if I am viewing the "G" page.

Comments

dawehner’s picture

Status: Active » Needs review
StatusFileSize
new1.63 KB

Here is a initial patch. I'm not sure whether this is fine and good to use for the themers :)

dafeder’s picture

StatusFileSize
new1.65 KB

Hadn't realized l() gives active classes automatically, duh! It almost works, but the urls are coming in already formatted with a "/" at the beginning, so l() doesn't format them correctly. Here is a corrected patch... This is pretty hacky - I think it provides important functionality but perhaps there's a more graceful way to accomplish this "higher up" in the views code?

dafeder’s picture

I actually ended up putting a template in my theme to avoid hacking the module. Here is the template for anyone interested.

dafeder’s picture

Oh, another thing... this doesn't work on the default argument. So, if you are using the typical glossary attachment, it defaults to "A". If you go to /yourview, it won't produce an active class for the "A" link, you have to go to /yourview/a to get that.

merlinofchaos’s picture

I don't really like using l() in a template. Can we just have the preprocess do the active check and throw that in a classes variable instead?

dafeder’s picture

I'm still not really comfortable working in the whole preprocess system. That sounds right but I'm not sure how it would work exactly. A preprocess function in the theme? Or are you asking about a potential patch to the module?

Christopher Herberte’s picture

Subscribe.

Meanwhile, i've used jQuery for a temporary fix. Works only with AJAX disabled (reads url) Maybe this helps.

Views default glossary:

   $(".view-glossary.view-display-id-attachment span a").each(function(){
     if($(this).attr("href") == location.pathname) {
       $(this).addClass('my-class');
     }
   });

or custom glossary attachment, I styled mine like primary tabs.

   $(".view-myview .views-summary li a").each(function(){
     if($(this).attr("href") == location.pathname) {
       $(this).parent().addClass('active');
     }
   });
meecect’s picture

+subscribe

I would like to see an easy way to do this for a block item's active trail. The 'a' tag appears to get the active class, but the 'li' tag in a block doesn't get the active-trail class so it's hard to style bullet items for the currently active page.

In the meantime, I used a slightly modified version of the jquery code from #7.

<script>
$(document).ready(function() {
  $("li").each(function(){
    if($(this).find('a').attr("href") == location.pathname) {
      $(this).addClass('active-trail');
    }
  });
});
</script>

I'd really like to do this the drupal way, but at the same time, it's hard not to notice that doing it with jquery is short, understandable and fast. The drupal way apparently involves the interaction of several modules, a tangled webs of template files, template preprocess functions, the stacked nature of Drupal API's and on and on.

It's things like this that really make me question my investment in Drupal, which seems sometimes to be a towering mess of modules, functions, overrides, templates and hundreds of SQL queries to generate a single page.

dboulet’s picture

Version: 6.x-2.6 » 6.x-2.x-dev
StatusFileSize
new1.91 KB

Here's my stab at it, using a preprocess function and modifying the template.

crashtest_’s picture

dboulet - your patch was pretty much right on for formatted summary view, however it also needs to apply to the unformatted summary view, or the default glossary view doesn't seem to work (at least for me).

I added to your patch to enable this to work with unformatted.

merlinofchaos’s picture

Status: Needs review » Fixed

Great work! Committed to all branches.

dboulet’s picture

Great! Thanks for the help CrashTest_.

dafeder’s picture

Thanks!

nauthiz693’s picture

The patch listed in comment 10 worked great for me, but it didn't handle the situation of a default. So for my view, I wanted the first item to have the 'active' class in the default case. I did this by implementing a preprocess function in my theme:

function MYTHEMENAME_preprocess_views_view_summary(&$vars) {
  if (sizeof($vars['classes']) == 0) {
    $vars['classes'][0] = 'active';
  }
}

I think you may also need to have your own template file overriding the default views one (views-view-summary.tpl.php) for drupal to find and call your function. In my case, I overwrote it for a specific view (so I added the file views-view-summary--VIEWNAME.tpl.php) because I also wanted the active class on the containing list element not the link.

MichaelP’s picture

Status: Fixed » Needs review
StatusFileSize
new1.56 KB

I experienced problems with differences between the url comparison on a Catalan site with aliases like "Alòs d'Isil".

This follow on patch runs the urls to be compared through rawurldecode (perhaps there is a better way?).

dawehner’s picture

Status: Needs review » Needs work

I would like to have documented why here rawurldecode is used....

MichaelP’s picture

Replaces patch at #15 using drupal_urlencode and only on incoming path. Comment added.

This resolves my case as in:


$vars['rows'][$id]->url  = /map/mercat/producte/Al%C3%B2s%20d%27Isil

base_path() . drupal_get_path_alias($_GET['q']) = /map/mercat/producte/Alòs d'Isil

MichaelP’s picture

Status: Needs work » Needs review
dawehner’s picture

Status: Needs review » Needs work

Sorry but why?
You only describe what you are doing, which is already covered nice by the function name

MichaelP’s picture

@dereine:

I made the patch at #558602-17: Class for active links in summary view attachments because:

I have summary lists of taxonomy term arguments. I noticed that the "active link" functionality was only being set when the "active link" was a single word with no space etc.

Upon investigation, I saw that the comparison to find the active link:

if ($vars['rows'][$id]->url == base_path() . $_GET['q'] || $vars['rows'][$id]->url == base_path() . drupal_get_path_alias($_GET['q']))

was trying to compare, for example:

"/map/mercat/producte/Al%C3%B2s%20d%27Isil" == "/map/mercat/producte/Alòs d'Isil"

(substituting the comparison with an actual example obtained from an xdebug session).

On the one side, the value has already been passed through drupal_urlencode in the url() call, so I perform a drupal_urlencode on the other side in order to compare like with like.

Does that make any sense at all?!!

MichaelP’s picture

Status: Needs work » Needs review
merlinofchaos’s picture

dereine means that the why should be in the comments in the code.

Whenever the reason for something is unclear or ambiguous, that is an opportunity to apply good code comments. =)

MichaelP’s picture

This replaces patch at #558602-17: Class for active links in summary view attachments, hopefully with better code comments.

Patience of all concerned much appreciated.

MichaelP’s picture

This replaces patch at #558602-17: Class for active links in summary view attachments, hopefully with better code comments.

Patience of all concerned much appreciated.

dboulet’s picture

Link to patch above doesn't work for some reason.

http://drupal.org/files/issues/views-Drupal-6--2_558602_drupal_urlencode...

owahab’s picture

None of these patches fix the issue I currently have: with one or more exposed filters, the active class doesn't get attached to the summary attachment because if I have exposed the body field filter, URLs will look like /map/mercat/producte/Al%C3%B2s%20d%27Isil?body= while the patch #10 (currently in Views 2.10) and the latest one in #25 will both ignore the filter hence this fix won't work.

I will try to add a patch as soon as I get it fixed.

merlinofchaos’s picture

I'm not sure how filters tie in to an active class?

owahab’s picture

Well Earl, the method currently used to evaluate the status of a link being active or inactive depends on comparing the $_GET['q'] to the link's URL and ignores the other parameters in $_GET.

Without exposed filters:
$vars['rows'][$id]->url = '/example/myview/A'
$_GET['q'] = '/example/myview/A'
Result: the link has the class active.

With exposed filters (body field in this example):
$vars['rows'][$id]->url = '/example/myview/A'
$_GET['q'] = '/example/myview/A?body=ape'
Result: the link doesn't have the class active though it should.

merlinofchaos’s picture

$_GET['q'] = '/example/myview/A?body=ape'

$_GET['q'] should never include query parameters.

owahab’s picture

Sorry, a typo.

With exposed filters (body field in this example):
$vars['rows'][$id]->url = '/example/myview/A?body=ape'
$_GET['q'] = '/example/myview/A'

merlinofchaos’s picture

Ok, so we need to add a parse_url() in there. Got it.

ducdebreme’s picture

I am sorry, it doesn't work with multilingual websites.

Here is the reason:

$vars['rows'][$id]->url = /mysite/de/my_favorites/by_language/9/de
base_path()             =            my_favorites/by_language/9/de
$_GET['q']              = /mysite/

==> the de is missing and thus the link is not getting highlighted

Proposed solution: use the url() function instead of concatenating the url fragments. It takes the language into account.
Use this ...

    if ($vars['rows'][$id]->url == url($_GET['q']) || $vars['rows'][$id]->url == url( drupal_get_path_alias($_GET['q']) ) ) {
      $vars['classes'][$id] = 'active';
    }

instead of this

    if ($vars['rows'][$id]->url == base_path() . $_GET['q'] || $vars['rows'][$id]->url == base_path() . drupal_get_path_alias($_GET['q'])) {
      $vars['classes'][$id] = 'active';
    }
dawehner’s picture

Status: Needs review » Needs work

So

ducdebreme’s picture

esmerel’s picture

Status: Needs work » Closed (works as designed)

No work done for more than 3 months

willhowlett’s picture

Subscribe

webnicola’s picture

Subscribe

No work width Drupal 6.20 Views 6.x-2.12

Ok sorry, see comment #32!

dgastudio’s picture

any update on this?

gawas.sachin’s picture

How Do I apply class "active" to default argument of attachment. ?

stolzenhain’s picture

subscribe

If just a quick frontend fix is needed, the active link class could also be set with a simple jquery command, that compares the href attribute to the actual URL.

Oh I see, someone already proposed that on #7

Carl Johan’s picture

Status: Closed (works as designed) » Needs review
StatusFileSize
new1.23 KB

Reviving :)

Created a proper patch based on #32 which fixes this for me (multilingual site)

nickbumgarner’s picture

Issue summary: View changes

Just an FYI for anybody looking for this on the 7.x branch. I found a working patch here. https://www.drupal.org/node/2159967

izmeez’s picture

Patch in comment #41 applies without difficulty to latest views-6.x-2.26

chris matthews’s picture

Status: Needs review » Closed (outdated)

The Drupal 6 branch is no longer supported, please check with the D6LTS project if you need further support. For more information as to why this issue was closed, please see issue #3030347: Plan to clean process issue queue

dasSouvik93’s picture

How will I achieve the same if the glossary view is in AJAX enabled? I am using Drupal 9.