0

My front page contains say 3 different views and each view has 10 rows. My requirement is Links which were not published to the frontpage the last time the user loaded it should be highlighted. I am using common template file for field shown on front page. (that is title file with anchor tag having link to some URL) Example in image :

enter image description here

I am having no idea how I can achieve this, I tried searching lot but I have idea that this can be possible with cookies.

Please suggest that will be very helpful !!

Thanks :)

UPDATE :

I have set nid values in cookie with below logic :

function custom_views_pre_render(&$view) {


  //frontpage highlight


  if (current_path() == "frontpage") {
    if (!empty($view->result)) {
      $nids = array();
      foreach ($view->result as $res) {
        $nids[] = $res->nid;
      }
      if(!isset($_COOKIE['FrontPage'])) {
        setcookie("FrontPage[$view->name]", implode(',', $nids), time() + (3600 * 24 * 5), '/');
      }
    }
}

Now I need to compare values of each row nid with in this array.

2

You could do this in a limited way where you store each item's nid and updated date in a cookie. The cookie would get updated each time the user loads the front page by JavaScript that reads the nid and date from the DOM.

Before the cookie is updated it is used to set a highlight class on any items that aren't set in the cookie.

In order for the cookie to not grow too large the items could be pruned by date if the front page only shows most recent.

For an demo take a look at https://jsfiddle.net/rnc2eLxr/2/

I've included the code below.

JS libraries:

HTML:

<body>
  <div class="view">
    <ul>
      <li class="link"><a href="http://google.com" data-nid="1" data-date="2017-01-01">Google</a></li>
      <li class="link"><a href="http://reddit.com" data-nid="2" data-date="2017-01-02">Reddit</a></li>
    </ul>
  </div>
</body>

Javascript:

// Get cookie value.
var linksString = $.cookie('frontLinks'),
  links = [];
if (linksString != undefined) {
  links = JSON.parse(linksString);
}

// Highlight any links not found in cookie.
$('li a').each(function(i, v) {
  var $link = $(v),
    tuple = {
      "date": $link.data('date'),
      "nid": $link.data('nid'),
    },
    match = findByNid(links, tuple.nid, true);
  if (!match) {
    $link.addClass('highlight');
    links.push(tuple)
  }
});

// Find array object by nid property.
function findByNid(inArr, nid, exists) {
  for (i = 0; i < inArr.length; i++) {
    if (inArr[i].nid == nid) {
      return (exists === true) ? true : inArr[i];
    }
  }
}

// Store links in cookie.
$.cookie('frontLinks', JSON.stringify(links))

CSS:

li a.highlight {
  border: red 1px solid;
}
1
  • Thanks for answer, I was thinking same way but not getting idea how to implement code as I am using field template, I thought can add code there but not sure. I already have nid in html of field. If possible please point towards some example code, that will be very helpful !
    – jas
    Apr 12 '17 at 5:02

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.