1

I have a few fields in one of my content types that will not always have values entered for them. One such field is "Booking Contact." When content is created, if the author does not know the booking contact, then they are supposed to leave the field blank. However, this means that the field data is empty, and the field does not display in the node at all. I'd like to display a message for these empty fields-- similar to Views' no results text -- so that users can see that there is not a value assigned to the field. What would be the best way to go about doing this?

EDIT: I'd like to avoid using "default value" to define this. The message I want displayed doesn't make as much sense in the creation form, and will likely be confusing to users. Unless, of course, there's a way to hide the default value in the creation form.

  • you could set the default value of the field to 'Unknown' or whatever and then that would show up instead of it being empty – Jimajamma Jul 21 '12 at 13:46
  • I had thought about that, but that value shows up in the field when the node is being created, too. Unknown would be fine, but the rest of the message that I want displayed doesn't really make sense when displayed in the form. – Mrweiner Jul 21 '12 at 18:22
  • Is it for Drupal 6? You wrote "CCK", so I suppose yes... But you should assign that to the appropriate tag. "but the rest of the message that I want displayed doesn't really make sense" - I didn't understand the counter-argument, can you explain that? Thanks. – Sk8erPeter Oct 10 '12 at 7:37
  • Actually like I said below, I solved this a while ago and forgot I had opened up this issue. It was actually for D7. And by "doesn't make sense" I meant that default text I wanted to include HTML and whatnot, so having that in the field on the form when a user creates a node is not very user friendly. – Mrweiner Oct 11 '12 at 14:37
1

The empty_fields modules will help in this case

  • it's a very good module for Drupal 7. But I think the OP wants to have a solution for Drupal 6 (he wrote "CCK"). – Sk8erPeter Oct 10 '12 at 7:34
  • Never knew about this module empty_fields. Looks good in the first look. Will certainly give this a try sometime. – Sumeet Pareek Oct 10 '12 at 8:34
  • I completely forgot I opened this issue, but I actually came across that module through another post and it did exactly what I needed. So, although I had already answered the question on my own, this was still my solution. – Mrweiner Oct 11 '12 at 14:34
0

I assumed you already know $node.

you can write the code belowed!

$node=node_load($nid);
$temp=$node->yourfield['und'][0]['value'];

if(empty($temp))
{
   drupal_set_message(t('alerting for empty field'));
}
  • When I use this code, I'm getting: Fatal error: Call to undefined function node_load(). Any idea why? – Mrweiner Jul 21 '12 at 19:17
  • you can write this code at hook_node_submit() and then the problem will be fixed – cobenash Jul 22 '12 at 0:52
0
yourmodule_node_submit($node, $form, &$form_state)
{

     if(empty($form['yourfield']['#value']))
     {
           drupal_set_message(t('alerting for empty field'));
     }
}

but if u already know the node id, u can use node_load

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.