Last active
December 12, 2018 22:34
-
-
Save emb03/9f26bead91a70cedfe7d1f221dcbfefe to your computer and use it in GitHub Desktop.
Drupal 7: A block that displays posts if the day it was created is a prime number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* A block module that displays items | |
*/ | |
/** | |
* Implements hook_block_info(). | |
*/ | |
function prime_block_info() | |
{ | |
$blocks = array(); | |
$blocks['prime'] = array( | |
'info' => t('Prime Block'), | |
); | |
return $blocks; | |
} | |
/** | |
* Custom content function. | |
* | |
* Set beginning and end dates, retrieve posts from database | |
* saved in that time period. | |
* | |
* @return | |
* A result set of the targeted posts. | |
*/ | |
function prime_posts_contents() | |
{ | |
//Get today's date if it is a prime number. | |
$today = getdate(); | |
//Calculate the date five days ago. | |
$start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 5), $today['year']); | |
//Get all posts from five days ago to the present | |
$end_time = time(); | |
//Use Database API to retrieve current posts. | |
$query = db_select('node', 'n') | |
->fields('n', array('nid', 'title', 'created')) | |
->condition('status', 1) //Published. | |
->condition('created', array($start_time, $end_time), 'BETWEEN') | |
->orderBy('created', 'DESC') //Most recent first. | |
->execute(); | |
return $query; | |
} | |
//debug database fields | |
function prime_query_alter($query) | |
{ | |
if (module_exists('prime')) { | |
//dpm((string) $query); | |
//dpm($query->arguments()); | |
//dpm(entity_get_info()); //gets a list of all defined entities | |
//dpm(field_info_fields()); //gets a list of all defined fields | |
//dpm((string) $query); | |
//dpm(entity_get_info()); //gets a list of all defined entities | |
//dpm(field_info_fields()); //gets a list of all defined fields | |
} | |
} | |
/** | |
* Implements hook_block_view(). | |
* | |
* Prepares the contents of the block. | |
*/ | |
function prime_block_view($delta = '') | |
{ | |
switch ($delta) { | |
case 'prime': | |
$block['subject'] = t('Prime posts'); | |
if (user_access('access content')) { | |
// Use our custom function to retrieve data. | |
$result = prime_posts_contents(); | |
// Array to contain items for the block to render. | |
$items = array(); | |
// Iterate over the resultset and format as links. | |
foreach ($result as $node) { | |
$day = format_date("$node->created", 'custom', "j"); | |
//find days that are prime | |
if ($day > 1 && (($day%2 >= 1) && ($day%3 >= 1) && ($day%5 >= 1))) { | |
$items[] = array( | |
'data' => l($node->title, 'node/' . $node->nid) . " " . t('created on ' . $day) | |
); | |
if (is_numeric($day)) { | |
echo "Yes, $day is numeric, "; | |
} else { | |
echo "No"; | |
} | |
if ($day > 1 && (($day%2 >= 1) && ($day%3 >= 1) && ($day%5 >= 1))) { | |
echo "$day is a prime number "; | |
} else { | |
echo "$day is a not prime number"; | |
} | |
if (empty($items)) { | |
$block['content'] = t('No posts available.'); | |
} | |
// Pass data through theme function. | |
$block['content'] = theme( | |
'item_list', | |
array( | |
'items' => $items, | |
'day' => $day) | |
); | |
dpm($items); | |
} | |
} | |
return $block; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment