简体   繁体   中英

How to set 'Author' for a node type on a custom form?

I have following code...

I have a node type called MY_NODE_TYPE.

I have set up a cusdtom URL for my node type: 'node/%/bla'). At this URL my 'default author' field is set to 'Anonymous' when it should be admin (uid 1).

I'd like to know where I should set uid to be 1 if following code does not work. (How can I make this code work?)

function bla_menu() {
  $items['node/%/bla'] = array(
  'title' => t('Bla'),
  'page callback' => 'bla_callback',
  );
}

function bla_callback () {
  module_load_include('inc', 'node', 'node.pages');
  $new_node = new stdClass;
  $new_node->type = 'bla';
  node_object_prepare($new_node);
  $new_node->language = LANGUAGE_NONE;
  $new_node->uid = 1;//This is wehre I set uid to be one and is not effective
  return drupal_get_form('MY_NODE_TYPE_form', $new_node);
}

You should implement hook_node_presave to set the values you need to change there.

Code sample:

function MODULE_node_presave($node) {
    if($node->type === 'MY_NODE_TYPE') 
        $node->uid = 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM