I use a panel with views on the home page of my drupal site (workaround.org). But I would like to put a "new" label in front of any article that was created or edited in the past 2 weeks. The view field "Node: has new content" did not help me because it will flag any content as "new" for anonymous users. Instead I just want to show anonymous users what content has been added recently.
3 Answers
I have some suggestions, ranked below sort of from simple to complex.
- You can simply add a static text field to the view, write "New" in the text field, and then filter the view to contain only content from the period you want to have the "New" label on them.
- If you want the same list to also display more content, create an
attachment
display in the same view, override the text field and the date filter, and set the display to attach after the first one. - Use the
Theme information
in the view to directly override the views output. Find the content id in the template, load the node, and check the creation date. - Render the content through a view mode instead of on a field by field basis. In the the node-template, you can now check the creation date of the node and output the label. This solution would possibly benefit from an added 'per-view-mode' theme suggestion.
You can make use of the Views PHP module to write a small custom code that will check if node creation date is within the last two weeks, and then print a label.
Something like this (I'm using [pseudo field names], you need to check which is correct ones)
$show_date = time() - (60*60*24*7*2); //time minus 2 weeks
if ($data->[node_updated_date] <= $show_date) {
print '<span class="new-label">New</span>';
}
To see which field names to use, you can get the content of the $data object by using the devel modules' dpm($data);
It is adviced not to use the Views PHP module too much, but I never ran into problems using it for small snippets like this.
-
Quentin, I habitually down-vote any answers that advocate the use of eval, because it's usually recommended without explaining or understanding the consequences. I will reverse the vote if your answer also explains why it's a bad to idea. You could, for example, link to this relevant question. What are the downsides of using custom php code in blocks nodes views arg Nov 20, 2012 at 9:02
Views by default provide this functionality for the new content
You just need to select the field(Content: Has new content) in views, it will add a div with unique class and with text "new"
it works with individual user, means if user is watching it first time then only for that user new tag will appear
I hope this will help you. Thanks
-
The filter "Content: Has new content" is provided by the history module (from Drupal core). Jun 3, 2021 at 15:58