3

I'm using this code in Rules, after saving a certain type of content, in execute custom PHP code. By now, i'm just trying to create anothe node of that type after the first is created. The problem is that this code creates a new node indeed, but without any custom fields. It has only language associated to it. Any idea on what's wrong? I followed the tutorial at http://fooninja.net/2011/04/13/guide-to-programmatic-node-creation-in-drupal-7/

$nodo = new stdClass(); // Create a new node object
$nodo->type = "capitoli"; // Or page, or whatever content type you like
node_object_prepare($nodo); // Set some default values
$nodo->language = 'it'; // Or e.g. 'en' if locale is enabled
$nodo->uid = $node->uid; // UID of the author of the node; or use $nodo->name
$nodo->field_numero_capitolo[$nodo->language][0]['value']="2220";
$nodo->field_link[$nodo->language][0]['value']="aafafsf";
$path = 'node_created_on' . date('YmdHis'); // I prefer using pathauto, which would override the below path
$nodo->path = array('alias' => $path);
if($nodo = node_submit($nodo)) { // Prepare node for saving
    node_save($nodo);
    echo "Node with nid " . $nodo->nid . " saved!\n";
}

If I replace

$nodo->language = 'it';

with

$nodo->language = LANGUAGE_NONE;

this code create another node identical to the first one, not considering the values I used in the code and I can't figure out why.

EDIT: I tried doing as suggested in comments

$nodo->field_numero_capitolo['und'][0]['value']="2220";
$nodo->field_link['und'][0]['value']="aafafsf";

And it seems to work. What i did was to insert directly 'und'. Generally I think it is not a good idea to write directyl 'und', but these two fields are a link and a number, that should not change depending on language.

  • Try using 'it' instead of 0 when you add values to the $node (for example, $nodo->field_link[$nodo->language][0]['value']="aafafsf"; becomes $nodo->field_link[$nodo->language]['it']['value']="aafafsf";) – sanzante Apr 6 '15 at 10:11
  • @tunic no, it is not worinkg neither in this way – Sanci Apr 6 '15 at 10:15
  • Forget my comment, I readed the code in a wrong way. Your code seems to be right. – sanzante Apr 6 '15 at 10:32
  • I'm assuming it somehow relates to the language because I've seen similar code that works using LANGUAGE_NONE. Does the answer to this question help at all: drupal.stackexchange.com/q/36949/10729 – rooby Apr 6 '15 at 10:34
  • @rooby, indeed reading that questions helped me. I edited my question with my changes. – Sanci Apr 6 '15 at 11:13
0

I better way is to simply use the entity api and entity_metadata_wrapper for entity CRUD operations:

$nodo = entity_create('node', array('type' => 'capitoli'));
$nodo_wrapper = entity_metadata_wrapper('node', $nodo);
$nodo_wrapper->author = $node->uid;
$nodo_wrapper->field_numero_capitolo->set('2020');
$nodo_wrapper->field_link->set("aafafsf");
$nodo_wrapper->save();

See http://www.drupalcontrib.org/api/drupal/contributions!entity!entity.module/function/entity_metadata_wrapper/7

| improve this answer | |
  • 1
    The question specifically relates to language so could you possibly change your answer to add setting of the language? – rooby Apr 7 '15 at 0:39
0

If you're using Rules anyway why don't you use the Create new entity action which should take care of everything? You will need to use the Set a data value action multiple times and possibly the Save entity action too.

In answer to your question I think you need to set the language before node_object_prepare so your code would become:

$nodo = new stdClass(); // Create a new node object
$nodo->type = "capitoli"; // Or page, or whatever content type you like
$nodo->language = 'it'; // Or e.g. 'en' if locale is enabled
node_object_prepare($nodo); // Set some default values

Also you don't need the node_submit($nodo) line

| improve this answer | |

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.