Drupal 7 Block revisions

This snippet embeds a nodes content in a block & provides an edit link to that node, effectively creating a block with revision history. I've included a switch to display the edit link only when users are logged in as I use this snippet on sites that only allow administrators to login. You may want to change this so that the link is only displayed to specific roles.

  1. Create a node to contain your text & make a note of its ID.
  2. Copy the code below to a block and add the node ID you saved earlier.
  3. Make sure the "Contextual links" module is enabled.

Paste This code into a block:

<?php
$yourNodeID = 51;
if(function_exists('anthill_blockrevision')) 
print anthill_blockrevision($yourNodeID );
?>

Paste this into a template.php file:

function anthill_blockrevision($nid=null){
	if(!$nid) return;
	$output = "";
	$dest = drupal_get_destination();
	$dest = $dest['destination'];
	// Add a quick edit link within contextual menus
	$editlink = '
		<div class="contextual-links-wrapper contextual-links-processed" style="left:5px;right:none;width:33px;">
		<a class="contextual-links-trigger" href="/node/'.$nid.'/edit?destination='.$dest.'" style="background:none;background:#ccc;color:#0074BD;padding:0px;text-indent:2px;">edit</a></div>';
        // Very crude switch to display the edit link when users are logged in
	global $user;
	if($user->uid) $output .= $editlink;
	// Load the node & return its content
	$thisnode = node_load($nid);
	$output .= $thisnode->body['und'][0]['safe_value'];
	return $output;
}

Comments

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h2> <h4> <img> <div> <table> <tr> <td> <pre>
  • Lines and paragraphs break automatically.
  • Insert image: [img:node_id align=(left|right) size=(|fullLA|fullPreview|fullSQ|halfLA|halfPO|halfSQ|thirdSQ) action=(popup|link|frame) url=url style="margin:10px;" title=text description=text]

More information about formatting options

 
info@theanthillsocial.co.uk XHTML 1.0 Strict CSS2 WAI