I am gonna repost this question:

"My question is simple: How do I render a submit button invisible?

In some cases my form submit button should not be shown.

Thanks..."

I have a multipage form(3 pages), and it should only be possible to submit the form on page 2(!).

Actually, I would like the submit button, as well as the delete and preview buttons to be hidden except on page 2(other buttons handle the navigation)

How can I accomplish this?

Comments

nevets’s picture

The following function would be added to your module, replacing 'yourmodule' with you modules name and 'yourtype' with your content type.
In building your form you need to track if this is the lastpage (as in the example) setting it to false for everything but the last page. You could use different logic for when to hide the buttons.

<?php

function yourmodule_form_alter($form_id, &$form) {
        // Is this our form
	if ( $form_id == 'yourtype_node_form' ) {
                // Should we hide the previews/submit/delete buttons
		$lastpage = $form['lastpage']['#value'];
		if ( ! $lastpage ) {
			unset($form['preview']);
			unset($form['submit']);
			unset($form['delete']);
		}
	}
}
Bossen’s picture

Thank you very much!

It works perfectly...

Subhransu.addweb’s picture

You can use below alternative method. Syntax is as per Drupal 8

/**
 * Implements hook_form_alter
 */
function custom_module_name_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

	if ($form_id == 'user_register_form') {

		$form['actions']['submit']['#access'] = FALSE;
	}

}