0

In the hook_node_load function I want to alter a field's value. I printed the node variable and got the following structure for the field:


    [field_myfield] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => 1316760775
                        )

                )

        )

So I should write something like $node->field_myfield[und][0]['value'] = 'sth'. My site has 2 available languages, and [und] will be changed when language is changed. How can I alter the field value for all languages?

2 Answers 2

1

The $node object will have its language in a language variable. So, create another variable to work with like $lang = $node->language;; then you can write $node->field_myfield[$lang][0]['value'] = 'sth';, where the language will be dynamic with the $node object.

2

I'd go with entity_metadata_wrapper(), see this blog post for more info on that.

$node = node_load(323);
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->title = 'New title for the node';
$wrapper->save();

It works for any property: title, fields, author, etc.

1
  • 1
    I guess the second line should be: $wrapper = entity_metadata_wrapper('node', $node); Commented Sep 24, 2011 at 7:29

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.