I'm trying to make a check that the user has not selected two contradictory checkboxes. The following code makes the change OK during the change of a node, but not during the creation. In both new and update, the conditions are met and it enters the unset() part of the code. But only during the update does it actually work. Ideas how to fix? Thanks!

<?php
/**
* Implements hook_node_presave().
*/
function lingopolo_node_presave($node) {
 
// Ensure that phrases do not appear in the "Level" lesson
 
if ($node->type == 'thai_word'
 
&& isset($node->field_difficulty_level['und'][0]['tid'])
  && isset(
$node->field_recording_type['und'][0]['tid'])) {
   
$level = $node->field_difficulty_level['und'][0]['tid'];
   
$recording_type = $node->field_recording_type['und'][0]['tid'];
    if (
$recording_type == LINGOPOLO_RECORDING_TYPE_PHRASE
   
||  $recording_type == LINGOPOLO_RECORDING_TYPE_DIALOGUE) {
     
drupal_set_message("Removing from the Level lesson, since Recording Type is Phrases or Dialogues.");
      unset(
$node->field_difficulty_level['und'][0]);
    }
  }
}
?>

Comments

Because you didn't pass it with reference.

Change
/ function lingopolo_node_presave($node) {
To

/ function lingopolo_node_presave(&$node) {

// Ayesh

Hi Ayesh,

Thank you for your suggestion. I tried it and it didn't work.

I see that the documentation does not require this
https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hoo...
but I thought that I'd try anyway.

The reason seems to appear here:
https://api.drupal.org/comment/48578#comment-48578

With PHP 5, objects passed as arguments for a function are passed by reference. The example is correct, since Drupal 7 requires PHP 5.

So basically the $node object is being passed by reference anyway.

drupalshrek
Maintainer of the Flipcard module visible in use at the website to learn French and learn Thai