9

Basically, I want to put first name & surname together into $node->field_par_contact_name

But it gives me warning. (See below)

function partnership_node_presave($node) {
  if($node->type == 'partnership') {
    $first_name = $node->field_par_contact_first_name['und'][0]['name'];
    $surname = $node->field_par_contact_surname['und'][0]['name'];

    // https://drupal.org/project/field_hidden
    // Hidden variable
    $node->field_par_contact_name['und'][0]['value'] = $first_name. ' '. $surname;

    // Warning after save: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 173 of .../drupal-7.23/includes/entity.inc
  }
}

Updated, please see image: enter image description here

1
  • are u submitting the form programmatically ? I mean are you using hook_form_alter ? and then drupal_form_submit somewhere in your code?
    – harshal
    Commented Nov 18, 2013 at 5:48

1 Answer 1

6

For starters, you're probably using the wrong array key for the field value that you're saving; 'name' should be 'value' there.

$node->field_par_contact_name['und'][0]['value'];

Second, you're actually using Taxonomy Term Reference fields for your "first name" and "surname" fields. One important point about Reference fields is that you can't always assume that the referenced entities have been fully loaded and are available in your object. By default, when the node object is loaded, that field only looks like this:

$node->field_par_contact_first_name (Array)
  'und' (Array)
    0 (Array)
      'tid' => 284

Only the 'tid' key is definitely there. 'name' is a property of the referenced term, and isn't usually automatically available. (Subsequent code may actually load the referenced term and make it available within the $node object, but that's just happenstance if so.)

You would have a much easier time of it if your "First Name" and "Surname" fields were text fields instead of references. Then, those values would belong directly to the node itself, rather than to a separate taxonomy term being referenced.

If you're stuck using taxonomy term references, though, then you should make a point in your code to load the referenced term, then get the values you need from the $term->name property itself.

3
  • See image above. There is no 'value'
    – kenpeter
    Commented Nov 18, 2013 at 11:30
  • Ah, you're using Taxonomy Term Reference fields for the first name and surname, not text fields as I assumed. That's peculiar. :) Updated the answer.
    – Les Lim
    Commented Nov 18, 2013 at 16:55
  • you are correct.
    – kenpeter
    Commented Nov 18, 2013 at 22:33

Your Answer

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

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