Hi guys, is there a quick tute somewhere on taking an existing field and moving it to the Publishing Options vertical tab? I have managed to get it there, but upon save, get a screen like this:

Accepted answer

Thanks @argiepiano, in the end this was all that was required to make it work, whilst inside the form_alter of the custom content type:

// Add the custom field to the vertical Tabs, remove from the regular form

$form['options']['field_ready_to_publish'] = $form['field_ready_to_publish'];

// May need to set a weight to position the field where it is desired.

$form['options']['field_ready_to_publish']['#weight'] = 10;

// Hide the original field. This does not interfere with saving, etc.

unset($form['field_ready_to_publish']);

No need for hook_update nor presave, and it was the hide that was stuffing up, resulting in the json. Having tested this, it successfully saves, and checks/unchecks as expected. Cheers!

Most helpful answers

Hi onix. So, what form are you altering? Is this a custom form, or a form provided by a module?

Also, the implementation of hook_node_update() is not going to work. This hook is invoked AFTER the node has been saved, so moving the field to its rightful place will have no effect, since the node has been saved. 

Instead I recommend either adding a custom submit handler (using array_shift so that it's called before other submit handlers), or using hook_node_presave(), which is invoked before the node is saved. 

The garbage output you posed looks like json output that hasn't been properly interpreted by the browser - probably because the browser is expecting HTML instead of json. This may have been caused by a number of factors, most likely some interaction with other modules and the fact that the field is in the wrong place when saving.

Also, using hide outside the context of a template: not a good idea. Since you are going to move the field back to its correct place in the node object, you should simply unset the original field before sending the form back to the form builder.

Comments

Can you post your full form alter hook implementation?

if($form['#form_id']==="role_node_form"){

//dpm($form);

$nid = $form['nid']['#value'];

$newnode=node_load($nid);

global $org_id;

$org_id=$newnode->field_organisation['und'][0]['target_id'];

$form['options']['field_ready_to_publish'] = $form['field_ready_to_publish'];

// May need to set a weight to position the field where it is desired.

$form['options']['field_ready_to_publish']['#weight'] = 10;

// Hide the original field. This does not interfere with saving, etc.

hide($form['field_ready_to_publish']);

}

}

function vhb_node_update($node) {

// Check if field exists on node form.

if(isset($node->path['field_ready_to_publish']['und'][0]['value'])) {

// Redefine field in original definition and unset in 'path' fieldset.

$node->field_ready_to_publish['und'][0]['value'] = $node->path['field_ready_to_publish']['und'][0]['value'];

unset($node->path['field_ready_to_publish']);

}}

That was using the above post, thanks @stpaultim, and the subsequent Chris burge link off the bottom. Of course the _form_alter code is truncated above what is seen here. In any case, I get : But saving produces the black and white garbage.

Hi onix. So, what form are you altering? Is this a custom form, or a form provided by a module?

Also, the implementation of hook_node_update() is not going to work. This hook is invoked AFTER the node has been saved, so moving the field to its rightful place will have no effect, since the node has been saved. 

Instead I recommend either adding a custom submit handler (using array_shift so that it's called before other submit handlers), or using hook_node_presave(), which is invoked before the node is saved. 

The garbage output you posed looks like json output that hasn't been properly interpreted by the browser - probably because the browser is expecting HTML instead of json. This may have been caused by a number of factors, most likely some interaction with other modules and the fact that the field is in the wrong place when saving.

Also, using hide outside the context of a template: not a good idea. Since you are going to move the field back to its correct place in the node object, you should simply unset the original field before sending the form back to the form builder.

Thanks @argiepiano, in the end this was all that was required to make it work, whilst inside the form_alter of the custom content type:

// Add the custom field to the vertical Tabs, remove from the regular form

$form['options']['field_ready_to_publish'] = $form['field_ready_to_publish'];

// May need to set a weight to position the field where it is desired.

$form['options']['field_ready_to_publish']['#weight'] = 10;

// Hide the original field. This does not interfere with saving, etc.

unset($form['field_ready_to_publish']);

No need for hook_update nor presave, and it was the hide that was stuffing up, resulting in the json. Having tested this, it successfully saves, and checks/unchecks as expected. Cheers!