0

I want to hide a field on a node when its value is set to zero using a content type. Here is what I had done so far to achieve my goal.

function pay_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global $user;

  if ($node->type=='salary_info' && $op=='presave') {
    $public_holiday = $node->field_sstru_public_holiday[0]['value']; // here I get the value that I set to 0(No) or 1(YES) using a select list. 

    if ($public_holiday == 0) {
      $nid = node_load(207919); // load node where i want to unset this field   
      unset($nid->field_pd_public_holiday[0]); // here iam wrong and confused 
    }
    else {
      drupal_set_message("HOLIDAY");
    }
  }

what should I do to unset my field?

  • you should not use unset to hide field. Instead you can use $node->field_sstru_public_holiday[0]['value'] = FALSE . Here is a reference i found. – nit3ch Apr 6 '13 at 10:23
  • @nit3ch instead i can use ? i had edited my code i.e. unset($nid->field_pd_public_holiday[0]); – aashi Apr 6 '13 at 10:28
  • @aashi edited the above comment please check. – nit3ch Apr 6 '13 at 10:29
  • @nit3cch i just want that the field whos valuet i set to 0(NO) should not be seen when the page is viwed.. – aashi Apr 6 '13 at 10:30
  • @nit3ch the reference shows to hide or unset field when we use formid. But here iam working with hook_node_api. what to do? – aashi Apr 6 '13 at 10:36
0

I'm not sure why you are using presave operation.

If you want to hide a field on node view you should use view operation.

So your code should be as the following code:

function pay_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {   
  if ($node->type=='salary_info' && $op=='view') {
    $public_holiday = $node->field_sstru_public_holiday[0]['value'];

    if ($public_holiday == 0) {
      // you don't need to load node again.
      unset($node->field_pd_public_holiday);    
    } else {
      drupal_set_message("HOLIDAY");
    }
  }
}

Edit:

If you want to hide field on another content type or node using presave operation, then you need to load node and unset field value then save node again using node_save function

So your code should look as the following:

function pay_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global $user;

  if ($node->type=='salary_info' && $op=='presave') {
    $public_holiday = $node->field_sstru_public_holiday[0]['value']; // here I get the value that I set to 0(No) or 1(YES) using a select list. 

    if ($public_holiday == 0) {
      $node = node_load(207919); // load node by nid
      unset($node->field_pd_public_holiday[0]); // unset field from node object
      node_save($node); // save node after unsetting the unwanted field value. 
    }
    else {
      drupal_set_message("HOLIDAY");
    }
  }
}
  • field_pd_public_holiday - this field is on another node i.e why iam loading it. – aashi Apr 8 '13 at 4:25
  • fields-field_sstru_public_holiday and field_pd_public_holiday are on different page. I want to hide field - field_pd_public_holiday when field - fields-field_sstru_public_holiday is set to 0(NO). – aashi Apr 8 '13 at 5:04
  • @aashi updated my answer. – Aboodred1 Apr 8 '13 at 15:25

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.