PHP

Truncate A Drupal View Node Title

UPDATE

It appears that I missed the very obvious solution to this problem (!) which, as pointed out by a couple of commenters, is to use the ‘Trim this field to a maximum length’ option within the settings for the node title field.

I’ll leave this post in place as I think it still serves as a useful guide on how to create template override files for views, but please be advised that for implementing truncation of a views node title it is easier to:

  • navigate to the view's 'Edit view viewname' page (/admin/build/views/edit/viewname) and click the ‘Node: Title' link
    Truncate Drupal view node title
  • check the ‘Trim this field to a maximum length’ checkbox, and adjust the settings as required
    Truncate Drupal view node title

Using PHP In Your Drupal Theme's Footer

The problem:
Drupal’s default 'Footer message' area (to which you can add content via the admin section - Administer > Site configuration > Site information) will not parse PHP.

If, for example, you add a copyright message to your footer message area such as "© Copyright 2009" it'll be fine until the New Year begins and you have to go in and manually change it to "© Copyright 2010". So, instead, it would be great if you could handle the date dynamically using the following PHP code (see http://php.net/date for further PHP date details):

© Copyright <?php echo date('Y'); ?>

However, try adding that to the default 'Footer message' area and it will simply output the entirety of the code as text, ignoring the PHP tags.

No PHP in a Drupal footer message

So, what to do?

Solution 1:
Create a block (admin/build/block/add) containing the code, set its input format to PHP, and then place that block in the footer region via the main blocks settings page (admin/build/block). Crazy simple.

Drupal + PHP - How To Auto-Truncate Content At The End Of A Word

The problem:
Whilst working on an aggregation site recently I needed to find a way to truncate the length of aggregated posts so that they only showed the first 200 or so characters of the post. However, I also wanted the split point in the post to occur at the end of a whole word, as opposed to part way through a word, and add some visual indication that there was more still to read.

The solution:
I'll first show the entire solution and then walk through how it works afterwards.

In your theme's node.tpl.php file the node content will most usually be output using code similar to:

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

Find this code and replace it with the following code (minus the opening and closing php tags which are just used here to improve the formatting):

<?php
<div class="content">
  <?
php
  $mywords
= explode(" ", $content);
 
$finalstring = "";
  foreach(
$mywords as $word) {
    if(
strlen($finalstring) <= 197) {
       
$finalstring = $finalstring . " " . $word;   
    } else {
       
$finalstring = $finalstring . " <strong>[...]</strong>";   
        break;
    }
  }
  echo
$finalstring;
 
?>

</div>
?>

Adding A Class To A Drupal Theme For Logged In Users

I was working on a Drupal theme recently which required a number of different elements to be present on a number of different pages, but only for logged in users.

Using a little PHP I was able to add a class of 'in' to the body which would only appear in the html when a user was logged in.

To do this, just find the opening <body> tag in your page.tpl.php file, and add in the code below.

<body<?php global $user; ?><?php if ($user->uid): ?> class="in"<?php endif; ?>>

Then, you'll be able to write a general rule for logged out users, such as:

body {
background: #fff;
}

And a more specific rule for logged in users, such as:

body.in {
background: #000;
}

Adding A Permalink

I got the idea for today's post after I received an e-mail during the week from Colin who was wondering how I added a permalink, and then styled the links section so that everything (comments, read more, permalink etc.) lines up.

I thought this might be of general interest, so here's the answer.

It's basically just a two step process:

  • add some markup to the node.tpl.php file
  • add some css to the style.css file

Step 1 - Add markup to the node.tpl.php file

To start with I just added some markup to the links div, and then positioned the whole links div at the end of my node.tpl.php file, right before the closing node div tag.

   <?php if ($links): ?>
   <div class="links">
      <p class="permalink"><a href="<?php print $node_url ?>" title="Permalink to this my Drupal blog post">Permalink</a></p>
      <?php print $links ?> 
   </div>
   <?php endif; ?>

This positioned the permalink within the links div before the other links.