0

How can I check (via PHP code) if a content type is published?

I have a content type called 'House' and a content type called 'People'. On the 'People' content type, there's a field called 'House'. If the user didn't publish a 'House' content type yet, I want to hide this field.

I thought I could do it by using a hook_form_alter, isn't it possible? If it is, I just don't know how to check if a content type was published or not. Something like: if (isset($node->type == 'House')) ... Of course it doesn't work.

Do you have any suggestions?

1

Something like this?

hook_form_alter() {
  /* check that you're looking at the right form here.. */
    global $user;
    $type_to_check = 'House';

    $query = db_query("SELECT nid FROM node n JOIN users u ON n.uid = u.uid WHERE n.status = 1 AND u.uid = %d AND n.type = '%s'", $user->uid, $type_to_check);

    if(empty(db_result($query))) {
      /* kill the thing you don't want here */
    }
}
0

You can do this using a template to your content type. In the respective new template of your content type, you can add the condition to do this:

if ($node->status == 1){
unset($node->your_field_name);//here, for example, the field name is 'your_field_name'
}

note: to create a new template to your content type, look inside your theme directory, and create a copy of file node template node.tpl.php, then rename this copy according your Content Type, for example, if machine name of your content type is my_content_type, then:

node-my_content_type.tpl.php

and then add the code indicated above.

8
  • I'm not sure if this is exactly what I need. I'm gonna be more specific: I have a content type called 'House' and a content type called 'People'. On the People' content type, there's a field called 'House'. If the user didn't publish a content type 'House' yet, I want to hide this field. I thought I could do it using a custom module, is it possible? Something like: if ($form_id == 'people_node_form') { if (!isset($node->type == 'House')) { $form['field_house']['#type'] = 'hidden'; } } Is it possible to do something like that?
    – MMalke
    Aug 25 '11 at 18:51
  • you can build a custom module and then use hook form_alter() (here is one example of this hook stackoverflow.com/questions/1067782/drupal-form-alter) and review this documentation lullabot.com/articles/modifying-forms-drupal-5-and-6
    – cigotete
    Aug 25 '11 at 19:04
  • Yes, I'm using hook_form_alter. But I don't know how to check if a specific content type is published or not, inside the hook_form_alter. I was thinking about something like: if (!isset($node->type == 'House')), but it doesn't work.
    – MMalke
    Aug 25 '11 at 19:11
  • I think that you can use $node->status
    – cigotete
    Aug 25 '11 at 19:23
  • hello, I think that you can use $form_state['values']['status']
    – cigotete
    Aug 25 '11 at 19:30
0

You have to query the nodes table for the uid of the current people node and type house. If you don't find a house unset the field in your hook_form_alter function.

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.