2

So there is a custom field I added called 'qid'. I can see that it is being created with each node. My issue is that I cannot programmatically save a value to my custom field. In the code below I will display and instance where I add two values, one to my custom field qid and another to the Ubercart uc_product field height. Height will be saved successfully and qid will not.

function my_module_test_page() {
  $output = t('foo');

  $node = node_load('151');
  dpm($node);
  $node->height = '555';
  $node->qid = '222';
  dpm($node);
  node_save($node);
  $test = node_load('151');
  dpm($test);

  return $output;

}

While I understand that this is probably not enough code to get to the root of the problem I have no idea where to look. I have scoured the node_save() function and I dont understand why my field is having a problem. If it helps I have checked field_data_qid in my MYSQL db; however, the table is empty.

EDIT: So the next interesting fold is that when I enter the QID field through the GUI it saves fine.

2
  • Is the qid field an integer or varchar in the database? If it's an integer then it makes sense because you're passing a string in as the value. Apr 5, 2013 at 15:41
  • Thats a good call Lester, thanks. I checked and it is listed as a varchar(255). Just to play around some more I tried submitting $node->qid as a string, an int, and an array with a string inside it - no luck. Apr 5, 2013 at 16:46

1 Answer 1

3

My suggestion would be to inspect an existing node that already has a value for field_data_qid. Is qid a field created through the Fields GUI? $node->qid is not where I'd expect to see a custom field like this.. $node->field_qid['und'][0]['value'] is a more likely candidate. You'd end up with:

function my_module_test_page() {
  $node = node_load(151);
  dpm($node, 'Pre-modified node');
  $node->field_qid['und'][0]['value'] = 222;
  dpm($node, 'Pre-saved modified node');
  node_save($node);
  // Passing TRUE for $reset to bump the cache
  $test = node_load(151, NULL, TRUE);
  dpm($test, 'Reloaded node');
  return 'Test page';
}

Also: node_load takes a nid integer as a parameter, not a string: http://api.drupal.org/api/drupal/modules%21node%21node.module/function/node_load/7

See also: https://stackoverflow.com/questions/7876458/drupal-6-drupal-7-migration-field-unds and Best practice for language key for "und" in hook_form_alter

5
  • I install the qid field in my .install file using field_create_field and field_create_instance as described in the docs for field creation. I'm starting to think the problem could be back there. Here's the code re: the field install. pastebin.com/MKGhuXqD Apr 5, 2013 at 17:02
  • $node->field_qid['und'][0]['value'] was exactly the problem. I also found the answer here: stackoverflow.com/questions/5448889/… Can you explain why this language thing is such an issue? Apr 5, 2013 at 19:23
  • 1
    i18n is a tough thing to master, but essentially the use of "und" - undefined language, leaves the option open of specifying per-language values for a given field, using the language code as the key. This gets important when your site needs to be multilingual, so Drupal fields, entities, and lots else are all built to be multilingual capable.
    – rypalmer
    Apr 5, 2013 at 19:49
  • rhetorical: why doesn't it just default to the language type which I already specified in my $node value (which isn't listed in the sample code above but in my implementation I do use). It seems like that would be a fair default situation and that $node->field_qid['rus'][0]['value'] would be the exception not the rule, ie I had a Russian language field attached to an English language node. Apr 5, 2013 at 20:35
  • It's that way to keep the structure of the arrays consistant, and to prevent people from being lazy. If these were object methods instead of arrays, it would be more doable, but that's not the design that was chosen. Drupal APIs... not quite as developer friendly as they could be.
    – rypalmer
    Apr 8, 2013 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.