tags: drupal


Cautionary Backup Tale

Here is the news:

I thought it would be nice to be able to browse the old posts from work in a sensible way. Rather than having to go back 600 pages on the blog, or browsing through tags. Tags are fine in Drupal if you don't want to navigate to the next / previous node while reading a story, and are really good for quickly getting to content now I've streamlined the web site so much (lots of tagging work still to do, there, though).

So I tried a few solutions...  ...read more »





template.php

For overriding various theme functions.

<?php
/***
* template.php
*
* This file contains functions for over-riding the default theme functions
* in Drupal core and modules (look at the API documentation for more info).
* The functions don't actually _do_ anything, except pass the variables
* available to phptemplate for use in the *.tpl.php files.
*
* Add similar 'stub' functions to override other default theme functions.
*/

  // variables for fixing topics to 'on_topic' only:
function _phptemplate_variables($hook, $vars) {
 
// if ($hook == 'page') {
  // }
 
if ($hook == 'node') {
  }
  if (
module_exist('taxonomy') && $vars['node']->nid) {
    foreach (
taxonomy_node_get_terms($vars['node']->nid) as $term) {

     
// Change '123' with the vocabulary id.
     
if ($term->vid == '1') {
       
$vars['node_terms'] = $vars['node_terms'] . eregi_replace('[^a-z0-9]', '-', $term->name);
      }
    }
  }
 
// enable me to add service links to templates:
 
if (module_exist('service_links')) {
       
$vars['service_links'] = theme('links', service_links_render($vars['node']));
  }
  return
$vars;
}

 
// move rss icon to centre and give it css class:
function skipMain_feed_icon($url) {
  return
_phptemplate_callback('feedicon', array('url' => $url));
}

function
skipMain_xml_icon($url) {
  return
_phptemplate_callback('feedicon', array('url' => $url));
}

// Quotes module formatting:
function skipMain_quotes_quote($node) {
  return
_phptemplate_callback('quotes', array('node' => $node));
}

// Today's Music title
function flexinode_form_alter($form_id, & $form) {
  if (isset (
$form['type']) && $form['type']['#value'] === 'flexinode-4' && $form['type']['#value'] . '_node_form' == $form_id) {
    global
$user;
           
$form['title']['#default_value'] = t('Today\'s Music') . " " . date('d-m-Y');
  }
}

// upcoming event item
function skipMain_event_upcoming_item($node) {
  return
_phptemplate_callback('event-item', array('node' => $node));
}

//next - prev links (for 'listenings' nodes)
function next_prev($current_nid, $type, $button_type, $label, $class) {
  
$tid = db_result(db_query(db_rewrite_sql("SELECT tid FROM {term_node} WHERE nid = $current_nid;")));
   if (empty(
$tid)){
    return
'';
   }

   switch (
$button_type) {
     case
'next':
      
$sort= 'DESC';
      
$case = '< ';
       break;
     case
'prev':
      
$sort = 'ASC';
      
$case = '> ';
       break;
     case
'parent':
      
$name = db_result(db_query(db_rewrite_sql("SELECT name FROM {term_data} WHERE tid = $tid;")));
       return
l($label.'up', "$name", array('title' => $name, 'class' => $class));
       break;
     case
'last':
      
$sql  = "SELECT n.nid, n.title FROM node n INNER JOIN term_node t ON n.nid = t.nid";
      
$sql .= " INNER JOIN term_data r ON t.tid = r.tid WHERE n.type = '". $type ."'";
      
$sql .= " AND r.tid = $tid AND n.status = 1 ORDER BY nid ASC LIMIT 0 , 1";
      
$result = db_fetch_array(db_query(db_rewrite_sql($sql)));
       return
l($label, 'node/'. $result['nid'], array('title' => $result['title'], 'class' => $class));
       break;
     case
'first':
      
$sql  = "SELECT n.nid, n.title FROM node n INNER JOIN term_node t ON n.nid = t.nid";
      
$sql .= " INNER JOIN term_data r ON t.tid = r.tid WHERE n.type = '" .$type. "'";
      
$sql .= " AND r.tid = $tid AND n.status = 1 ORDER BY nid DESC LIMIT 0 , 1";
      
$result = db_fetch_array(db_query(db_rewrite_sql($sql)));
       return
l($label, 'node/'. $result['nid'], array('title' => $result['title'], 'class' => $class));
       break;
     default:
       return
NULL;
       break;
   }
  
$sql = "SELECT n.nid, n.title FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid ";
  
$sql .= "INNER JOIN {term_data} r ON t.tid = r.tid WHERE n.type = '". $type ."' AND n.nid ". $case;
  
$sql .= $current_nid ." AND r.tid = ". $tid ." AND n.status = 1 ORDER BY nid ". $sort;
  
$result = db_fetch_array(db_query(db_rewrite_sql($sql)));
   if (!
$result) {
     return
NULL;
     return
l($label.$name, "$type/tid/$tid", array('title' => $name, 'class' => $class));
   } else {
     return
l($label, 'node/'. $result['nid'], array('title' => $result['title'], 'class' => $class));
   }
}
?>






