Is there a way that I can remove the preview button in my custom node?

When I send the user to the add screen, i use this function:

function weblinks_form_weblink_user() {
drupal_goto("node/add/weblinks", $query = "ref=user");
}

The only way I can think of is hacking the core code...

please help...

Comments

tille’s picture

hi nasium,

I don't know if you're still interested in this - I found your post because I was looking for the same thing and found it from here: http://drupal.org/node/62319

First I was a bit scared of going for my (first) own 'module' - but it is really not that dramatic.. For drupal 5.x you just need the .module and the .info file and throw the function (as descriped on http://drupal.org/node/62319) in that .module-file and that's basically it..

I added a form_id-check to control which form has a preview button and which form not.. - ..the 'echo' is there to find out what form_id I have on a specific page - here's my code for hiding the preview button from the comments and from mycontenttype:

function mymodulename_form_alter($form_id, &$form) {

// echo("form_id".$form_id);

if(($form_id=="mycontenttype_node_form")||($form_id=="comment_form")) {
  unset ($form['preview']);
}
}

..hope it helps..

greetings, t..

___________________________
my pictures: www.bilderbook.org

___________________________

bleak26’s picture

Thankyou for leaving this how-to , it worked first time.

tille’s picture

..thank you for the thank you! ..glad to hear it helped..:]

greetz, t..

___________________________
my pictures: www.bilderbook.org

___________________________

stormer’s picture

Hi Tille

thanks for the info on how to deal with the pesky Preview button. However, I can't get it to work - I've created a nopreview.module (with nothing but the your code) and a nopreview.info file and placed them in a "nopreview" folder with my other modules but it's not showing up in the module list? What am I doing wrong.

Thanks very much for any help and cheerio

Ole

scottrigby’s picture

Hi,

well, here's what I added for the module:

<?php
// $Id$

/**
 * @file
 * Hides preview button on nodes.
 */

/**
 * From: http://drupal.org/node/138393#comment-259431
 *
 * Make sure to add the correct node-types below:
 */ 
function mymodulename_form_alter($form_id, &$form) {

// echo("form_id".$form_id);

if(($form_id=="contractor_node_form")||($form_id=="contractor_coupon_node_form")) {
  unset ($form['preview']);
}
}

and the info file:

; $Id$
name = nopreview
description = Allows users to annotate nodes.
package = Custom
version = "$Name$"

Uploaded in a folder called "nopreview" to sites/all/modules.
I enabled it on the modules page. Then tried to create those content-types - but preview button still shows.
then considered maybe it was the ccontributed theme I was using - so switched to Garland. Still didn't hide preview button.

Does anyone have any insight into this?

Thanks in advance!

Scott

twhitchu’s picture

I believe you are forgetting to rename the "function mymodule_form_alter(..." part. In your example, you would put "function nopreview_form_alter" instead. and this file would be named nopreview.module. The info file would then be named nopreview.info (putting both of those files in a folder called /sites/all/modules/nopreview).

ashiwebi’s picture

I have content type which is developed using cck.I donot want the preview button on that.Is your approach help me.

rkdeveloper’s picture

un comment the echo("form_id".$form_id); and check once again whether form ids are written correct. one more thing you need to check is function name. if your module name is "noextrainfo", your function name should be noextrainfo_form_alter($form_id,&$form).

that's it.

blogjunkie’s picture

Hi,

Is it possible to hide the Preview button without a module? I just want to hide it for 1 content type. Thanks,

David

richykerr’s picture

Try this using CSS

I entered the following into my themes style sheet. Worked a charm. But it does remove all preview buttons on the site.

#edit-preview {
display:none;
}

mrfelton’s picture

