I am running into a strange issue where I can't export some of the cck types I make. Here is the steps I have gone through to reproduce the error.

Create a content type, with the following:

-Name: Topic
-Type: topic
-Description: A topic is the top-level organizational placeholder
-Body field label: Description

Everything else I left at default

Exporting this content type works just fine. My issue is with the next one.
I create another content type with the following:

-Name: Goal
-Type: goal
-Description: Goals belong to a particular topic.
-Body field label: Description

Create the content type and then go back and add a field to it with these settings:

-Name: goal_topic
-Field type: Node Reference -> Select List

Click 'Create Field' then then fill out the next form with these settings:

-Label: Topic
-Check the box next to required
-Check the box next to topic

Click 'Save Field Settings'

Now go try to export the Goal content type. I get the following errors:

* Topic : This post can't be referenced.
* The default value is invalid.

I thought maybe because the field is required, and I haven't set a default value, things weren't working so I went back and unchecked Required, but I still get the same errors. Any Ideas? Anyone else able to reproduce this?

As a sidenote, I have a bunch of other CCK types to create this hierarchy where
Topics have Goals, Goals have Objectives, Objectives have Action Steps.
I use node references on the rest on down and they export just fine. Goals is just being stubborn and won't export. Any insight is appreciated.

Thanks,
-Steve

Comments

verbal@drupal.org’s picture

I narrowed the problem down a bit, but I dont understand why this export would work in the first place.

from line 113 of nodereference.module


function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      $refs = _nodereference_potential_references($field, TRUE);
      foreach ($items as $delta => $item) {
        $error_field = isset($item['error_field']) ? $item['error_field'] : '';
        unset($item['error_field']);
        if (!empty($item['nid'])) {
          if (!in_array($item['nid'], array_keys($refs))) {
            form_set_error($error_field, t('%name : This post can\'t be referenced.', array('%name' => t($field['widget']['label']))));
          }
        }
      }
      return;
  }
}

doing some debugging I was able to produce this output

Item:
Array
(
    [nid] => Array
        (
            [0] => 0
        )

)
Refs:
Array
(
)

doing some reading of the _nodereference_potential_references() function, the part of the code that is returning an empty array, which subsequently gets stored into $refs is:

    if (isset($string)) {
      $string_clause = $exact_string ? " AND title = '%s'" : " AND title LIKE '%%%s%'";
      $related_clause = "(". $related_clause .")". $string_clause;
      $args[] = $string;
    }

    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title AS node_title, n.type AS node_type FROM {node} n WHERE ". $related_clause ." ORDER BY n.title, n.type"), $args);
  }

  if (db_num_rows($result) == 0) {
    return array();
  }

So my understanding is that because there are no nodes of the Topics content type, the select statement returns Nothing in the query. And a check is done to see if 0 is in the empty array, which it isn't, and an error is returned. So how would you export a type where a nodereference is required, but there are no nodes of the referenced type yet? It seems like there needs to be a modification to how cck nodereference deals with required fields that do not have any nodes of the required type yet. During the use case of my CCK types, the user will be probably create a "Topic" and then create a "Goal", but the nodereference field is still required because a Goal belongs to a Topic, and should not be able to be created without the reference to the topic. Anyone else having this issue, or at least understand the problem here?

-Steve

verbal@drupal.org’s picture

Ugh.. this still isn't working. I went as far as using devel module and recorded the macro of submitting these two content types and that is even crashing on the Goals content type.

-Steve

jdevoid’s picture

I ran into this problem as well. My solution to get around it was to add an check to the validation if statement(see the 2nd if statement).

function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      $refs = _nodereference_potential_references($field, TRUE);
      foreach ($items as $delta => $item) {
        $error_field = isset($item['error_field']) ? $item['error_field'] : '';
        unset($item['error_field']);
        if (!empty($item['nid'])) {
          if (!in_array($item['nid'], array_keys($refs))&&arg(0)!='admin') {
            form_set_error($error_field, t('%name : This post can\'t be referenced.', array('%name' => t($field['widget']['label']))));
          }
        }
      }
      return;
  }
}

