A little background:

I'm trying to create a custom content type for short announcements on our school website. These announcements are usually only a few sentences long, but there are many of them during a week, and "more important" news items are drowning in them. I thought I would split them off and display them in a block on the side of the less ephemeral news.

In this new block I want to display two sentences for each announcement. If the announcement is longer than two sentences I would like the first sentence to be a link to the announcement node. These sentences are entered individually as custom fields when the node is created.

My problem:

I'm trying to combine the two sentences of the announcement in a computed field that I will use in the announcement block, but I'm getting "Validation error, please try again. If this error persists, please contact the site administrator." I'm assuming that this is Drupal's way of telling me that I don't know what I'm doing with the PHP in the Computed Code field. Drupal is right, I don't know what I'm doing, but I looked at a few examples and this is what I think I should be doing: (minus the <?php)

// ensure the node has an id by saving it if it is new
if (!$node->nid) {node_save($node);}

// if the node has a body, make a link
if ($node->body) {
  $link = l($node->field_announcement_first_senten[0]['value'], 'node/'.$node->nid);
  $sentence2 = $node->field_announcement_second_sente[0]['value'];
  $field = '<span class="announcement">' . $link . '<br />' . $sentence2 . '</span>';
}
// otherwise, just two sentences
else {
  $sentence1 = $node->field_announcement_first_senten[0]['value'];
  $sentence2 = $node->field_announcement_second_sente[0]['value'];
  $field = '<span class="announcement">' . $sentence1 . $sentence2 . '</span>';
};
// store the data in our computed field
$node_field[0]['value'] = $field;

I'm also open to the notion that I'm just doing this wrong, and there's a better way to do it. For instance, I know I don't really have to computer and store anything for this. I could just use $node->nid and the two fields on the fly, but I don't know how to do that. I could also just store $node->nid in my Computed Field and use the Display Format field of computed fields to perform the computation, but my PHP above would still be wrong. There aren't a lot of tools in my toolbox, and everything is looking like a nail.

Comments

WorldFallz’s picture

Maybe I'm not understanding what you're trying to do-- it there any reason you can't use a block view created with the http://drupal.org/project/views module?

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

marcvangend’s picture

I'm not a pro on the subject of computed fields, so I can't tell you why it doesn't work like this. I do have some thoughts about the other tools that could (should?) be in your toolbox.
First of all, you could split the content type into two content types, one for announcements and one for news. This would make it much easier to put the more important news items in their own block.
Second, you might want to try the contemplate module. Contemplate allows you to do about anything you want with the cck values in a node 'on the fly'.

lsommerer’s picture

I think you're right. I do want two content types. Sorry if that wasn't clear from the original post.

My problem was trying to get the format correct for displaying that new content type (sometimes with links and sometimes not). I have that field now and will use views to populate a block.

I'll look into contemplate. It sounds like that will let me do this without storing an extra field.

lsommerer’s picture

My problem doesn't seem to be related to the PHP code above (which does have an extra ";") . I believe I was seeing that error message because of the field I was using to automatically computer the titles for these nodes. Changing that and then working through the Computed Field again left me without an error.

But I would still be interested in hearing a better solution to this than using a computed field if anyone has one.

marcvangend’s picture

In some cases computed field simply is the best solution, let's make that clear. Especially when combining it with views: computed field stores a cck-field value in the database, so views can easily put it in lists and tables.