create a comment-form.tlp.php in your theme directory (if you don't already have one). And add this:

    $form['preview'] = null;
    print drupal_render($form);

--
Tom
kirkdesigns.co.uk - web design and development

--
Tom
www.systemseed.com - drupal development. drupal training. drupal support.

ContentChris’s picture

I got this from another thread and I modified it slightly to work within my own module

/**
* Here we disable in step 1 the submit button. In step 2 we disable the preview button*/
function metadatagath_form_alter(&$form, $form_state, $form_id){
	if($form_id == 'metadatagath_node_form'){
		if($form['step']['#value'] < 2){
			$form['#rebuild'] = TRUE;
			unset($form['buttons']['submit']);
			$form['buttons']['preview']['#value'] = t('Next');
		}
		else{
			$form['#rebuild'] = FALSE;
			$form['#redirect'] = TRUE;
			unset($form['buttons']['preview']);
		}
	}
}

This line: unset($form['buttons']['preview']);
That removes the preview button just for this "metadatagath_node_form" form.

eforth’s picture

Perfectly! Just changing the appropriated variables, it works perfectly. Thanks!

Abhinesh Sharma’s picture

Hi tille,

It's simply use the form alter hook to resolved this problem : have a look below example.
use below code for custom module:-

function modulename_form_alter($form_id ,&$form){

if($form_id == "content_type_name_node_form"){

unset($form['buttons']['preview']);

}
}

and you found form_id to print below example :-

function modulename_form_alter($form_id ,&$form){
print_r($form_id);
}

caktux’s picture

// $Id:

/**
* Implementation of hook_menu
*
* For settings page
*/
function nopreview_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/nopreview',
      'access' => user_access('administer site configuration'),
      'title' => t('Hide Preview Buttons'),
      'description' => t('Hide Preview Buttons by content type.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('nopreview_settings_form'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
* Form function for settings
*/

function nopreview_settings_form() {
  $options = array('yes','no');
  $node_types = node_get_types('names');
  $node_previews = variable_get('nopreview_node_types', array());
  $form['description'] = array(
    '#tyoe' => 'markup',
    '#weight' => '-100',
    '#value' => t('Use these settings to configure which content types to hide the preview button for'),
  );
  $form['nopreview_node_types'] = array(
      '#tree' => TRUE,
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#title' => t('Preview button by node type'),
  );
  foreach ($node_types as $key => $value) {
    $form['nopreview_node_types'][$key] = array(
      '#type' => 'select',
      '#title' => t('Show Preview button for'). " $value",
      '#default_value' => $node_previews[$key],
      '#options' => $options,
    );
  }
  
  return system_settings_form($form);
}

/**
* Hide the buttons
*/
function nopreview_form_alter($form_id, &$form) {

  $nopreviews = variable_get('nopreview_node_types', array());

  if ($nopreviews[$form['type']['#value']] == 1) {
    unset ($form['preview']);
  }
}
EyeChartBrew’s picture

....at least not with my D6.4 build.

warning: Missing argument 1 for nopreview_menu() in /home/my_website/public_html/sites/all/modules/nopreview/nopreview.module on line 10.

Line numbers added for clarity:

 10 function nopreview_menu($may_cache) {
11  $items = array();
12  if ($may_cache) {
13   $items[] = array(
14    'path' => 'admin/settings/nopreview',
15      'access' => user_access('administer site configuration'),
16      'title' => t('Hide Preview Buttons'),
17      'description' => t('Hide Preview Buttons by content type.'),
18      'callback' => 'drupal_get_form',
19      'callback arguments' => array('nopreview_settings_form'),
20      'type' => MENU_NORMAL_ITEM,
21    );
22  }
23 return $items;
24 }  

Anybody have any ideas...??

I've tried every trick mentioned here so far to not show a simple "preview" button (or better, disable it), with nil results....

Cheers,
//TB

EyeChartBrew’s picture

Came across this sandbox, where "preview_plug" module does EXACTLY what I needed: killing off the Preview button on a node-type by node-type basis (or across the entire site, by default):

http://cvs.drupal.org/viewvc.py/drupal/contributions/sandbox/deekayen/mo...

Cheers!
//TB

francewhoa’s picture

Hiding PREVIEW button for one specific page with css: http://drupal.org/node/194890#comment-1161353

---

How to contribute to Drupal.

Loving back your Drupal community result in multiple benefits for you  
nathaniel’s picture

nopreview.module

<?php

// $Id:

/**
* Implementation of hook_menu
*
* For settings page
*/
function nopreview_menu() {
    $items = array();
    $items['admin/settings/nopreview'] = array(
      'title' => 'Hide Preview Buttons',
      'description' => 'Hide Preview Buttons by content type.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('nopreview_settings_form'),
      'access arguments' => array('administer imageapi'),
    );
  return $items;
}

/**
* Form function for settings
*/

function nopreview_settings_form() {
  $options = array('yes','no');
  $node_types = node_get_types('names');
  $node_previews = variable_get('nopreview_node_types', array());
  $form['description'] = array(
    '#tyoe' => 'markup',
    '#weight' => '-100',
    '#value' => t('Use these settings to configure which content types to hide the preview button for'),
  );
  $form['nopreview_node_types'] = array(
      '#tree' => TRUE,
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#title' => t('Preview button by node type'),
  );
  foreach ($node_types as $key => $value) {
    $form['nopreview_node_types'][$key] = array(
      '#type' => 'select',
      '#title' => t('Show Preview button for'). " $value",
      '#default_value' => $node_previews[$key],
      '#options' => $options,
    );
  }
 
  return system_settings_form($form);
}

/**
* Hide the buttons
*/
function nopreview_form_alter(&$form, $form_state, $form_id) {

  $nopreviews = variable_get('nopreview_node_types', array());

  if ($nopreviews[$form['type']['#value']] == 1) {
    unset($form['buttons']['preview']);
  }
}
?>

nopreview.info

name = No-Preview Button
description = Remove preview button (especially for courses)
package = Other
core = 6.x

version = "6.x-1.0"
core = "6.x"
project = "No-Preview Button"
stevekerouac’s picture

Thanks.

Any chance we could expand it to cover comments and their preview buttons...?

ccshannon’s picture

That's fantastic, but as another pointed out, does not account for comment forms.

In Drupal 6.10 I have tried both these and neither works:

	if ($form_id == "comment_form") {
		unset($form['preview']);		
	}
	if ($form_id == "comment_form") {
		unset($form['buttons']['preview']);		
	}

Anyone have any idea why neither of these works? Hmmmmmm.

ccshannon’s picture

The first one of mine should work. I'm working with another developer who has been focused on theming. He had another custom module that re-created the comment form's Preview button after I unset it (because his module is 'heavier' than mine, it executes its hooks after mine). No wonder!

berenddeboer’s picture

Use:

unset($form['buttons']['preview']);

Darko’s picture

If you ever decide to contribute this as a module, let me know. I have ported it to drupal 5 if someone wants it.

skizzo’s picture

could the proposed solution(s) find its way into contributed modules?

Gerald Mengisen’s picture

Just for the record:
http://drupal.org/project/preview

then go to "Post Settings" and there you can globally disable the preview button.

slimx’s picture

guys, just give it an access = 0 to almost anything you want to hide in the form..
this is something that I've been using for a long time now and it's better than unset because sometime doing unset breaks down the form.. once I've tried to hide the path and menu items (collapsible) by unsettling them and the nodes would not appear in proper places.. but hiding is not an extreme solution as unsettling, because everything is still there, you just choose to hide it in the form that the user is previewing.

example - Hiding the preview button.

ModuleName_form_alter(&$form, &$form_state, $form_id) {
$form['preview']['#access'] = 0;
}

Remeber that that first you have to put preview settings optional.. it's not wise to hide something that is required.. you can put preview optionally by going to the content type settings (under edit)

hope this helps..
take care..

slimx’s picture

i've just realize that this is under drupal 5... this solution is for drupal 6 (also i don't know if it will work in drupal 7, although I'm guessing it will)..

littleneo’s picture

butyou made a typo (drupal 6) :

$form['buttons']['preview']['#access'] = 0;

jag339’s picture

Folks,

In Drupal 7, unset ($form['preview']); does not un-set the preview button.
The following does not work either:
unset ($form['preview']);
unset($form['buttons']['preview']);
$form['preview']['#access'] = 0;
$form['preview'] = null;
And yes, I did enable my module, and the naming conventions are correct. I cleared the cache every time I made a change to the module.

Does anyone know why this little tiny thing is so difficult in version 7? And more importantly, how to get it to work?

Thanks

p.s. I installed and enabled the "preview" module, and it doesn't work either! (http://drupal.org/project/preview)

nathaniel’s picture

I think it is built into D7... Did you try this?

For nodes:
Under the title field "Preview before submitting" select Disabled.

For comment settings:
Under "Preview comment" select Disabled.

jag339’s picture

Yes, that worked! Cheers!

Alpinist1974’s picture

In response to Jag339, the correct way to do this for Drupal 7, through hook_form_alter, in a custom module, is:

unset($form['actions']['preview']);

using ['buttons'] does not work.

Matt

jaypan’s picture

Actually, using:

$form['actions']['preview']['#access'] = FALSE;

Is better than using:

unset($form['actions']['preview']);

Contact me to contract me for D7 -> D10/11 migrations.