Use the Services-3.x node resource to create a new *unpublished* node
-
22-10-2019 - |
Question
Is there a way that I can force a node to be unpublished by using the node resource?
Requirement: Site A defines a content type 'item' which is published by default. Site B needs to create 'item' nodes on A, and these auto-created nodes should be unpublished.
function toolreq_test_create() {
$toolreq_endpoint = variable_get("toolreq_endpoint", "");
$options = toolreq_service_login();
if (!is_array($options)) {
print "error<p>";
return;
}
//create a node
$node_data = array(
"title"=>"DHC Tool 04 dev",
"type"=>"item",
//"status"=> FALSE, //ensure unpublished
"status" => 0,
"language" => "und", //lanugage neutral
"field_webpage"=>array(
"und"=>array(0=>array("url"=>"http://berkeley.edu",)
)
)
);
//use json
$options['headers']['content-type'] = "application/json";
$options['method'] = 'POST';
$options['data'] = json_encode($node_data);
$request = drupal_http_request($toolreq_endpoint . '/node', $options);
}
Since the node table's status field is the mysql INT type (with a default of 1), I would think that I could pass status=>0 in the above code, but doing that results in a published node. I have tried passing 0 using both JSON and x-www-form-urlencoded data.
If I use status=>FALSE and use JSON, the node is created as unpublished, but I think that this is equivalent to inserting '' in the node.status field which results in a mysql warning:
mysql> INSERT INTO `bamboodirt`.`node` (`nid`, `vid`, `type`, `language`, `title`, `uid`,
`status`, `created`, `changed`, `comment`, `promote`, `sticky`, `tnid`, `translate`)
VALUES (NULL, 21, 'item', 'und', 'sql insert test', '0', '', '0', '0', '0', '0', '0', '0',
'0');
Query OK, 1 row affected, 1 warning (0.03 sec)
mysql> SHOW WARNINGS
+---------+------+----------------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: '' for column 'status' at row 1 |
+---------+------+----------------------------------------------------------+
I would live with that warning, but my webhost's mysql is less permissive and I get a real error (durning node_save() I think) on that server:
"SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'status' at row 1"
(Here's the thread over at Pantheon http://help.getpantheon.com/pantheon/topics/json_node_create_works_on_localstack_fails_with_error_500_on_pantheon.)
Is there a way that I can force a node to be unpublished by using the node resource, or do I need to write my own resource?
Solution
Try setting the 'status' field to TRUE
or FALSE
instead of 0
or 1
.