headerimage.module in Header image 6
Same filename and directory in other branches
headerimage.module Conditionally display an node in a block.
WHISH LIST: create CCK node type at install (see: http://drupal.org/node/82908) (maybe: http://drupal.org/node/101742)
File
headerimage.moduleView source
<?php
/**
* @file headerimage.module
* Conditionally display an node in a block.
*
* WHISH LIST:
* create CCK node type at install (see: http://drupal.org/node/82908) (maybe: http://drupal.org/node/101742)
*/
/**
* Implementation of hook_menu()
*/
function headerimage_menu() {
$items['admin/settings/headerimage'] = array(
'title' => t('Header Image'),
'description' => t('Control the Header Image.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'headerimage_settings_block_add',
),
'access arguments' => array(
'administer header image',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'headerimage.admin.inc',
);
$items['admin/settings/headerimage/list'] = array(
'title' => t('List'),
'page arguments' => array(
'headerimage_settings_block_add',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/settings/headerimage/settings'] = array(
'title' => t('Settings'),
'page arguments' => array(
'headerimage_settings_form',
),
'access arguments' => array(
'administer header image',
),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
$items['admin/settings/headerimage/edit/%'] = array(
'title' => t('Edit Header Image block'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'headerimage_settings_block_edit',
4,
),
'access arguments' => array(
'administer header image',
),
'type' => MENU_CALLBACK,
);
$items['admin/settings/headerimage/delete/%'] = array(
'title' => t('Delete Header Image block'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'headerimage_block_confirm_delete',
4,
),
'access arguments' => array(
'administer header image',
),
'type' => MENU_CALLBACK,
'file' => 'headerimage.admin.inc',
);
return $items;
}
/**
* Edit block data form
*/
function headerimage_settings_block_edit(&$form_state, $delta) {
$blocks = headerimage_get_blocks();
$form = array();
$form['delta'] = array(
'#type' => 'hidden',
'#value' => $delta,
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Block title'),
'#description' => t('The block name must be unique.'),
'#default_value' => $blocks[$delta],
'#required' => true,
);
$form['op'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['#redirect'] = 'admin/settings/headerimage';
return $form;
}
function headerimage_settings_block_edit_validate($form, &$form_state) {
$blocks = headerimage_get_blocks();
// Remove current blockname to prevent false error
unset($blocks[$form_state['values']['delta']]);
if (!empty($blocks)) {
// Check if name is unique
if (in_array($form_state['values']['title'], $blocks)) {
form_set_error('', t('Header Image block %s already exists. Please use a different name.', array(
'%s' => $form_state['values']['title'],
)));
}
}
}
function headerimage_settings_block_edit_submit($form, &$form_state) {
db_query("UPDATE {headerimage_block} SET title = '%s' WHERE delta = %d", $form_state['values']['title'], $form_state['values']['delta']);
drupal_set_message(t('Header Image block updated.'));
return 'admin/settings/headerimage';
}
/**
* Implementation of hook_perm().
*/
function headerimage_perm() {
return array(
'administer header image',
'maintain display conditions',
'view header image',
);
}
/**
* Implementation of hook_block().
*/
function headerimage_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$headerimage_blocks = headerimage_get_blocks();
foreach ($headerimage_blocks as $key => $name) {
$blocks[$key]['info'] = check_plain($name);
}
return $blocks;
break;
case 'configure':
return headerimage_block_configure($delta);
case 'save':
variable_set('headerimage_block_' . $delta . '_random_fallback', $edit['random_fallback']);
break;
case 'view':
if (user_access('view header image')) {
// select node from nodes assigned to this block
$nid = headerimage_select_node($delta);
$teaser = variable_get('headerimage_teaser', true);
// prepare block output
if (!empty($nid)) {
$node = node_load($nid);
$node = node_prepare($node, $teaser);
//use node teaser view
// mimic node_view
$node = node_build_content($node, $teaser, false);
$content = drupal_render($node->content);
if ($teaser) {
$node->teaser = $content;
unset($node->body);
}
else {
$node->body = $content;
unset($node->teaser);
}
node_invoke_nodeapi($node, 'alter', $teaser);
$block['content'] = theme('headerimage_block', $node, $teaser);
return $block;
}
}
break;
}
}
/**
* Select a node to be displayed in the block
*
* Node selection by (1)weight and (2)condition.
* If multiple conditions are present, any true condition will select the node.
* If no node is selected by the conditions and random fallback selection is
* enabled for the block, one of the available nodes will be selected at random.
*
* @param $block
* The headerimage block number ($delta)
*
* @return
* nid of the selected node
* null: no node selected
*/
function headerimage_select_node($block) {
$result = db_query("SELECT nid, conditions FROM {headerimage} WHERE block = %d ORDER BY weight, nid ASC", $block);
while ($header_image = db_fetch_object($result)) {
$conditions = unserialize($header_image->conditions);
$match = false;
// Store the nid in an array for the random selection fallback option.
$block_nids[] = $header_image->nid;
$selected_types = variable_get('headerimage_condition_types', array(
'nid' => 'nid',
));
foreach ($conditions as $type => $condition) {
if (!empty($condition) && !empty($selected_types[$type])) {
switch ($type) {
case 'nid':
$match = headerimage_eval_nid($condition);
break;
case 'url':
$match = headerimage_eval_url($condition);
break;
case 'taxonomy':
$match = headerimage_eval_taxonomy($condition);
break;
case 'book':
$match = headerimage_eval_book($condition);
break;
case 'nodetype':
$match = headerimage_eval_nodetype($condition);
break;
case 'php':
$match = drupal_eval($condition);
break;
}
}
if ($match) {
break;
}
}
if ($match) {
break;
}
}
if ($match) {
return $header_image->nid;
}
elseif (variable_get('headerimage_block_' . $block . '_random_fallback', 0) && count($block_nids)) {
return $block_nids[array_rand($block_nids)];
}
}
/**
* Evaluate nid condition
*
* Checks if current page is in list of nids
*
* @param $condition
* comma separated list of nids
*
* @return
* true: current page is in $condition
* false: if not
* null: current page is not a node
*/
function headerimage_eval_nid($condition) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nids = explode(',', $condition);
$match = in_array(arg(1), $nids);
}
return $match;
}
/**
* Evaluate url condition
*
* Check if current page is in selected paths
*
* @param $condition
* text with paths or '<frontpage>'. May contain wild cards.
*
* @return
* true: current page matches one of the $condition paths;
* false: if not
*/
function headerimage_eval_url($condition) {
$path = drupal_get_path_alias($_GET['q']);
$regexp = '/^(' . preg_replace(array(
'/(\\r\\n?|\\n)/',
'/\\\\\\*/',
'/(^|\\|)\\\\<front\\\\>($|\\|)/',
), array(
'|',
'.*',
'\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
), preg_quote($condition, '/')) . ')$/';
// Compare with the internal and path alias (if any).
$match = preg_match($regexp, $path);
if ($match) {
return true;
}
if ($path != $_GET['q']) {
$match = preg_match($regexp, $_GET['q']);
}
return $match;
}
/**
* Evaluate taxonomy condition
*
* Check if current page has selected taxonomy terms
*
* @param $condition
* array of taxonomy term tid's
*
* @return
* true: current page contains one of $condition's tags
* false: if not
* null: current page is not a node
*/
function headerimage_eval_taxonomy($condition) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
if ($node = menu_get_object()) {
foreach (array_keys($node->taxonomy) as $key) {
if (isset($condition[$key])) {
return true;
}
}
return false;
}
}
return null;
}
/**
* Evaluate book condition
*
* Return true if current node is a page of the book(s) selected in the condition
*
* @param $condition
* array of book root nid's
*
* @return
* true: current node is a page in $condition's books
* false: if not
* null: current page is not a node
*/
function headerimage_eval_book($condition) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
// check if current node is one of the condition pages (books)
$match = in_array(arg(1), $condition);
if ($match) {
return true;
}
// check if current page is part of book in the condition pages
$bid = db_result(db_query("SELECT bid FROM {book} WHERE nid = %d", arg(1)));
$match = in_array($bid, $condition);
}
return $match;
}
/**
* Evaluate node type condition
*
* Return true if type of current node is selected
*
* @param $condition
* array of selected node types
*
* @return
* true: current node type is selected
* false: if not
* null: current page is not a node
*/
function headerimage_eval_nodetype($condition) {
$match = FALSE;
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = menu_get_object();
$match = in_array($node->type, $condition);
}
return $match;
}
/**
* Implementation of hook_form_alter
*
* Add display conditions to header image node forms
*/
function headerimage_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, '_node_form') && isset($form['type']['#value']) && in_array($form['type']['#value'], variable_get('headerimage_node_type', array()), true) && user_access('maintain display conditions')) {
$form['headerimage'] = array(
'#type' => 'fieldset',
'#title' => t('Display conditions'),
'#description' => t('This node is displayed in a Header Image block when one of the conditions below are evaluated true.'),
'#collapsible' => true,
'#collapsed' => false,
'#weight' => -2,
);
$form['headerimage']['headerimage_block'] = array(
'#type' => 'select',
'#title' => t('Block name'),
'#description' => t('The block in which this node is displayed.'),
'#options' => headerimage_get_blocks(),
'#default_value' => $form['#node']->headerimage_block,
'#required' => true,
'#multiple' => false,
);
$form['headerimage']['headerimage_weight'] = array(
'#type' => 'weight',
'#title' => t('Condition weight'),
'#description' => t('Determines the order of in which the nodes are evaluated. The conditions of a node with a smaller weight will be evaluated first, those with a larger weight are evaluated later. A default image (the one displayed if the conditions of all other images fail) should have the largest weight: 10.'),
'#default_value' => $form['#node']->headerimage_weight,
'#delta' => 10,
);
$condition_types = variable_get(headerimage_condition_types, array(
'nid' => 'nid',
));
if (!empty($condition_types)) {
foreach ($condition_types as $type) {
if ($type != '0') {
$all_types = headerimage_get_condition_types();
$title = t("@name condition", array(
'@name' => $all_types[$type],
));
$name = 'headerimage_condition_' . $type;
switch ($type) {
case 'nid':
$form['headerimage'][$name] = array(
'#type' => 'textarea',
'#title' => $title,
'#description' => t("Enter node id's separated by comma's."),
'#default_value' => $form['#node']->{$name},
'#rows' => 4,
);
break;
case 'url':
$form['headerimage'][$name] = array(
'#type' => 'textarea',
'#title' => $title,
'#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog. %front is the front page.", array(
'%front' => '<front>',
)),
'#default_value' => $form['#node']->{$name},
'#rows' => 4,
);
break;
case 'taxonomy':
$options = array();
if (module_exists('taxonomy')) {
$vocab = taxonomy_get_vocabularies();
if (!empty($vocab)) {
foreach ($vocab as $v) {
$taxonomy = taxonomy_get_tree($v->vid);
if (!empty($taxonomy)) {
foreach ($taxonomy as $tag) {
$options[$tag->tid] = check_plain($tag->name);
}
}
}
}
if (!empty($options)) {
$description = t("One tag or multiple tags can be selected.");
}
else {
drupal_set_message(t("No vocabulary or tags defined. Please create vocabulary and tags or remove taxonomy from the !settings.", array(
'!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'),
)));
break;
}
$form['headerimage'][$name] = array(
'#type' => 'select',
'#title' => $title,
'#description' => $description,
'#default_value' => $form['#node']->{$name},
'#options' => $options,
'#multiple' => true,
);
}
else {
drupal_set_message(t("The taxonomy module is not enabled. Please enable the module or remove it from the !settings.", array(
'!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'),
)));
}
break;
case 'book':
if (module_exists('book')) {
$options = array();
$books = book_get_books();
if (count($books)) {
$description = t("One book or multiple books can be selected.");
foreach ($books as $key => $book) {
$options[$key] = $book['title'];
}
}
else {
$description = t("No books defined. Please create a book before using it as a condition.");
}
$form['headerimage'][$name] = array(
'#type' => 'select',
'#title' => $title,
'#description' => $description,
'#default_value' => $form['#node']->{$name},
'#options' => $options,
'#multiple' => true,
);
}
else {
drupal_set_message(t("The book module is not enabled. Please enable the module or remove it from the !settings.", array(
'!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'),
)));
}
break;
case 'nodetype':
$nodes = node_get_types();
foreach ($nodes as $node) {
$nodetype[$node->type] = check_plain($node->name);
}
$form['headerimage'][$name] = array(
'#type' => 'select',
'#title' => $title,
'#description' => t('Select one or multiple node types'),
'#default_value' => $form['#node']->{$name},
'#options' => $nodetype,
'#multiple' => true,
);
break;
case 'php':
$form['headerimage'][$name] = array(
'#type' => 'textarea',
'#title' => $title,
'#description' => t("Enter PHP code between <?php ?> tags. Note that executing incorrect PHP-code can break your Drupal site."),
'#default_value' => $form['#node']->{$name},
'#rows' => 4,
);
break;
}
}
}
}
}
}
/**
* Return a form with a list of Header Image nodes
*
* Nodes assigned to Header Image block $delta are listed with weight and
* edit link
*
* @param $delta
* Header Image block delta
*
* @return
* form array
*/
function headerimage_block_configure($delta) {
$result = db_query("SELECT title, weight, n.nid FROM {headerimage} hi JOIN {node} n ON n.nid = hi.nid AND hi.block = %d ORDER BY weight, nid ASC", $delta);
// Table with image set details
$rows = array();
while ($data = db_fetch_object($result)) {
$rows[] = array(
'node' => check_plain($data->title),
'weight' => $data->weight,
'edit' => l(t('Edit'), "node/{$data->nid}/edit"),
);
}
if (empty($rows)) {
$rows[] = array(
array(
'data' => t('No Header Image nodes assigned to this block.'),
'colspan' => '3',
),
);
}
$header = array(
t('Node'),
t('Weight'),
array(
'data' => t('Operation'),
'colspan' => '1',
),
);
$output = theme('table', $header, $rows, array(
'id' => 'imageblock',
));
// Pack the tabel in form as markup text.
$form['headerimage']['description'] = array(
'#prefix' => '<p>',
'#value' => t('The table below contains the nodes which will be displayed in this block when there condition evaluates to true.'),
'#suffix' => '</p>',
);
$form['headerimage']['table'] = array(
'#prefix' => '<div>',
'#value' => $output,
'#suffix' => '</div>',
);
$form['headerimage']['random_fallback'] = array(
'#type' => 'checkbox',
'#title' => t('Enable a random fallback selection'),
'#default_value' => variable_get('headerimage_block_' . $delta . '_random_fallback', 0),
'#description' => t('If no node is selected by the conditions, select a random node from those assigned to this block.'),
);
return $form;
}
/**
* Implementation of hook_nodeapi
*/
function headerimage_nodeapi(&$node, $op) {
if (!empty($node->type) && in_array($node->type, variable_get('headerimage_node_type', array()), true)) {
switch ($op) {
case 'update':
db_query("DELETE from {headerimage} WHERE nid = %d", $node->nid);
case 'insert':
// Pack all conditions into one array
$conditions = variable_get(headerimage_condition_types, array(
'nid' => 'nid',
));
if (!empty($conditions)) {
foreach ($conditions as $condition) {
if ($condition != '0') {
$name = 'headerimage_condition_' . $condition;
$conditions[$condition] = $node->{$name};
}
}
}
db_query("INSERT INTO {headerimage} (nid, block, weight, conditions)\n VALUES (%d, %d, %d, '%s')", $node->nid, $node->headerimage_block, $node->headerimage_weight, serialize($conditions));
break;
case 'prepare':
// Load data from database if node is edited
$result = db_fetch_object(db_query("SELECT * from {headerimage} where nid = %d", $node->nid));
$node->headerimage_block = $result->block;
$node->headerimage_weight = $result->weight;
$conditions = unserialize($result->conditions);
if (!empty($conditions)) {
foreach ($conditions as $condition => $value) {
$name = 'headerimage_condition_' . $condition;
$node->{$name} = $value;
}
}
break;
case 'delete':
db_query("DELETE from {headerimage} WHERE nid = %d", $node->nid);
break;
}
}
}
/**
* Implementation of hook_help
*/
function headerimage_help($path, $arg) {
switch ($path) {
case 'admin/help#headerimage':
// Determine the status of the installation
$type_is_set = count(variable_get('headerimage_node_type', array())) ? t('(DONE)') : t('(TO DO)');
$block_is_created = count(headerimage_get_blocks()) ? t('(DONE)') : t('(TO DO)');
foreach (headerimage_get_blocks() as $delta => $name) {
$block_has_nodes = db_result(db_query("SELECT nid FROM {headerimage} WHERE block = %d", $delta));
if ($block_has_nodes) {
break;
}
}
$block_has_nodes = $block_has_nodes ? t('(DONE)') : t('(TO DO)');
foreach (headerimage_get_blocks() as $delta => $name) {
$block_in_region = db_result(db_query("SELECT region FROM {blocks} WHERE module = '%s' AND theme = '%s' AND delta = %d", 'headerimage', variable_get('theme_default', 'garland'), $delta));
if ($block_in_region) {
break;
}
}
$block_in_region = $block_in_region ? t('(DONE)') : t('(TO DO)');
$output = "<p>" . t('Header Image allows you to to display an image on selected pages. It can display one image at the front page, a different one at FAQ pages and yet another at all other pages.') . "</p>\n";
$output .= "<p>" . t('Visibility of each image, included in a node, can be determined by node ID, path, taxonomy, book, node type or custom PHP code. Header Image uses an arbitrary node type.') . "</p>\n";
$output .= "<p>" . t('Multiple images (nodes) can be displayed in one block, with each image having its own conditions. Using a weight per node the order of selection can be controlled.') . "</p>\n";
$output .= "<h2>" . t('Installation') . "</h2>\n";
$output .= "<p>" . t('<ol>
<li>Set up <a href="!permissions">permissions</a>.</li>
<li>Optionally <a href="!create_node_type">create a node type</a> which will contain the header image.</li>
<li><a href="!set_node_type">Select a node type</a> to be used as Header Image. %status_type</li>
<li><a href="!create_header_image_block">Create a Header Image block</a>. %status_create</li>
<li>Create Header Image nodes. Select a Header Image block and set display conditions for each node. %status_assign</li>
<li><a href="!assign_header_image_block">Assign the Header Image block</a> to a region. %status_region</li>
</ol>', array(
'!permissions' => url('admin/user/permissions'),
'!create_node_type' => url('admin/content/types/add'),
'!set_node_type' => url('admin/settings/headerimage/settings'),
'%status_type' => $type_is_set,
'!create_header_image_block' => url('admin/settings/headerimage'),
'%status_create' => $block_is_created,
'!assign_header_image_block' => url('admin/build/block'),
'%status_assign' => $block_has_nodes,
'%status_region' => $block_in_region,
)) . "</p>\n";
$output .= "<p>" . t('For more information please read the <a href="@handbook">configuration and customization handbook</a> on Header Image.', array(
'@handbook' => 'http://drupal.org/node/201426/',
)) . "</p>\n";
return $output;
}
}
/**
* Return all condition types available
*
* @return
* array of condition types
*/
function headerimage_get_condition_types() {
return array(
'nid' => t('Node ID'),
'url' => t('URL'),
'taxonomy' => t('Taxonomy'),
'book' => t('Book'),
'nodetype' => t('Node type'),
'php' => t('PHP'),
);
}
/**
* Return all Header Image blocks
*
* @return
* array of Header Image blocks
*/
function headerimage_get_blocks() {
static $blocks;
if (!isset($blocks)) {
$blocks = array();
$result = db_query("SELECT * FROM {headerimage_block}");
while ($block = db_fetch_object($result)) {
if (!empty($block)) {
$blocks[$block->delta] = $block->title;
}
}
}
return $blocks;
}
/**
* Implementation of hook_theme()
*/
function headerimage_theme() {
return array(
'headerimage_block' => array(
'template' => 'headerimage-block',
'arguments' => array(
'node' => NULL,
'teaser' => NULL,
),
),
);
}
/**
* Process variables to format the headerimage block.
*
* $variables contains:
* - $node
* - $teaser: TRUE = display node as teaser; FALSE = display full node
*
* @see headerimage-block.tpl.php
*/
function template_preprocess_headerimage_block(&$variables) {
$node = $variables['node'];
$teaser = $variables['teaser'];
$variables['unpublished'] = !$node->status;
if ($teaser && isset($node->teaser)) {
$variables['content'] = $node->teaser;
}
else {
$variables['content'] = $node->body;
}
}
Functions
Name | Description |
---|---|
headerimage_block | Implementation of hook_block(). |
headerimage_block_configure | Return a form with a list of Header Image nodes |
headerimage_eval_book | Evaluate book condition |
headerimage_eval_nid | Evaluate nid condition |
headerimage_eval_nodetype | Evaluate node type condition |
headerimage_eval_taxonomy | Evaluate taxonomy condition |
headerimage_eval_url | Evaluate url condition |
headerimage_form_alter | Implementation of hook_form_alter |
headerimage_get_blocks | Return all Header Image blocks |
headerimage_get_condition_types | Return all condition types available |
headerimage_help | Implementation of hook_help |
headerimage_menu | Implementation of hook_menu() |
headerimage_nodeapi | Implementation of hook_nodeapi |
headerimage_perm | Implementation of hook_perm(). |
headerimage_select_node | Select a node to be displayed in the block |
headerimage_settings_block_edit | Edit block data form |
headerimage_settings_block_edit_submit | |
headerimage_settings_block_edit_validate | |
headerimage_theme | Implementation of hook_theme() |
template_preprocess_headerimage_block | Process variables to format the headerimage block. |