Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm trying to update a field with the format full_html. But when I'm updating the field, all the html tags will disappear and the old text is still there in the safe_value. So how can I update the the text in field and keep the existing structure and the html-tags?

I have tried this:

$node = $node_load($my_node_id);
$node->body['und'][0]['value'] = 'My new text.';

My field before editing

[body] => Array
    (
        [und] => Array
            (
                [0] => Array
                    (
                        [value] => <p>My old text.</p>
                        [summary] => 
                        [format] => full_html
                        [safe_value] => <p>My old text.</p>

                        [safe_summary] => 
                    )

            )

    )

My field after editing

[body] => Array
    (
        [und] => Array
            (
                [0] => Array
                    (
                        [value] => My new text. //All the html-tags are removed
                        [summary] => 
                        [format] => full_html
                        [safe_value] => <p>My old text.</p> //The old text is still there

                        [safe_summary] => 
                    )

            )

    )
share|improve this question
What's the goal there in changing the field value? – David Thomas Jan 9 at 9:24
I want to update the body text for a node programmatically. – user1149117 Jan 9 at 9:27
Is that your exact code? This bit should produce an exception: $node = $node_load($my_node_id); as I doubt you've defined $node_load = 'node_load'; anywhere – Clive Jan 9 at 9:53

2 Answers

up vote 0 down vote accepted

Calling node_save() won't automatically re-load the node so you'll need to do it manually if you want to see the computed field values:

$node = node_load($my_node_id);
$node->body['und'][0]['value'] = 'My new text.';

node_save($node);

// Re-load the node from the db
$node = node_load($node->nid, NULL, TRUE);

// $node->body now has the values you would expect.

I just tried that and can confirm I get the same behaviour as you (the safe_value is not updated) if the call to node_load() is commented out.

share|improve this answer
Thank you Clive. The problem is that the html-tags in the value will be removed. Before I updated the node, the text was wrapped with <p>-tags. – user1149117 Jan 9 at 10:41
Yes that's what should happen, you've explicitly written: $node->body['und'][0]['value'] = 'My new text.'; without HTML tags. The HTML corrector is run when the content is displayed at the other end. To recreate you can run $html_body = check_markup($node->body[LANGUAGE_NONE][0]['value'], 'full_html'); – Clive Jan 9 at 10:46
Thank you! You saved my day! :) – user1149117 Jan 11 at 15:52

I want to update the body text for a node programmatically.

If you want to update the body text, probably better to do it once when the node is saved.

for example, one of the node API hooks

It will be simpler and easier to update there.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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