it appears that this validation function is being run even on the admin pages for output and that is the cause of the problem(kinda hope I am wrong but the fix still works as I have used it).

Hope this helps out.

Jeremy Devoid

rokr’s picture

Steve, are you using "Auto Nodetitles"? I recently ran into the same problem. When i disable "Auto Nodetitles" for the specific content type, node referencing works fine. While enabled an error appears after previewing/submitting which tells: "This post can't be referenced." Can't really tell if this is an issue of CCK, Auto Nodetitle, maybe Token.
Please tell me if you got news about that issue.

ciao, Ronald

rokr’s picture

In addition to my last comment i have to say that it works fine with a fresh install of drupal using only the few modules needed (cck node reference, token, auto nodetitles) in current versions (stable and dev). The nice side of this fact is i got this working in general. The bad: something still is wrong with the other instance of drupal and i don't know which kind inconsistence happened and which module was guilty. I stumbled upon this issue while i was looking for error message "This post can't be referenced."

cheers, shibby

aangel’s picture

Version: 5.x-1.5 » 5.x-1.6-1

I'm having the same problem and upgrading the modules did not get rid of the error.

However, your workaround did get rid of the error. Thanks for providing it.

greggles’s picture

I just ran into this problem as well. It's a really old installation I use for development. CCK was on 5.x-1.5 so I upgraded it (I know that was insecure and buggy, but this was just local and runs behind multiple firewalls...).

I can't really offer any advice on what might have caused this. I've installed and uninstalled so many modules that it could have been anything. I followed the advice from comment #3 to add && arg(0)!='admin' to the second if and that "fixed" the problem by letting me export the data.

greggles’s picture

Title: Node Reference export Problems? » Node Reference export Problems
Category: support » bug

Some more information on the topic - I exported this (using the suggested workaround/hack) and then imported it into a new site that was a completely fresh install. The import failed. I then manually recreated the field and tried to export this new definition and it failed again. So, I think that limits it down to something specific to this field and no longer related to my old system nor a cck field that gets upgrade.

Interestingly, this only affects one of my fields on a site that uses nodereference three times.

attribute field_portfolio field_owned_items field_item
type select list select list select list
label Portfolio Owned Items Item to Trade
required true true true
multiple values false false false
content types that can be referenced Portfolio Item Item or Portfolio
advanced view not used not used not used
other fields on content type None! 1 5

So, that last line is what helped me figure this out. To fix the problem I added a second field "dummy" to this content type. Then it exported the data (I didn't try an import yet).

I'm changing this to a bug report though I'm not sure how to fix it at least we have more solid steps to repeat.

scor’s picture

Title: Node Reference export Problems » Node Reference export/import problems - This post can't be referenced
Version: 5.x-1.6-1 » 5.x-1.x-dev
Component: General » nodereference.module

I'm facing the same problem, it seems to happen with any 'nodereference_select' widget type, even if there already exists nodes of the referenced type.

marked http://drupal.org/node/186579 as duplicate.

@greggles: have you tried to reimport the content type with the second 'dummy' field? did it work?

EDIT: reposting greggles' fields in table layout:

attribute field_portfolio field_owned_items field_item
type select list select list select list
label Portfolio Owned Items Item to Trade
required true true true
multiple values false false false
content types that can be referenced Portfolio Item Item or Portfolio
advanced view not used not used not used
other fields on content type None! 1 5
greggles’s picture

Thanks for the reformat - somehow my formatting got lost. No - I haven't tried an import with that content type again.

colan’s picture

Just thought I'd point out that this has nothing to do with the Automatic Nodetitles module. I've run into this without using it.

isaac77’s picture

Like scor (comment #9), my experience leads me to believe this issue is related use of the select list widget. After changing my nodereference field to use the autocomplete widget, I was able to export the content type without the "This post can't be referenced" and "The default value is invalid" errors.

karens’s picture

Status: Active » Closed (duplicate)

This is the a part of the problem in #128038: Critical failures using drupal_execute() on nodes with CCK fields, so we'll work on it there.