1
  1. I have created a content type calls as "XYZ Inner Pages".
  2. Here I am adding a Fieldset and inside field set I am adding field type as Link.
  3. Now I create a page using XYZ "Inner Pages" Content Type, add content and the links.
  4. I am hiding the Links from Manage display because I want to show those on the right side of page.
  5. Now I am creating a view which will pull all the links from "XYZ Inner Pages" Content Type.
  6. Now I am showing the views block in the right section of "XYZ Inner Pages" Content Type page.
  7. This right block will list the link.
  8. Now I want to add active class to links so when I open any of the link from that list, then opened link from the list should be highlighted.
  • You can do it with jQuery and check if the current page url = the link url. If yes, add the active class. – No Sssweat Jan 7 '17 at 0:03
  • Thanks. I am planning to do the same but is there any other way to do? I wanted to avoid jQuery. – Manjiri Limaye Jan 9 '17 at 8:50
  • Now that I thought more about it, probably with a preprocress function. – No Sssweat Jan 9 '17 at 11:42
  • I am bit new in Drupal. Can you please guide me on this? – Manjiri Limaye Jan 10 '17 at 7:24
  • What Drupal version are you using 7 or 8? – No Sssweat Jan 10 '17 at 8:23
1

Example:

In my content type I added a link field called "link", so the machine name is field_link

In this link field for url I put http://example.com/node/3

I created a View called "Test".

In fields I added link.

In fields I added link again, but this time I change the formatter to URL, as plain text

enter image description here

We are going to use template_preprocess_views_view_fields

In your theme's template.php file you add

function mytheme_preprocess_views_view_fields($vars){

  if ($vars['view']->name == "test"){

    $field_link = strip_tags($vars['fields']['field_link_1']->content); // use strip_tags function to remove HTML markup.
    $field_link = str_replace('http://', '', $field_link); // remove http://
    $field_link = str_replace('www.', '', $field_link); // remove www.

    $node_url = $_SERVER['SERVER_NAME'] . '/' . request_path(); // see https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/request_path/7.x

    if ($field_link == $node_url){

      $vars['fields']['field_link']->content = '<div class="active-link">' . $vars['fields']['field_link']->content . '</div>';

    }

    $vars['fields']['field_link_1']->content = null; // hides the field_link_1 field

  }

}
  • replace mytheme with the actual name of your theme.
  • replace test with the actual name of your view.
  • replace field_link with the actual name of your field.

Then you add the CSS to your theme's style.css file, Example:

.active-link a { color: green; }
  • Remember to flush/clear the cache to see changes. – No Sssweat Jan 11 '17 at 13:08
  • yes sure. Will get back to you soon. – Manjiri Limaye Jan 12 '17 at 6:12
  • @ManjiriLimaye you're welcome, please accept the answer by clicking the ✅ checkmark, thx – No Sssweat Feb 1 '17 at 7:11

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.