Hi
I'm creating a module that inserts storylinks programmatically and it's working fine. What I would like to do is add tags to the created node and I don't know the best way to approach this. I have the variables that I'd like to insert and was wondering if you or anyone would have any pointers? I'm aware it will be taxonomy that I'd need to insert, but I am stuck on the format/how to?
I realise node_save is perhaps cheating but I couldn't get drupal_execute to work.

		$node = new stdClass();
		$node = array(
		'type' => 'storylink',
		'title' => $threadtitle,
		'body' => $postbodycontent,
		'vote_storylink_url' => $urlstory,
		'promote' => 1,
		'status' => 1,
		'comment' => 2,
		'sticky' => 0,
//		$node->taxonomy = array($o->tags ());
		);
	$node = node_submit($node);
	node_save($node);
		}
	}

All and any help is much appreciated thanks.

Comments

Cromicon’s picture

I should clarify.

I have 2 variables I'd like to insert into a node as tags. One is $charactername and the other is $forumname.
Looking at the way nodes have tags it looks like I need to convert those two variables into an array, which I can do.
What I'm stuck on is how to actually insert that array into the node in the code example above and what the array should be called.

I could also be totally wrong and if so please let me know where I've gone wrong! Many thanks.

Cromicon’s picture

ok I know the Vocabulary ID ($vid) is 3....

Cromicon’s picture

Not sure what to do with it though...Hopefully someone has a suggestion =)

tetramentis’s picture

Status: Active » Fixed

You need this simple line:

$node->taxonomy = array('tags' => array('3' => $your_tags));

here '3' is vid, and $your_tags is just a comma-separated string, where each item will become a tag.

See here for more.

Cromicon’s picture

Thanks for your response and trying to help me, it's much appreciated. I've seen that thread and tried that line but it doesn't work in the context of the function I quoted above.

For example:

$node = new stdClass();
$node = array(
'type' => 'storylink',
'title' => $threadtitle,
'body' => $postbodycontent,
'vote_storylink_url' => $urlstory,
'promote' => 1,
'status' => 1,
'comment' => 2,
'sticky' => 0,
$node->taxonomy = array('tags' => array('3' => $sanitisedcharactername));
);
$node = node_submit($node);
node_save($node);
}
}

results in:
Parse error: syntax error, unexpected ';', expecting ')' in /home/starflee/public_html/sites/all/module in line 114 (where line 114 is the line you suggest.

So then I think that maybe I need to just add taxonomy as I do the likes of comment and sticky.

So:

$node = new stdClass();
$node = array(
'type' => 'storylink',
'title' => $threadtitle,
'body' => $postbodycontent,
'vote_storylink_url' => $urlstory,
'promote' => 1,
'status' => 1,
'comment' => 2,
'sticky' => 0,
'taxonomy' = array('tags' => array('3' => $sanitisedcharactername));
);
$node = node_submit($node);
node_save($node);
}
}

which gives:
Parse error: syntax error, unexpected '=', expecting ')' in /home/starflee/public_html/sites/all/modules/line 114
So I'm thinking yes, thats perhaps because I should use => after 'taxonomy' so I try that.

Rewarded with:
Parse error: syntax error, unexpected ';', expecting ')' in /home/starflee/public_html/sites/all/modules/line 114

And now I'm puzzled. I'm sure I'm doing something obviously wrong but can't see it, can anyone?

Cromicon’s picture

Status: Fixed » Active
tetramentis’s picture

I would advise not to mix array and object representations. If you say $node = new stdClass();, then please use object notation like $node->type = 'storylink';. Or, if you write $node = array(...);, then do not use $node = new stdClass(); and object notation - be consistent, that will save you from many troubles.

Having said that, try this fragment:

$node = new stdClass();
$node->type = 'storylink';
$node->title => $threadtitle;
$node->body => $postbodycontent;
$node->vote_storylink_url => $urlstory;
$node->promote => 1;
$node->status => 1;
$node->comment => 2;
$node->sticky => 0);
$node->taxonomy = array('tags' => array('3' => $sanitisedcharactername));

It works for me.

See also this more complete example:

#!/usr/bin/php
<?php
# run from Drupal root directory

# set HTTP_HOST or drupal will refuse to bootstrap, when run from cli
$_SERVER['HTTP_HOST'] = 'yoursitename';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['REQUEST_METHOD'] = 'POST';

error_reporting(E_ALL);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

# prepare admin user object
global $user;
$user = user_load(1);

      $node = new stdClass();
      $node->is_new = TRUE;  //If this is a new entry, add this; otherwise replace with $node->nid
      $node->type = 'story';
      $node->status = 1;
      $node->promote = 0; // 1: promote to front page
      $node->title = $fields[0];
      $node->comment = 2;  // allow comments?
      $node->sticky = 0;
      $node->format = 2; // 1: filtered, 2: full HTML
      $node->language = 'en';
      #CCK
      $node->field_full_text = array(0 => array('value' => trim($fields[3]), 'format' => 2,),);
      # tags
      $node->taxonomy = array('tags' => array('5' => $fields[11]));
      # categories
      $node->taxonomy[$topic_key] = array('tid' => $topic_key, 'vid' => '2', 'name' => $fields[10]);
      # save
      $node = node_submit($node); // performs validation
      if ($node->validated) {
         $node->created = (int)$fields[13];
         $node->changed = (int)$fields[13];
         node_save($node); //Actually save or edit the node now
      }
      else {
         echo 'Node: ' . $node->title . " was not saved.\n";
      }
?>
Cromicon’s picture

Status: Active » Fixed

Tetramentis, you are the best!

Thanks very much for being so patient and taking the time out, not only to help me with my issue but also to educate me generally in PHP arrays. I took your fragment example and you'd quite cleverly included a test. For example $node->sticky => 0); needed the > removing and I think an erroneous ) to become $node->sticky = 0; which along with the rest of the example worked great.

I'm looking at the more complete example you've posted and it's given me some ideas and a practical framework to follow too, so thank you very much!

tetramentis’s picture

You are welcome, great I could help.

Those errors were unintentional ;) , just an erroneous copy-paste... fortunately, the 'extended' example from the actual working script didn't have errors.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.