Trouble forcing status=0 (unpublished) when using the node resource

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
bwood's picture

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.)

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?

Comments

You should use FALSE instead

ygerasimov's picture

You should use FALSE instead of 0 value for status value as it is checkbox in node's form. The matter is that node is saved not directly to database table but via drupal_form_submit() (in 7.x branch) so values are passed to node's form for submission. This is done specifically for validation.

Hope it helps.

Thanks for the quick

bwood's picture

Thanks for the quick reply!

This answer http://drupal.stackexchange.com/a/18720/3444 contradicts what you say about the data going via drupal_form_submit(), but maybe that person was talking about the 6.x version of the module.

With some debugging I

bwood's picture

With some debugging I verified that drupal_form_submit() is called. The FALSE value is converted to 0 by MySQL. In D7 common.inc: drupal_write_record() there is this comment:

// MySQL PDO silently casts e.g. FALSE and '' to 0 when inserting the value
// into an integer column, but PostgreSQL PDO does not. Also type cast NULL
// when the column does not allow this.

still have problem...

francis55's picture

I fell on this page because I was trying to do the following and it wasn't working:
$node->status = 0;

Having read this article I tried:
$node->status = FALSE ;
and also
$node->status = "FALSE" ; 
but still not working: the Published checkbox remains checked when I open the node to edit it.
I'm sure the node is saved because other attributes I set (such a $node->body = "test";) get taken into account.

I'm using Drupal 6

Here's my code

bwood's picture

I am successfully creating unpublished nodes with this code. Hope it helps:

/**
* Service request to create tool node
*/
function toolreq_create_tool($term) {
$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"=>$term->name,
                          "type"=>"item",
                         "status"=> false, //ensure unpublished (in json use false)
                        "language" => "und", //lanugage neutral
   );
$options['headers']['content-type'] = "application/json";
    $options['method'] = 'POST';
   $options['data'] = json_encode($node_data);
  $request = drupal_http_request($toolreq_endpoint . '/node', $options);

  watchdog('toolreq', t('Service request create tool node: ') . '%toolname', array('%toolname' => $term->name), WATCHDOG_DEBUG );
  $response = json_decode($request->data);
    watchdog('toolreq', t('Response: ') . '%response', array('%response' => $response->uri), WATCHDOG_DEBUG, $response->uri);
}

thanks for your fast input!

francis55's picture

I tried using the array approach as you suggest, but I got an error message:
Fatal error: Cannot use object of type stdClass as array
I receive the $node in the context of an action implementing hook_action_info, to use in a view with the Bulk Actions module.
(I am still baffled by php arrays, I'm more familiar with objects and functions and html - are there any good resources for mastering arrays in the context of Drupal? ...)

below is a barebone version of what I tried:

function test_action_info ()
{
  return array(
       'test_setUnpublished_action' => array(
       'type' => 'node',
       'description' => t('Set unpublished'),
       'configurable' => FALSE,
       'hooks' => array(
       'nodeapi' => array('presave', 'insert', 'update', 'view'),
       'comment' => array('insert'),
       )
    ),

  );
}
function test_setUnpublished_action(&$node, $context) {
$node['data']['status']= false;
// gives: Fatal error: Cannot use object of type stdClass as array 
node_save($node);
}

Same issue with Promote/Unpromote

ethanw's picture

I'm having the same issue trying to unpromote nodes using a JSON API, but from the browser instead of PHP (using Backbone).

I opened this ticket for the issue, currently marked as a support request.

The short version is that I have tried both "0" and false for the values of the publish settings, with no luck in either case. Updating title and other values works fine, just not these values.

It looks like the JSON array that's getting sent over should match this spec pretty much to the letter. Has anyone gotten un-publishing/etc. of nodes using JSON via browser JS working? Any ideas?

Have you got a solution yet

stone_d's picture

Have you got a solution yet how to pass these values?

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: