Problem: A flag or field was needed to display "NEW!" for new content.
Impact: Users won't be able to pinpoint at a glance the new content.
Solution:
In this solution, you'll need to have the Display Suite module, so make sure it is installed and enabled. To create the field go to Administration > Structure > Display Suite.
On this panel, in the top-right corner, click on the 'FIELDS' tab depicted in the image below to see the custom fields created with Display Suite.
![]() |
'FIELDS' tab selected within Display Suite section |
Click on the option "Add a code field".
Give your field a name in the Label textbox. You can call it "NEW field" or somesuch name.
Choose 'Node' from the list called Entities.
Under Field code, make sure 'Display Suite code' is chosen as the text format. Type in the following in the Field code text area to display "NEW!" for any content that has been added in the past week:
<?php
if (!empty($entity->created)) {
$now = new DateTime();
$interval = ($now->format('U') - $entity->created) / 86400;
if ($interval <= 7) print "NEW!";
}
?>
The first if statement checks to make sure that $entity->created for the node exists so that you don't throw any errors otherwise. The value of $entity->created is the date and time when the node was created in the form of a UNIX timestamp (the number of seconds elapsed since January 1, 1970 midnight).
To check the interval of time elapsed in days since the node was created ($interval), subtract the current date and time in Unix format—hence $now->format('U')—from the node-created date and divide by 86400. The number 86400 is 3600 (number of seconds per hour) multiplied by 24 (number of hours per day).
Finally, if the number of days elapsed is less than or equal to 7 days print "NEW!"
Click Save to create and save your new custom field.
Now you can use this field within your node page, node teasers or any other node display.
0 comments:
Post a Comment