2

This is the code I execute with "Execute PHP" with the Devel module.

$lang = LANGUAGE_NONE; // Replace with ISO639-2 code if localizing
$node_type = 'my_writings'; // Machine name of the content type

$query = new EntityFieldQuery;
$result = $query
  ->entityCondition('entity_type', 'node')
  ->propertyCondition('type', $node_type)
  ->execute();

if (!empty($result['node'])) {
  $nodes = entity_load('node', array_keys($result['node']));

  foreach($nodes as $node) {

    $str=<<<END
 <h3> TITLE</h3>
<p>
Hello world.
</p> 
END;
    // Replace field_foo with the machine name of the field to update.
    // - 0 refers to specific within the field array, for when the field contains
    //    multiple values. If the field only has one value, it should be 0.
    $node->field_author_box[$lang][0]['value'] = $str;
    node_save($node);
  }
}

The output I get is this HTML code like this.

<h3> TITLE</h3>
 <p>
    Hello world.
</p> 

The input format is set to Full HTML, but the HTML tags are shown escaped, even if in the database they appear normal.

screenshot

I tried to re-save the node; then, it is normal. I tried also to use html_entity_decode() without luck, if I do the same with body field then it works fine, but with a new field its showing escaped characters.

What could the problem be?

  • It seems you haven't escaped your output code, so what we see is what you want to get and not what you are getting. What is the text format used on this node? "full html" If not, you need to save it too. – Mołot May 31 '13 at 6:25
  • If it's one-time operation, something in lines of this question will help, just adjust to your field. – Mołot May 31 '13 at 6:33
  • Could you get a screen shot with all field_author_box_* columns? Not only value? – Mołot May 31 '13 at 7:12
4

Drupal core does not modify the data you save in the database [*]. All filters are applied only when displaying the data - this ensures that you never loose any data.

Assuming that you are using Drupal 7, and that the field in question has filtered text enabled, then what is happening is that when you save your new field, you are not setting what the filter format should be - so it defaults to plain text. You need to set the format of the field when saving it. In your code, you would do this as follows:

$node->field_author_box[$lang][0]['value'] = $str;
$node->field_author_box[$lang][0]['format'] = 'full_html';
node_save($node);

[*] Though some contrib modules may do this. For instance WYSIWYG editors such as CKEditor do modify your data (though in that case only when using the UI).

  • wow it works, now i understand, default settings, Thanks. There is also a module that that does bulk change values in an easy UI called Administration views drupal.org/project/admin_views – user16924 May 31 '13 at 11:34
  • Contrib modules can modify content before save. That's something worth remembering when debugging surprising content changes. – Mołot May 31 '13 at 12:45
  • @Mołot : thanks, I've edited the response to point this out. – Alice Heaton May 31 '13 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy