1

I'm trying to update a node->body using a php script in my root directory of my Drupal 7.22 installation.
I call the script through a browser (firefox) with [http://MYSITEURL/nodeupdate.php]
But somehow I don't get it to work. Here is the code I use:

<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$nid = 9; // YES node 9 exists.
$node = node_load($nid); // where $nid is the node id

echo "$node->title <br>"; // Just to make sure it changes the right node and it does.

$node->body[$node->language][0]['value']   = "Test updated";

if( $node = node_submit($node)) {
    node_save($node);
    echo "Node with nid " . $node->nid . " updated!<br>";
}

print_r($node); // Check if we see any changes.
?>

The script executes fine without any errors. Through the print_r function I can see that the "changed" field and "revision_timestamp" field are changing.
However the body doesn't change!
I have been googling and found many posts from which I gather that this is the way to do it.
What am I missing?

  • Just for a sanity check, also run print(user_access('edit any page content'));, replacing "page" with the machine-readable content type. It should return 1 (true). Also, is the body not updating when you view the node, or even when you run print_r($node); in your script? – Eric N Jul 31 '13 at 15:56
  • Also, flush your caches just to make absolutely certain there isn't a problem there. There shouldn't be, but let's cover all the bases. – Eric N Jul 31 '13 at 16:00
  • I'm using my development site so caching is turned off. Nevertheless did a clear cache, no improvement. – Robert Jul 31 '13 at 16:17
  • Added print(user_access('edit any page content')); to my script and it returned 1. The node I what to update is a basic page so I didn't change 'page' in the code snipped. – Robert Jul 31 '13 at 16:21
  • When viewing the page through the browser will show no updated page. The print_r function doesn't show an updated page also. Invoking the script several times will show only changes in the [created] => *** [changed] => *** and [revision_timestamp] => *** fields. – Robert Jul 31 '13 at 16:27
1

Make sure you're logged into the site as a user with permissions to update the specified content type, because that session will carry over to your bootstrapped script. If you're logged out, you're essentially attempting to update the node as an anonymous user in your script, and permissions will come into play.

Try adding...

global $user;
print_r($user);

...to your script and see what, if anything, that outputs.

| improve this answer | |
  • Did that and the output after login is<br> ... [session] => [roles] => Array ( [2] => authenticated user [3] => administrator ) )<br> But no change of the body field. – user19682 Jul 31 '13 at 15:17
  • Refer to my comments on your initial question. – Eric N Jul 31 '13 at 16:09
0

Solved without knowing why.

If I use
$node->body[LANGUAGE_NONE][0]['value'] = "Test updated";
it works, and if I use
$node->body[$node->language][0]['value'] = "Test updated";
it doesn't.

| improve this answer | |
  • So for some reason your node's language is set to 'nl' (Dutch?), but it displays the body value under 'und' (undefined and what the LANGUAGE_NONE constant evaluates to). So there's probably a discrepancy in your locale or i18n settings, and unfortunately I'm not versed in how those work, but I think that LANGUAGE_NONE is the "safe" option because it will act as the default value if there is no other, or if no other module defines a value for the "current" language. – Eric N Aug 1 '13 at 13:18

Your Answer

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

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