HowTo: Create a Random, Single, Full Node on Your Home Page (4.7.x/5.x)
Description:
This HowTo: provides basic instructions to display, through the Views module, one random piece of full content from everything published in the last two weeks to show up on the front page of your site every time someone loads the site's front page. (Anonymous users constrained by site wide Cache settings.)
Additionally, as Views does not currently update the {node_counter} .totalcount or .daycount in this instance, a (kludge) mechanism is provided to increment both fields for the single Full Node that is being displayed. Unfortunately the kludge to update the totals does not work correctly for anonymous users if your site has cache turned on. You will get one totals update per caching. When (If?) I find the mechanism to disable cache for a View, I'll update here with the information.
Credits:
This page was created by the knowledge of mkalbere, coreyp_1, and dvessel. I just summarized all the parts and did the write up. -MJT
# # # Random Front Page # # #
A) Create a New View
4.7.x Home >> Administer >> views : Add
( /admin/views/add )
5.x Home >> Administer >> Site building >> Views: Add
( /admin/build/views/add )
B) Basic Information Block
- Add "Name:" and "Description:"
- UnCheck all boxes in "Access:"
C) In the "Page" Block
- check the "Provide Page View" box
- in "URL:" enter a url
- in "View Type:" select "Full Nodes"
- uncheck "Use Pager"
- check "Breadcrumb trail should not include "Home""
- set "Nodes per Page:" to 1
D) Filters Block (Add Filter)
- "Node: Published" "Equals" "Yes"
- "Node: Front Page" "Equals" "Yes"
- "Node: Created Time" "Is Greater Than" "now" "-1209600"
Optional [to include or exclude specific node type(s)]:
- "Node: Type" "Is None Of" "page" <<== Select what works for your site
E) Sort Criteria Block (Add Criteria)
- "Random"
F) Click "Save"# # # Update Counters # # #
Findings:
After exploring the var_dump($GLOBALS) given by Dvessel ( http://drupal.org/node/160921#comment-254323 ), there is seemingly no global var that holds the displayed Node ID(s) accessible by PHP within a View. Both 4.7 and 5.1 have this same problem and the below solution (ugly hack) works for both.
Constraints:
- Only tested for a View returning a single node in the Page format.
- has less than stellar error handling (actually none).
To find the NID(s):
A) hack views.module
- Add as the first two lines of "function theme_views_view_nodes("
<code>
global $whatisthenodeid;
$whatisthenodeid = $nodes;To update the counters:
B) create a View such as the one above , then:
== In your View's Page >> Footer
- set "Input format" to "PHP code"
- Add the code:
<code>
<?php
global $whatisthenodeid;
$nodenid = $whatisthenodeid[0]->nid;
db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), $nodenid);
?># # # Default front page # # #
Update your "Default front page:" with the View's "URL" from above.
4.7.x Home >> Administer >> settings : General settings
( admin/settings )
5.x Home >> Administer >> settings >> Site information
( /admin/settings/site-information )
# # # End # # #