Book Navigation

In Default Drupal, long links in the book navigation leave it all messed up. This CSS fixes that problem, ensuring that the 'up' link is always in the middle.

The links' colours and styles are defined further up in style.css.

/*******************/
/* Book Navigation */
/*******************/

.clear-block:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}

.clear-block {
display: inline-block;
}

/* Hides from IE-mac \*/
* html .clear-block {
height: 1%;
}
.clear-block {
display: block;
}
/* End hide from IE-mac */

.book-navigation,
.content .book-navigation .page-links {
margin-top: 2em;
}

.book-navigation .page-previous {
text-align: right;
width: 42%;
display: block;
float: left;
}
.book-navigation .page-up {
margin: 0 5%;
width: 4%;
display: block;
float: left;
}
.book-navigation .page-next {
text-align: left;
width: 42%;
display: block;
float: left;
}






page.tpl.php

This is the first place that Drupal looks when generating the web site. It looks at where you are, then uses the correct theme to display. In the original Drupal theme, this is usually where the code found in frontpage.tpl.php is found.

I split it up because I wanted my admin pages to look different to the rest of the site - albeit only slightly.

<?php
if ($is_front) {
    include
"frontpage.tpl.php";
    return;
}
else {
$uri_request_id = $_SERVER['REQUEST_URI'];
$section = explode("/", $uri_request_id);
switch (
$section[1])
  {
    case
'admin': include 'admin.tpl.php'; break;
    case
'indiablog': include 'page-india.tpl.php'; break;
    case
'event': include 'page-calendar.tpl.php'; break;
    default: include
'inside.tpl.php';
  }
}
?>




See links below for details:






node.tpl.php

The default node layout. Drupal calls stories or posts 'nodes'.

The 'taxonomy' is how content is categorised, on Skip The Budgie I have one main category called 'on_topic' and I need to restrict the displayed category to just one 'term', even if the node is linked to several different terms. This is Very Important, as $node_terms just picks up everything.

<div class="cnr-<?php print $node_terms; ?>">
<div class="tax-<?php print $node_terms; ?>">
<div class="node<?php print ($sticky) ? " sticky" : ""; ?>">
  <?php if ($page == 0): ?>
    <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
  <?php endif; ?>
<div class="info">posted <?php print $date ?></div>

<?php if ($terms): ?>
<?php
/* sort taxonomy links by vocabulary 1 */
$terms1 = taxonomy_node_get_terms_by_vocabulary($node->nid, 1);
if (
$terms1) {
  print
'<div class="terms">topic: ';
     foreach (
$terms1 as $key => $term1) {
    
$lterm1 = l($term1->name, 'taxonomy/term/'.$term1->tid);
  print
$lterm1;
     }
  print
'</div>';
}
?>

  <?php endif; ?>

  <div class="content">
  <?php print $content ?>
  </div>
<br class="clear" />

/* show 'add new comment' etc */
<?php if ($links): ?>
  <div class="links"><?php print $links ?></div>
<br class="clear" />

/* show the social bookmarking images */

  <div class="service-links">
    <?php print $service_links ?>
  </div>

<?php endif; ?>
</div>
</div>
</div>
<br class="clear" />




see below for different layouts for particular node types.