Using Text CCK field with multiple role-dependant "show" trims.

warning: Invalid argument supplied for foreach() in sites/all/modules/option_trim/option_trim.module on line 112.

Specifically,

          if ($relationship['preserve_values']) {
            foreach ($pre_saved_node->{$relationship['field2']} as $old_option) { //Error here
              $old_options[] = $old_option[key($field2_info['columns'])];
            }
          }

Probable fix (untested)

Line 92
$relationships = _option_trim_relationships($node->type);
++if($node->nid > 0) //Don't load the old one if there isn't any.  Should this be if(empty($node->nid)) ?
$pre_saved_node = node_load($node->nid);
foreach ($relationships as $key => $relationship) {
...
$old_options = array();
--if ($relationship['preserve_values']) {
++if ($relationship['preserve_values'] && !empty($pre_saved_node)) {
  foreach ($pre_saved_node->{$relationship['field2']} as $old_option) {

As it is, the value still ends up saving.

CommentFileSizeAuthor
#2 optiontrim_create-preserve.patch908 bytesRoboPhred

Comments

RoboPhred’s picture

Title: Error on initial node creation with "perserve values" » Error on initial node creation with "preserve values"
RoboPhred’s picture

Status: Needs work » Active
StatusFileSize
new908 bytes

Fixed it and rolled a patch. Since "$pre_saved_node" is only used in the preserve_values case, I moved it into there. This also saves a node_load call that might not be needed.

RoboPhred’s picture

Status: Active » Needs work
RoboPhred’s picture

Status: Active » Needs review
cyu’s picture

Status: Needs review » Fixed

I kept the node_load outside of the loop on all trims. Even though node_load will cache it, I wanted to only make one call to node_load (which it will still make even if there are no preserved value relationships for that content type). The warning should be gone though with http://drupal.org/cvs?commit=183012

Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.