Drupal Snippets

From MtTWiki

Jump to: navigation, search

Contents

Format the "Submitted by..." text of a node ($submitted) (DRUPAL 6)

<?php print $submitted; ?>

Default Submitted text

<?php print t('Submitted by !username on !date', array('!username' => theme('username', $node), '!date' => format_date($node->created))); ?>
Result: Submitted by admin on Fri, 01/01/2011

Change Submitted text

<?php print t('!username on !date', array('!username' => theme('username', $node), '!date' => format_date($node->created))); ?>
Result: admin on Fri, 01/01/2011

Format Submitted Date

<?php print t('Submitted !date', array('!date' => format_date($node->created, 'custom', "M j, Y"))); ?>
Result: Submitted Jan 4, 2011

(PHP 4, PHP 5) date — Format a local time/date

How to count results in a db query (DRUPAL 6)

Firstly, db_num_rows() was dropped in D6

Count results without limit

<?php
// Count totally the sticky nodes
$stickyNodes= db_result(db_query("SELECT COUNT(*) FROM {node} WHERE sticky='1'"));
print $stickyNodes;
?>

Count results with limit

<?php
// Count the sticky nodes among the first 10 (10 is the limit)
$result = db_query("SELECT * FROM {node} WHERE sticky='1' LIMIT 10");
$stickyNodes=0;
while (db_fetch_object($result)) {
$stickyNodes++;  
}
print $stickyNodes;
?>

Load User profile field in node (DRUPAL 6)

This example shows how to load the node author user and not the logged in user. Try this code in the node.tpl.php

<?php $node_author = user_load(array('uid' => $uid));
print $node_author->profile_description; ?>

Generate jquery ui tabs for all blocks in a region (DRUPAL 6)

<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#tabs").tabs();
});
</script>
</head>
<body style="font-size:62.5%;">
 
<div id="tabs">
<ul>
<li><a href="#fragment-1"><span>One</span></a></li>
<li><a href="#fragment-2"><span>Two</span></a></li>
<li><a href="#fragment-3"><span>Three</span></a></li>
</ul>
<div id="fragment-1">
<p>First tab is active by default:</p>
</div>
<div id="fragment-2">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</div>
<div id="fragment-3">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</div>
</div>
</body>
</html>


Jquery ui tabs implementation

1. yourthemename.info

regions[sidebar_tabs] = Sidebar Tabs

2. page.tpl.php

.
.
.
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#tabs").tabs();
});
</script>
</head>
.
.
.
<body>
.
.
.
<?php if ($sidebar_tabs) { ?><?php print yourthemename_tabs_blocks('sidebar_tabs'); ?><?php } ?>
.
.
.
</body>

3. template.php

function yourthemename_tabs_blocks($region) {
  $output = $t = '';
  $i = 1;
  if ($list = block_list($region)) {
	$c = count($list);
    foreach ($list as $key => $block) {
	  $t .= "<li><a href=\"#tabs-" .$i. '">'. $block->subject.'</a></li>';
      $output .= "<div id=\"tabs-" .$i. '">'.$block->content.'</div>';
	  $i++;
    }
  }
  $output = '<div id="tabs"><ul>'.$t.'</ul><div id="tabs-container">'.$output.'</div></div>';
  return $output;
}
Personal tools
Namespaces
Variants
Actions
Main Menu
Premium Themes
Toolsets
Freebies
Navigation
Toolbox
This wiki is hosted by morethanthemes.com, creators of top quality premium and free themes for Drupal 6 and 7, as well as HTML and PSD themes.
Find out more
for Drupal 6 & 7